SlHelpers
PathSpec.h
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #pragma once
4 
5 #include <optional>
6 #include <string>
7 #include <vector>
8 
9 #include <git2.h>
10 
11 #include "../helpers/Unique.h"
12 
13 namespace SlGit {
14 
15 class Diff;
16 class Index;
17 class Repo;
18 class Tree;
19 
24  using GitTy = git_pathspec_match_list;
26 
27  friend class Repo;
28  friend class PathSpec;
29 public:
30  PathSpecMatchList() = delete;
31 
33  size_t entrycount() const noexcept {
34  return git_pathspec_match_list_entrycount(matchList());
35  }
37  std::string entry(size_t pos) const noexcept {
38  return git_pathspec_match_list_entry(matchList(), pos);
39  }
40 
42  const git_diff_delta *diffEntry(size_t pos) const noexcept {
43  return git_pathspec_match_list_diff_entry(matchList(), pos);
44  }
45 
47  size_t failedEntrycount() const noexcept {
48  return git_pathspec_match_list_failed_entrycount(matchList());
49  }
51  std::string failedEntry(size_t pos) const noexcept {
52  return git_pathspec_match_list_failed_entry(matchList(), pos);
53  }
54 
56  GitTy *matchList() const noexcept { return m_matchList.get(); }
58  operator GitTy *() const noexcept { return matchList(); }
59 private:
60  explicit PathSpecMatchList(GitTy *matchList) noexcept : m_matchList(matchList) { }
61 
62  Holder m_matchList;
63 };
64 
68 class PathSpec {
69  using GitTy = git_pathspec;
71 
72  friend class Repo;
73 public:
74  PathSpec() = delete;
75 
77  static std::optional<PathSpec> create(const std::vector<std::string> &pathSpec) noexcept;
78 
80  bool matchesPath(const std::string &path,
81  uint32_t flags = GIT_PATHSPEC_DEFAULT) const noexcept {
82  return git_pathspec_matches_path(m_pathSpec, flags, path.c_str());
83  }
84 
86  std::optional<PathSpecMatchList> matchWorkdir(const Repo &repo,
87  uint32_t flags = GIT_PATHSPEC_DEFAULT) const noexcept;
89  std::optional<PathSpecMatchList> matchIndex(const Index &index,
90  uint32_t flags = GIT_PATHSPEC_DEFAULT) const noexcept;
92  std::optional<PathSpecMatchList> matchTree(const Tree &tree,
93  uint32_t flags = GIT_PATHSPEC_DEFAULT) const noexcept;
95  std::optional<PathSpecMatchList> matchDiff(const Diff &diff,
96  uint32_t flags = GIT_PATHSPEC_DEFAULT) const noexcept;
97 
99  GitTy *pathSpec() const noexcept { return m_pathSpec.get(); }
101  operator GitTy *() const noexcept { return pathSpec(); }
102 private:
103  explicit PathSpec(GitTy *pathSpec) noexcept : m_pathSpec(pathSpec) { }
104 
105  Holder m_pathSpec;
106 };
107 
108 }
std::optional< PathSpecMatchList > matchTree(const Tree &tree, uint32_t flags=GIT_PATHSPEC_DEFAULT) const noexcept
Return paths this PathSpec matches in the tree.
Definition: Blob.h:11
size_t entrycount() const noexcept
Get count of entries in this PathSpecMatchList.
Definition: PathSpec.h:33
Index is a representation of a git index.
Definition: Index.h:78
size_t failedEntrycount() const noexcept
Get count of failed entries in this PathSpecMatchList.
Definition: PathSpec.h:47
PathSpec is a representation of git pathspecs.
Definition: PathSpec.h:68
static std::optional< PathSpec > create(const std::vector< std::string > &pathSpec) noexcept
Create a new PathSpec, to match every path in pathSpec.
std::string entry(size_t pos) const noexcept
Get an entry on pos-th position.
Definition: PathSpec.h:37
GitTy * pathSpec() const noexcept
Get the stored pointer to libgit2&#39;s git_pathspec.
Definition: PathSpec.h:99
The most important Git class.
Definition: Repo.h:45
const git_diff_delta * diffEntry(size_t pos) const noexcept
Get a diff entry on pos-th position.
Definition: PathSpec.h:42
std::string failedEntry(size_t pos) const noexcept
Get a failed entry on pos-th position.
Definition: PathSpec.h:51
Tree is a representation of a git tree.
Definition: Tree.h:27
bool matchesPath(const std::string &path, uint32_t flags=GIT_PATHSPEC_DEFAULT) const noexcept
Return true if this PathSpec matches path.
Definition: PathSpec.h:80
GitTy * matchList() const noexcept
Get the stored pointer to libgit2&#39;s git_pathspec_match_list.
Definition: PathSpec.h:56
std::optional< PathSpecMatchList > matchIndex(const Index &index, uint32_t flags=GIT_PATHSPEC_DEFAULT) const noexcept
Return paths this PathSpec matches in the index.
std::optional< PathSpecMatchList > matchDiff(const Diff &diff, uint32_t flags=GIT_PATHSPEC_DEFAULT) const noexcept
Return paths this PathSpec matches in the diff.
Diff is a representation of a git diff.
Definition: Diff.h:21
PathSpecMatchList is a representation of a git pathspecs match list.
Definition: PathSpec.h:23
std::optional< PathSpecMatchList > matchWorkdir(const Repo &repo, uint32_t flags=GIT_PATHSPEC_DEFAULT) const noexcept
Return paths this PathSpec matches in the workdir.