SlHelpers
Loading...
Searching...
No Matches
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
13namespace SlGit {
14
15class Diff;
16class Index;
17class Repo;
18class Tree;
19
23class PathSpecMatchList {
24 using GitTy = git_pathspec_match_list;
25 using Holder = SlHelpers::UniqueHolder<GitTy>;
26
27 friend class Repo;
28 friend class PathSpec;
29public:
30 PathSpecMatchList() = delete;
31
33 size_t entrycount() const noexcept {
34 return git_pathspec_match_list_entrycount(matchList());
35 }
36
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 }
50
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(); }
59private:
60 explicit PathSpecMatchList(GitTy *matchList) noexcept : m_matchList(matchList) { }
61
62 Holder m_matchList;
63};
64
68class PathSpec {
69 using GitTy = git_pathspec;
70 using Holder = SlHelpers::UniqueHolder<GitTy>;
71
72 friend class Repo;
73public:
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(); }
102private:
103 explicit PathSpec(GitTy *pathSpec) noexcept : m_pathSpec(pathSpec) { }
104
105 Holder m_pathSpec;
106};
107
108}
Diff is a representation of a git diff.
Definition Diff.h:21
Index is a representation of a git index.
Definition Index.h:78
PathSpecMatchList is a representation of a git pathspecs match list.
Definition PathSpec.h:23
std::string entry(size_t pos) const noexcept
Get an entry on pos-th position.
Definition PathSpec.h:37
size_t entrycount() const noexcept
Get count of entries in this PathSpecMatchList.
Definition PathSpec.h:33
const git_diff_delta * diffEntry(size_t pos) const noexcept
Get a diff entry on pos-th position.
Definition PathSpec.h:42
GitTy * matchList() const noexcept
Get the stored pointer to libgit2's git_pathspec_match_list.
Definition PathSpec.h:56
size_t failedEntrycount() const noexcept
Get count of failed entries in this PathSpecMatchList.
Definition PathSpec.h:47
std::string failedEntry(size_t pos) const noexcept
Get a failed entry on pos-th position.
Definition PathSpec.h:51
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::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 > matchWorkdir(const Repo &repo, uint32_t flags=GIT_PATHSPEC_DEFAULT) const noexcept
Return paths this PathSpec matches in the workdir.
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
std::optional< PathSpecMatchList > matchTree(const Tree &tree, uint32_t flags=GIT_PATHSPEC_DEFAULT) const noexcept
Return paths this PathSpec matches in the tree.
std::optional< PathSpecMatchList > matchDiff(const Diff &diff, uint32_t flags=GIT_PATHSPEC_DEFAULT) const noexcept
Return paths this PathSpec matches in the diff.
GitTy * pathSpec() const noexcept
Get the stored pointer to libgit2's git_pathspec.
Definition PathSpec.h:99
The most important Git class.
Definition Repo.h:45
Tree is a representation of a git tree.
Definition Tree.h:27
Wrapper around std::unique_ptr for easier re-definition of free.
Definition Unique.h:41