1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 module libgit2_d.pathspec;
8 
9 
10 private static import libgit2_d.diff;
11 private static import libgit2_d.strarray;
12 private static import libgit2_d.types;
13 
14 extern (C):
15 nothrow @nogc:
16 public:
17 
18 /**
19  * Compiled pathspec
20  */
21 struct git_pathspec;
22 
23 /**
24  * List of filenames matching a pathspec
25  */
26 struct git_pathspec_match_list;
27 
28 /**
29  * Options controlling how pathspec match should be executed
30  */
31 enum git_pathspec_flag_t
32 {
33 	GIT_PATHSPEC_DEFAULT = 0,
34 
35 	/**
36 	 * GIT_PATHSPEC_IGNORE_CASE forces match to ignore case; otherwise
37 	 * match will use native case sensitivity of platform filesystem
38 	 */
39 	GIT_PATHSPEC_IGNORE_CASE = 1u << 0,
40 
41 	/**
42 	 * GIT_PATHSPEC_USE_CASE forces case sensitive match; otherwise
43 	 * match will use native case sensitivity of platform filesystem
44 	 */
45 	GIT_PATHSPEC_USE_CASE = 1u << 1,
46 
47 	/**
48 	 * GIT_PATHSPEC_NO_GLOB disables glob patterns and just uses simple
49 	 * string comparison for matching
50 	 */
51 	GIT_PATHSPEC_NO_GLOB = 1u << 2,
52 
53 	/**
54 	 * GIT_PATHSPEC_NO_MATCH_ERROR means the match functions return error
55 	 * code GIT_ENOTFOUND if no matches are found; otherwise no matches is
56 	 * still success (return 0) but `git_pathspec_match_list_entrycount`
57 	 * will indicate 0 matches.
58 	 */
59 	GIT_PATHSPEC_NO_MATCH_ERROR = 1u << 3,
60 
61 	/**
62 	 * GIT_PATHSPEC_FIND_FAILURES means that the `git_pathspec_match_list`
63 	 * should track which patterns matched which files so that at the end of
64 	 * the match we can identify patterns that did not match any files.
65 	 */
66 	GIT_PATHSPEC_FIND_FAILURES = 1u << 4,
67 
68 	/**
69 	 * GIT_PATHSPEC_FAILURES_ONLY means that the `git_pathspec_match_list`
70 	 * does not need to keep the actual matching filenames.  Use this to
71 	 * just test if there were any matches at all or in combination with
72 	 * GIT_PATHSPEC_FIND_FAILURES to validate a pathspec.
73 	 */
74 	GIT_PATHSPEC_FAILURES_ONLY = 1u << 5,
75 }
76 
77 /**
78  * Compile a pathspec
79  *
80  * Params:
81  *      out_ = Output of the compiled pathspec
82  *      pathspec = A git_strarray of the paths to match
83  *
84  * Returns: 0 on success, <0 on failure
85  */
86 //GIT_EXTERN
87 int git_pathspec_new(.git_pathspec** out_, const (libgit2_d.strarray.git_strarray)* pathspec);
88 
89 /**
90  * Free a pathspec
91  *
92  * Params:
93  *      ps = The compiled pathspec
94  */
95 //GIT_EXTERN
96 void git_pathspec_free(.git_pathspec* ps);
97 
98 /**
99  * Try to match a path against a pathspec
100  *
101  * Unlike most of the other pathspec matching functions, this will not
102  * fall back on the native case-sensitivity for your platform.  You must
103  * explicitly pass flags to control case sensitivity or else this will
104  * fall back on being case sensitive.
105  *
106  * Params:
107  *      ps = The compiled pathspec
108  *      flags = Combination of git_pathspec_flag_t options to control match
109  *      path = The pathname to attempt to match
110  *
111  * Returns: 1 is path matches spec, 0 if it does not
112  */
113 //GIT_EXTERN
114 int git_pathspec_matches_path(const (.git_pathspec)* ps, uint flags, const (char)* path);
115 
116 /**
117  * Match a pathspec against the working directory of a repository.
118  *
119  * This matches the pathspec against the current files in the working
120  * directory of the repository.  It is an error to invoke this on a bare
121  * repo.  This handles git ignores (i.e. ignored files will not be
122  * considered to match the `pathspec` unless the file is tracked in the
123  * index).
124  *
125  * If `out` is not null, this returns a `git_patchspec_match_list`.  That
126  * contains the list of all matched filenames (unless you pass the
127  * `git_pathspec_flag_t.GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of
128  * pathspecs with no match (if you used the `git_pathspec_flag_t.GIT_PATHSPEC_FIND_FAILURES`
129  * flag).  You must call `git_pathspec_match_list_free()` on this object.
130  *
131  * Params:
132  *      out_ = Output list of matches; pass null to just get return value
133  *      repo = The repository in which to match; bare repo is an error
134  *      flags = Combination of git_pathspec_flag_t options to control match
135  *      ps = Pathspec to be matched
136  *
137  * Returns: 0 on success, -1 on error, git_error_code.GIT_ENOTFOUND if no matches and the git_pathspec_flag_t.GIT_PATHSPEC_NO_MATCH_ERROR flag was given
138  */
139 //GIT_EXTERN
140 int git_pathspec_match_workdir(.git_pathspec_match_list** out_, libgit2_d.types.git_repository* repo, uint flags, .git_pathspec* ps);
141 
142 /**
143  * Match a pathspec against entries in an index.
144  *
145  * This matches the pathspec against the files in the repository index.
146  *
147  * NOTE: At the moment, the case sensitivity of this match is controlled
148  * by the current case-sensitivity of the index object itself and the
149  * USE_CASE and IGNORE_CASE flags will have no effect.  This behavior will
150  * be corrected in a future release.
151  *
152  * If `out` is not null, this returns a `git_patchspec_match_list`.  That
153  * contains the list of all matched filenames (unless you pass the
154  * `git_pathspec_flag_t.GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of
155  * pathspecs with no match (if you used the `git_pathspec_flag_t.GIT_PATHSPEC_FIND_FAILURES`
156  * flag).  You must call `git_pathspec_match_list_free()` on this object.
157  *
158  * Params:
159  *      out_ = Output list of matches; pass null to just get return value
160  *      index = The index to match against
161  *      flags = Combination of git_pathspec_flag_t options to control match
162  *      ps = Pathspec to be matched
163  *
164  * Returns: 0 on success, -1 on error, git_error_code.GIT_ENOTFOUND if no matches and the git_pathspec_flag_t.GIT_PATHSPEC_NO_MATCH_ERROR flag is used
165  */
166 //GIT_EXTERN
167 int git_pathspec_match_index(.git_pathspec_match_list** out_, libgit2_d.types.git_index* index, uint flags, .git_pathspec* ps);
168 
169 /**
170  * Match a pathspec against files in a tree.
171  *
172  * This matches the pathspec against the files in the given tree.
173  *
174  * If `out` is not null, this returns a `git_patchspec_match_list`.  That
175  * contains the list of all matched filenames (unless you pass the
176  * `git_pathspec_flag_t.GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of
177  * pathspecs with no match (if you used the `git_pathspec_flag_t.GIT_PATHSPEC_FIND_FAILURES`
178  * flag).  You must call `git_pathspec_match_list_free()` on this object.
179  *
180  * Params:
181  *      out_ = Output list of matches; pass null to just get return value
182  *      tree = The root-level tree to match against
183  *      flags = Combination of git_pathspec_flag_t options to control match
184  *      ps = Pathspec to be matched
185  *
186  * Returns: 0 on success, -1 on error, git_error_code.GIT_ENOTFOUND if no matches and the git_pathspec_flag_t.GIT_PATHSPEC_NO_MATCH_ERROR flag is used
187  */
188 //GIT_EXTERN
189 int git_pathspec_match_tree(.git_pathspec_match_list** out_, libgit2_d.types.git_tree* tree, uint flags, .git_pathspec* ps);
190 
191 /**
192  * Match a pathspec against files in a diff list.
193  *
194  * This matches the pathspec against the files in the given diff list.
195  *
196  * If `out` is not null, this returns a `git_patchspec_match_list`.  That
197  * contains the list of all matched filenames (unless you pass the
198  * `git_pathspec_flag_t.GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of
199  * pathspecs with no match (if you used the `git_pathspec_flag_t.GIT_PATHSPEC_FIND_FAILURES`
200  * flag).  You must call `git_pathspec_match_list_free()` on this object.
201  *
202  * Params:
203  *      out_ = Output list of matches; pass null to just get return value
204  *      diff = A generated diff list
205  *      flags = Combination of git_pathspec_flag_t options to control match
206  *      ps = Pathspec to be matched
207  *
208  * Returns: 0 on success, -1 on error, git_error_code.GIT_ENOTFOUND if no matches and the git_pathspec_flag_t.GIT_PATHSPEC_NO_MATCH_ERROR flag is used
209  */
210 //GIT_EXTERN
211 int git_pathspec_match_diff(.git_pathspec_match_list** out_, libgit2_d.diff.git_diff* diff, uint flags, .git_pathspec* ps);
212 
213 /**
214  * Free memory associates with a git_pathspec_match_list
215  *
216  * Params:
217  *      m = The git_pathspec_match_list to be freed
218  */
219 //GIT_EXTERN
220 void git_pathspec_match_list_free(.git_pathspec_match_list* m);
221 
222 /**
223  * Get the number of items in a match list.
224  *
225  * Params:
226  *      m = The git_pathspec_match_list object
227  *
228  * Returns: Number of items in match list
229  */
230 //GIT_EXTERN
231 size_t git_pathspec_match_list_entrycount(const (.git_pathspec_match_list)* m);
232 
233 /**
234  * Get a matching filename by position.
235  *
236  * This routine cannot be used if the match list was generated by
237  * `git_pathspec_match_diff`.  If so, it will always return null.
238  *
239  * Params:
240  *      m = The git_pathspec_match_list object
241  *      pos = The index into the list
242  *
243  * Returns: The filename of the match
244  */
245 //GIT_EXTERN
246 const (char)* git_pathspec_match_list_entry(const (.git_pathspec_match_list)* m, size_t pos);
247 
248 /**
249  * Get a matching diff delta by position.
250  *
251  * This routine can only be used if the match list was generated by
252  * `git_pathspec_match_diff`.  Otherwise it will always return null.
253  *
254  * Params:
255  *      m = The git_pathspec_match_list object
256  *      pos = The index into the list
257  *
258  * Returns: The filename of the match
259  */
260 //GIT_EXTERN
261 const (libgit2_d.diff.git_diff_delta)* git_pathspec_match_list_diff_entry(const (.git_pathspec_match_list)* m, size_t pos);
262 
263 /**
264  * Get the number of pathspec items that did not match.
265  *
266  * This will be zero unless you passed git_pathspec_flag_t.GIT_PATHSPEC_FIND_FAILURES when
267  * generating the git_pathspec_match_list.
268  *
269  * Params:
270  *      m = The git_pathspec_match_list object
271  *
272  * Returns: Number of items in original pathspec that had no matches
273  */
274 //GIT_EXTERN
275 size_t git_pathspec_match_list_failed_entrycount(const (.git_pathspec_match_list)* m);
276 
277 /**
278  * Get an original pathspec string that had no matches.
279  *
280  * This will be return null for positions out of range.
281  *
282  * Params:
283  *      m = The git_pathspec_match_list object
284  *      pos = The index into the failed items
285  *
286  * Returns: The pathspec pattern that didn't match anything
287  */
288 //GIT_EXTERN
289 const (char)* git_pathspec_match_list_failed_entry(const (.git_pathspec_match_list)* m, size_t pos);