SlHelpers
Misc.h
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #pragma once
4 
5 #include <optional>
6 #include <string>
7 
8 #include <git2.h>
9 
10 #include "../helpers/Unique.h"
11 #include "Repo.h"
12 
13 namespace SlGit {
14 
18 class Reference {
19  using GitTy = git_reference;
21 
22  friend class Repo;
23 public:
24  Reference() = delete;
25 
27  const git_oid *target() const noexcept { return git_reference_target(ref()); }
29  const git_oid *targetPeel() const noexcept { return git_reference_target_peel(ref()); }
31  std::string symbolicTarget() const noexcept { return git_reference_symbolic_target(ref()); }
33  git_reference_t type() const noexcept { return git_reference_type(ref()); }
35  std::string name() const noexcept { return git_reference_name(ref()); }
36 
38  std::optional<Reference> resolve() const noexcept;
39 
41  GitTy *ref() const noexcept { return m_ref.get(); }
43  operator GitTy *() const noexcept { return ref(); }
44 private:
45  explicit Reference(GitTy *ref) noexcept : m_ref(ref) { }
46 
47  Holder m_ref;
48 };
49 
50 class Commit;
51 
55 class RevWalk {
56  using GitTy = git_revwalk;
58 
59  friend class Repo;
60 public:
61  RevWalk() = delete;
62 
64  bool push(const git_oid &oid) const noexcept {
65  return !Repo::setLastError(git_revwalk_push(revWalk(), &oid));
66  }
68  bool push(const std::string &id) const noexcept;
70  bool pushHead() const noexcept {
71  return !Repo::setLastError(git_revwalk_push_head(revWalk()));
72  }
74  bool pushRef(const std::string &ref) const noexcept {
75  return !Repo::setLastError(git_revwalk_push_ref(revWalk(), ref.c_str()));
76  }
78  bool pushGlob(const std::string &glob) const noexcept {
79  return !Repo::setLastError(git_revwalk_push_glob(revWalk(), glob.c_str()));
80  }
82  bool pushRange(const std::string &range) const noexcept {
83  return !Repo::setLastError(git_revwalk_push_range(revWalk(), range.c_str()));
84  }
85 
87  bool hide(const git_oid &oid) const noexcept {
88  return !Repo::setLastError(git_revwalk_hide(revWalk(), &oid));
89  }
91  bool hide(const std::string &id) const noexcept;
93  bool hideGlob(const std::string &glob) const noexcept {
94  return !Repo::setLastError(git_revwalk_hide_glob(revWalk(), glob.c_str()));
95  }
96 
98  bool sorting(unsigned int mode) const noexcept {
99  return !Repo::setLastError(git_revwalk_sorting(revWalk(), mode));
100  }
101 
103  std::optional<Commit> next() const noexcept;
104 
106  GitTy *revWalk() const noexcept { return m_revWalk.get(); }
108  operator GitTy *() const noexcept { return revWalk(); }
109 private:
110  explicit RevWalk(const Repo &repo, GitTy *revWalk) noexcept :
111  m_repo(repo), m_revWalk(revWalk) { }
112 
113  const Repo &m_repo;
114  Holder m_revWalk;
115 };
116 
120 class Signature {
121  using GitTy = git_signature;
123 public:
124  Signature() = delete;
125 
127  static std::optional<Signature> now(const std::string &name, const std::string &email);
128 
130  std::string name() const noexcept { return signature()->name; }
132  std::string email() const noexcept { return signature()->email; }
133 
135  GitTy *signature() const noexcept { return m_signature.get(); }
137  operator GitTy *() const noexcept { return signature(); }
138 private:
139  explicit Signature(GitTy *sig) noexcept : m_signature(sig) {}
140 
141  Holder m_signature;
142 };
143 
144 }
Definition: Blob.h:11
GitTy * ref() const noexcept
Get the stored pointer to libgit2&#39;s git_reference.
Definition: Misc.h:41
const git_oid * target() const noexcept
Get the target of a Reference.
Definition: Misc.h:27
std::optional< Commit > next() const noexcept
Get next Commit in this RevWalk, or nullopt if there are no more.
static std::optional< Signature > now(const std::string &name, const std::string &email)
Create a new Signature with name and email.
GitTy * signature() const noexcept
Get the stored pointer to libgit2&#39;s git_signature.
Definition: Misc.h:135
std::string name() const noexcept
Get name of this Reference.
Definition: Misc.h:35
The most important Git class.
Definition: Repo.h:45
GitTy * revWalk() const noexcept
Get the stored pointer to libgit2&#39;s git_revwalk.
Definition: Misc.h:106
bool pushHead() const noexcept
Add HEAD to this RevWalk.
Definition: Misc.h:70
bool hideGlob(const std::string &glob) const noexcept
Stop this RevWalk at references matching glob.
Definition: Misc.h:93
bool sorting(unsigned int mode) const noexcept
Set mode as sorting mode (GIT_SORT_NONE, GIT_SORT_TOPOLOGICAL, ...)
Definition: Misc.h:98
bool pushRange(const std::string &range) const noexcept
Add all references matching range (commit1..commit2)
Definition: Misc.h:82
std::string name() const noexcept
Get the name in this Signature.
Definition: Misc.h:130
bool pushGlob(const std::string &glob) const noexcept
Add all references matching glob.
Definition: Misc.h:78
std::string symbolicTarget() const noexcept
Get the target of a symbolic Reference.
Definition: Misc.h:31
std::optional< Reference > resolve() const noexcept
Resolve a symbolic reference to a direct reference.
bool hide(const git_oid &oid) const noexcept
Stop this RevWalk at oid.
Definition: Misc.h:87
std::string email() const noexcept
Get the email in this Signature.
Definition: Misc.h:132
git_reference_t type() const noexcept
Get type of this Reference (GIT_REFERENCE_DIRECT, GIT_REFERENCE_SYMBOLIC)
Definition: Misc.h:33
Reference is a representation of a git reference.
Definition: Misc.h:18
bool pushRef(const std::string &ref) const noexcept
Add a commit referenced by ref to this RevWalk.
Definition: Misc.h:74
const git_oid * targetPeel() const noexcept
Get the peeled target of a Reference.
Definition: Misc.h:29
RevWalk is a representation of a git revwalk.
Definition: Misc.h:55
bool push(const git_oid &oid) const noexcept
Add one OID to this RevWalk.
Definition: Misc.h:64
Signature is a representation of a git signature.
Definition: Misc.h:120