13 #include "../helpers/Unique.h" 24 using GitTy = git_index_iterator;
42 return m_current == other.m_current;
46 return !(*
this == other);
49 const git_index_entry &
operator *() const noexcept {
return *m_current; }
51 const git_index_entry *
operator ->() const noexcept {
return m_current; }
54 std::string
path() const noexcept {
return m_current->path; }
56 std::string_view
pathSV() const noexcept {
return m_current->path; }
59 GitTy *
iterator() const noexcept {
return m_iterator.get(); }
61 operator GitTy *()
const noexcept {
return iterator(); }
64 explicit IndexIterator(GitTy *
iterator) noexcept : m_iterator(
iterator) { increment(); }
67 if (git_index_iterator_next(&m_current,
iterator()))
72 const git_index_entry *m_current;
79 using GitTy = git_index;
85 using MatchCB = std::function<int(const std::filesystem::path &, const std::string &)>;
90 static std::optional<Index>
open(
const std::filesystem::path &path) noexcept;
92 static std::optional<Index>
create() noexcept;
95 bool read(
bool force = true) const noexcept {
96 return !Repo::setLastError(git_index_read(
index(), force));
99 bool write() const noexcept {
return !Repo::setLastError(git_index_write(
index())); }
103 std::optional<Tree>
writeTree(
const Repo &repo)
const noexcept;
109 return git_index_get_byindex(
index(), idx);
112 const git_index_entry *
entryByPath(
const std::filesystem::path &path,
113 git_index_stage_t stage = GIT_INDEX_STAGE_NORMAL)
const noexcept {
114 return git_index_get_bypath(
index(), path.c_str(), stage);
118 bool addByPath(
const std::filesystem::path &path)
const noexcept {
119 return !Repo::setLastError(git_index_add_bypath(
index(), path.c_str()));
123 return !Repo::setLastError(git_index_remove_bypath(
index(), path.c_str()));
133 int addAll(
const std::vector<std::string> &paths,
unsigned int flags,
134 const MatchCB *cb =
nullptr)
const;
141 int removeAll(
const std::vector<std::string> &paths,
const MatchCB *cb =
nullptr)
const;
148 int updateAll(
const std::vector<std::string> &paths,
const MatchCB *cb =
nullptr)
const;
164 GitTy *
index() const noexcept {
return m_index.get(); }
166 operator GitTy *()
const noexcept {
return index(); }
170 static int matchCB(
const char *path,
const char *matched_pathspec,
void *payload);
Iterator returned from Index::begin()
Definition: Index.h:23
size_t entrycount() const noexcept
Get count of entries in this Index.
Definition: Index.h:106
IndexIterator cend() const noexcept
Get the end iterator for this Index.
Definition: Index.h:161
GitTy * index() const noexcept
Get the stored pointer to libgit2's git_index.
Definition: Index.h:164
Index is a representation of a git index.
Definition: Index.h:78
bool operator!=(const IndexIterator &other) const noexcept
Compare with other.
Definition: Index.h:45
bool operator==(const IndexIterator &other) const noexcept
Compare with other.
Definition: Index.h:41
static std::optional< Index > open(const std::filesystem::path &path) noexcept
Load an index at into a new Index.
static std::optional< Index > create() noexcept
Create a new Index.
bool write() const noexcept
Write this Index to disk.
Definition: Index.h:99
std::string path() const noexcept
Get path from this IndexIterator (as a string)
Definition: Index.h:54
const git_index_entry * entryByPath(const std::filesystem::path &path, git_index_stage_t stage=GIT_INDEX_STAGE_NORMAL) const noexcept
Get entry corresponding to path (and stage)
Definition: Index.h:112
int addAll(const std::vector< std::string > &paths, unsigned int flags, const MatchCB *cb=nullptr) const
Add all paths into this Index.
IndexIterator cbegin() const noexcept
Get the begin iterator for this Index.
const git_index_entry * entryByIndex(size_t idx) const noexcept
Get an entry on the idx-th position.
Definition: Index.h:108
The most important Git class.
Definition: Repo.h:45
const git_index_entry * operator->() const noexcept
Get entry from this IndexIterator.
Definition: Index.h:51
const git_index_entry & operator*() const noexcept
Get entry from this IndexIterator.
Definition: Index.h:49
std::string_view pathSV() const noexcept
Get path from this IndexIterator (as a string_view)
Definition: Index.h:56
Tree is a representation of a git tree.
Definition: Tree.h:27
bool hasConflicts() const noexcept
Returns true if this index has some conflicts.
Definition: Index.h:151
IndexIterator end() noexcept
Get the end iterator for this Index.
Definition: Index.h:156
IndexIterator & operator++()
Move to the next entry.
Definition: Index.h:31
bool removeByPath(const std::filesystem::path &path) const noexcept
Remove path from this Index.
Definition: Index.h:122
bool readTree(const Tree &tree) const noexcept
Read tree into this Index.
bool read(bool force=true) const noexcept
Read the index from the disk into this Index.
Definition: Index.h:95
bool addByPath(const std::filesystem::path &path) const noexcept
Add path to this Index.
Definition: Index.h:118
int updateAll(const std::vector< std::string > &paths, const MatchCB *cb=nullptr) const
Update all paths in this Index.
std::function< int(const std::filesystem::path &, const std::string &)> MatchCB
A callback for addAll(), removeAll(), updateAll()
Definition: Index.h:85
int removeAll(const std::vector< std::string > &paths, const MatchCB *cb=nullptr) const
Remove all paths from this Index.
GitTy * iterator() const noexcept
Get the stored pointer to libgit2's git_index_entry.
Definition: Index.h:59
IndexIterator begin() noexcept
Get the begin iterator for this Index.
Definition: Index.h:154
std::optional< Tree > writeTree(const Repo &repo) const noexcept
Write this Index as a Tree.