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  * @param out_ Output of the compiled pathspec
81  * @param pathspec A git_strarray of the paths to match
82  * @return 0 on success, <0 on failure
83  */
84 //GIT_EXTERN
85 int git_pathspec_new(.git_pathspec** out_, const (libgit2_d.strarray.git_strarray)* pathspec);
86 
87 /**
88  * Free a pathspec
89  *
90  * @param ps The compiled pathspec
91  */
92 //GIT_EXTERN
93 void git_pathspec_free(.git_pathspec* ps);
94 
95 /**
96  * Try to match a path against a pathspec
97  *
98  * Unlike most of the other pathspec matching functions, this will not
99  * fall back on the native case-sensitivity for your platform.  You must
100  * explicitly pass flags to control case sensitivity or else this will
101  * fall back on being case sensitive.
102  *
103  * @param ps The compiled pathspec
104  * @param flags Combination of git_pathspec_flag_t options to control match
105  * @param path The pathname to attempt to match
106  * @return 1 is path matches spec, 0 if it does not
107  */
108 //GIT_EXTERN
109 int git_pathspec_matches_path(const (.git_pathspec)* ps, uint flags, const (char)* path);
110 
111 /**
112  * Match a pathspec against the working directory of a repository.
113  *
114  * This matches the pathspec against the current files in the working
115  * directory of the repository.  It is an error to invoke this on a bare
116  * repo.  This handles git ignores (i.e. ignored files will not be
117  * considered to match the `pathspec` unless the file is tracked in the
118  * index).
119  *
120  * If `out` is not null, this returns a `git_patchspec_match_list`.  That
121  * contains the list of all matched filenames (unless you pass the
122  * `git_pathspec_flag_t.GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of
123  * pathspecs with no match (if you used the `git_pathspec_flag_t.GIT_PATHSPEC_FIND_FAILURES`
124  * flag).  You must call `git_pathspec_match_list_free()` on this object.
125  *
126  * @param out_ Output list of matches; pass null to just get return value
127  * @param repo The repository in which to match; bare repo is an error
128  * @param flags Combination of git_pathspec_flag_t options to control match
129  * @param ps Pathspec to be matched
130  * @return 0 on success, -1 on error, git_error_code.GIT_ENOTFOUND if no matches and
131  *         the git_pathspec_flag_t.GIT_PATHSPEC_NO_MATCH_ERROR flag was given
132  */
133 //GIT_EXTERN
134 int git_pathspec_match_workdir(.git_pathspec_match_list** out_, libgit2_d.types.git_repository* repo, uint flags, .git_pathspec* ps);
135 
136 /**
137  * Match a pathspec against entries in an index.
138  *
139  * This matches the pathspec against the files in the repository index.
140  *
141  * NOTE: At the moment, the case sensitivity of this match is controlled
142  * by the current case-sensitivity of the index object itself and the
143  * USE_CASE and IGNORE_CASE flags will have no effect.  This behavior will
144  * be corrected in a future release.
145  *
146  * If `out` is not null, this returns a `git_patchspec_match_list`.  That
147  * contains the list of all matched filenames (unless you pass the
148  * `git_pathspec_flag_t.GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of
149  * pathspecs with no match (if you used the `git_pathspec_flag_t.GIT_PATHSPEC_FIND_FAILURES`
150  * flag).  You must call `git_pathspec_match_list_free()` on this object.
151  *
152  * @param out_ Output list of matches; pass null to just get return value
153  * @param index The index to match against
154  * @param flags Combination of git_pathspec_flag_t options to control match
155  * @param ps Pathspec to be matched
156  * @return 0 on success, -1 on error, git_error_code.GIT_ENOTFOUND if no matches and
157  *         the git_pathspec_flag_t.GIT_PATHSPEC_NO_MATCH_ERROR flag is used
158  */
159 //GIT_EXTERN
160 int git_pathspec_match_index(.git_pathspec_match_list** out_, libgit2_d.types.git_index* index, uint flags, .git_pathspec* ps);
161 
162 /**
163  * Match a pathspec against files in a tree.
164  *
165  * This matches the pathspec against the files in the given tree.
166  *
167  * If `out` is not null, this returns a `git_patchspec_match_list`.  That
168  * contains the list of all matched filenames (unless you pass the
169  * `git_pathspec_flag_t.GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of
170  * pathspecs with no match (if you used the `git_pathspec_flag_t.GIT_PATHSPEC_FIND_FAILURES`
171  * flag).  You must call `git_pathspec_match_list_free()` on this object.
172  *
173  * @param out_ Output list of matches; pass null to just get return value
174  * @param tree The root-level tree to match against
175  * @param flags Combination of git_pathspec_flag_t options to control match
176  * @param ps Pathspec to be matched
177  * @return 0 on success, -1 on error, git_error_code.GIT_ENOTFOUND if no matches and
178  *         the git_pathspec_flag_t.GIT_PATHSPEC_NO_MATCH_ERROR flag is used
179  */
180 //GIT_EXTERN
181 int git_pathspec_match_tree(.git_pathspec_match_list** out_, libgit2_d.types.git_tree* tree, uint flags, .git_pathspec* ps);
182 
183 /**
184  * Match a pathspec against files in a diff list.
185  *
186  * This matches the pathspec against the files in the given diff list.
187  *
188  * If `out` is not null, this returns a `git_patchspec_match_list`.  That
189  * contains the list of all matched filenames (unless you pass the
190  * `git_pathspec_flag_t.GIT_PATHSPEC_FAILURES_ONLY` flag) and may also contain the list of
191  * pathspecs with no match (if you used the `git_pathspec_flag_t.GIT_PATHSPEC_FIND_FAILURES`
192  * flag).  You must call `git_pathspec_match_list_free()` on this object.
193  *
194  * @param out_ Output list of matches; pass null to just get return value
195  * @param diff A generated diff list
196  * @param flags Combination of git_pathspec_flag_t options to control match
197  * @param ps Pathspec to be matched
198  * @return 0 on success, -1 on error, git_error_code.GIT_ENOTFOUND if no matches and
199  *         the git_pathspec_flag_t.GIT_PATHSPEC_NO_MATCH_ERROR flag is used
200  */
201 //GIT_EXTERN
202 int git_pathspec_match_diff(.git_pathspec_match_list** out_, libgit2_d.diff.git_diff* diff, uint flags, .git_pathspec* ps);
203 
204 /**
205  * Free memory associates with a git_pathspec_match_list
206  *
207  * @param m The git_pathspec_match_list to be freed
208  */
209 //GIT_EXTERN
210 void git_pathspec_match_list_free(.git_pathspec_match_list* m);
211 
212 /**
213  * Get the number of items in a match list.
214  *
215  * @param m The git_pathspec_match_list object
216  * @return Number of items in match list
217  */
218 //GIT_EXTERN
219 size_t git_pathspec_match_list_entrycount(const (.git_pathspec_match_list)* m);
220 
221 /**
222  * Get a matching filename by position.
223  *
224  * This routine cannot be used if the match list was generated by
225  * `git_pathspec_match_diff`.  If so, it will always return null.
226  *
227  * @param m The git_pathspec_match_list object
228  * @param pos The index into the list
229  * @return The filename of the match
230  */
231 //GIT_EXTERN
232 const (char)* git_pathspec_match_list_entry(const (.git_pathspec_match_list)* m, size_t pos);
233 
234 /**
235  * Get a matching diff delta by position.
236  *
237  * This routine can only be used if the match list was generated by
238  * `git_pathspec_match_diff`.  Otherwise it will always return null.
239  *
240  * @param m The git_pathspec_match_list object
241  * @param pos The index into the list
242  * @return The filename of the match
243  */
244 //GIT_EXTERN
245 const (libgit2_d.diff.git_diff_delta)* git_pathspec_match_list_diff_entry(const (.git_pathspec_match_list)* m, size_t pos);
246 
247 /**
248  * Get the number of pathspec items that did not match.
249  *
250  * This will be zero unless you passed git_pathspec_flag_t.GIT_PATHSPEC_FIND_FAILURES when
251  * generating the git_pathspec_match_list.
252  *
253  * @param m The git_pathspec_match_list object
254  * @return Number of items in original pathspec that had no matches
255  */
256 //GIT_EXTERN
257 size_t git_pathspec_match_list_failed_entrycount(const (.git_pathspec_match_list)* m);
258 
259 /**
260  * Get an original pathspec string that had no matches.
261  *
262  * This will be return null for positions out of range.
263  *
264  * @param m The git_pathspec_match_list object
265  * @param pos The index into the failed items
266  * @return The pathspec pattern that didn't match anything
267  */
268 //GIT_EXTERN
269 const (char)* git_pathspec_match_list_failed_entry(const (.git_pathspec_match_list)* m, size_t pos);