SlHelpers
Loading...
Searching...
No Matches
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
13namespace SlGit {
14
18class Reference {
19 using GitTy = git_reference;
20 using Holder = SlHelpers::UniqueHolder<GitTy>;
21
22 friend class Repo;
23public:
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(); }
44private:
45 explicit Reference(GitTy *ref) noexcept : m_ref(ref) { }
46
47 Holder m_ref;
48};
49
50class Commit;
51
55class RevWalk {
56 using GitTy = git_revwalk;
57 using Holder = SlHelpers::UniqueHolder<GitTy>;
58
59 friend class Repo;
60public:
61 RevWalk() = delete;
62
64 bool push(const git_oid &oid) const noexcept {
65 return !Repo::setLastError(git_revwalk_push(revWalk(), &oid));
66 }
67
68 bool push(const std::string &id) const noexcept;
70 bool pushHead() const noexcept {
71 return !Repo::setLastError(git_revwalk_push_head(revWalk()));
72 }
73
74 bool pushRef(const std::string &ref) const noexcept {
75 return !Repo::setLastError(git_revwalk_push_ref(revWalk(), ref.c_str()));
76 }
77
78 bool pushGlob(const std::string &glob) const noexcept {
79 return !Repo::setLastError(git_revwalk_push_glob(revWalk(), glob.c_str()));
80 }
81
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 }
90
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(); }
109private:
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
120class Signature {
121 using GitTy = git_signature;
122 using Holder = SlHelpers::UniqueHolder<GitTy>;
123public:
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(); }
138private:
139 explicit Signature(GitTy *sig) noexcept : m_signature(sig) {}
140
141 Holder m_signature;
142};
143
144}
Commit is a representation of a git commit.
Definition Commit.h:21
Reference is a representation of a git reference.
Definition Misc.h:18
std::string symbolicTarget() const noexcept
Get the target of a symbolic Reference.
Definition Misc.h:31
const git_oid * target() const noexcept
Get the target of a Reference.
Definition Misc.h:27
const git_oid * targetPeel() const noexcept
Get the peeled target of a Reference.
Definition Misc.h:29
std::string name() const noexcept
Get name of this Reference.
Definition Misc.h:35
std::optional< Reference > resolve() const noexcept
Resolve a symbolic reference to a direct reference.
git_reference_t type() const noexcept
Get type of this Reference (GIT_REFERENCE_DIRECT, GIT_REFERENCE_SYMBOLIC).
Definition Misc.h:33
GitTy * ref() const noexcept
Get the stored pointer to libgit2's git_reference.
Definition Misc.h:41
The most important Git class.
Definition Repo.h:45
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
bool pushRef(const std::string &ref) const noexcept
Add a commit referenced by ref to this RevWalk.
Definition Misc.h:74
GitTy * revWalk() const noexcept
Get the stored pointer to libgit2's git_revwalk.
Definition Misc.h:106
bool hideGlob(const std::string &glob) const noexcept
Stop this RevWalk at references matching glob.
Definition Misc.h:93
bool pushHead() const noexcept
Add HEAD to this RevWalk.
Definition Misc.h:70
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
bool pushGlob(const std::string &glob) const noexcept
Add all references matching glob.
Definition Misc.h:78
std::optional< Commit > next() const noexcept
Get next Commit in this RevWalk, or nullopt if there are no more.
bool push(const std::string &id) const noexcept
Add one commit SHA to this RevWalk.
bool hide(const git_oid &oid) const noexcept
Stop this RevWalk at oid.
Definition Misc.h:87
bool hide(const std::string &id) const noexcept
Stop this RevWalk at commit id.
Signature is a representation of a git signature.
Definition Misc.h:120
std::string email() const noexcept
Get the email in this Signature.
Definition Misc.h:132
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's git_signature.
Definition Misc.h:135
std::string name() const noexcept
Get the name in this Signature.
Definition Misc.h:130
Wrapper around std::unique_ptr for easier re-definition of free.
Definition Unique.h:41