Commit 2e3d4b96c08f1b0e2ee9b248c53aec523d70fd25

Russell Belfer 2012-11-08T16:47:28

Move pathspec code in separate files Diff uses a `git_strarray` of path specs to represent a subset of all files to be processed. It is useful to be able to reuse this filtering in other places outside diff, so I've moved it into a standalone set of utilities.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
diff --git a/src/diff.c b/src/diff.c
index 55f6ee7..f3dfdd9 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -10,76 +10,7 @@
 #include "config.h"
 #include "attr_file.h"
 #include "filter.h"
-
-static char *diff_prefix_from_pathspec(const git_strarray *pathspec)
-{
-	git_buf prefix = GIT_BUF_INIT;
-	const char *scan;
-
-	if (git_buf_common_prefix(&prefix, pathspec) < 0)
-		return NULL;
-
-	/* diff prefix will only be leading non-wildcards */
-	for (scan = prefix.ptr; *scan; ++scan) {
-		if (git__iswildcard(*scan) &&
-			(scan == prefix.ptr || (*(scan - 1) != '\\')))
-			break;
-	}
-	git_buf_truncate(&prefix, scan - prefix.ptr);
-
-	if (prefix.size <= 0) {
-		git_buf_free(&prefix);
-		return NULL;
-	}
-
-	git_buf_unescape(&prefix);
-
-	return git_buf_detach(&prefix);
-}
-
-static bool diff_pathspec_is_interesting(const git_strarray *pathspec)
-{
-	const char *str;
-
-	if (pathspec == NULL || pathspec->count == 0)
-		return false;
-	if (pathspec->count > 1)
-		return true;
-
-	str = pathspec->strings[0];
-	if (!str || !str[0] || (!str[1] && (str[0] == '*' || str[0] == '.')))
-		return false;
-	return true;
-}
-
-static bool diff_path_matches_pathspec(git_diff_list *diff, const char *path)
-{
-	unsigned int i;
-	git_attr_fnmatch *match;
-
-	if (!diff->pathspec.length)
-		return true;
-
-	git_vector_foreach(&diff->pathspec, i, match) {
-		int result = strcmp(match->pattern, path) ? FNM_NOMATCH : 0;
-
-		if (((diff->opts.flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH) == 0) &&
-			result == FNM_NOMATCH)
-			result = p_fnmatch(match->pattern, path, 0);
-
-		/* if we didn't match, look for exact dirname prefix match */
-		if (result == FNM_NOMATCH &&
-			(match->flags & GIT_ATTR_FNMATCH_HASWILD) == 0 &&
-			strncmp(path, match->pattern, match->length) == 0 &&
-			path[match->length] == '/')
-			result = 0;
-
-		if (result == 0)
-			return (match->flags & GIT_ATTR_FNMATCH_NEGATIVE) ? false : true;
-	}
-
-	return false;
-}
+#include "pathspec.h"
 
 static git_diff_delta *diff_delta__alloc(
 	git_diff_list *diff,
@@ -125,7 +56,10 @@ static int diff_delta__from_one(
 		(diff->opts.flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0)
 		return 0;
 
-	if (!diff_path_matches_pathspec(diff, entry->path))
+	if (!git_pathspec_match_path(
+			&diff->pathspec, entry->path,
+			(diff->opts.flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH) != 0,
+			(diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0))
 		return 0;
 
 	delta = diff_delta__alloc(diff, status, entry->path);
@@ -295,7 +229,6 @@ static git_diff_list *git_diff_list_alloc(
 	git_repository *repo, const git_diff_options *opts)
 {
 	git_config *cfg;
-	size_t i;
 	git_diff_list *diff = git__calloc(1, sizeof(git_diff_list));
 	if (diff == NULL)
 		return NULL;
@@ -333,7 +266,10 @@ static git_diff_list *git_diff_list_alloc(
 		return diff;
 
 	memcpy(&diff->opts, opts, sizeof(git_diff_options));
-	memset(&diff->opts.pathspec, 0, sizeof(diff->opts.pathspec));
+
+	/* pathspec init will do nothing for empty pathspec */
+	if (git_pathspec_init(&diff->pathspec, &opts->pathspec, &diff->pool) < 0)
+		goto fail;
 
 	/* TODO: handle config diff.mnemonicprefix, diff.noprefix */
 
@@ -355,35 +291,6 @@ static git_diff_list *git_diff_list_alloc(
 	if (diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE_TREES)
 		diff->opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
 
-	/* only copy pathspec if it is "interesting" so we can test
-	 * diff->pathspec.length > 0 to know if it is worth calling
-	 * fnmatch as we iterate.
-	 */
-	if (!diff_pathspec_is_interesting(&opts->pathspec))
-		return diff;
-
-	if (git_vector_init(
-		&diff->pathspec, (unsigned int)opts->pathspec.count, NULL) < 0)
-		goto fail;
-
-	for (i = 0; i < opts->pathspec.count; ++i) {
-		int ret;
-		const char *pattern = opts->pathspec.strings[i];
-		git_attr_fnmatch *match = git__calloc(1, sizeof(git_attr_fnmatch));
-		if (!match)
-			goto fail;
-		match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE;
-		ret = git_attr_fnmatch__parse(match, &diff->pool, NULL, &pattern);
-		if (ret == GIT_ENOTFOUND) {
-			git__free(match);
-			continue;
-		} else if (ret < 0)
-			goto fail;
-
-		if (git_vector_insert(&diff->pathspec, match) < 0)
-			goto fail;
-	}
-
 	return diff;
 
 fail:
@@ -394,7 +301,6 @@ fail:
 static void diff_list_free(git_diff_list *diff)
 {
 	git_diff_delta *delta;
-	git_attr_fnmatch *match;
 	unsigned int i;
 
 	git_vector_foreach(&diff->deltas, i, delta) {
@@ -403,12 +309,7 @@ static void diff_list_free(git_diff_list *diff)
 	}
 	git_vector_free(&diff->deltas);
 
-	git_vector_foreach(&diff->pathspec, i, match) {
-		git__free(match);
-		diff->pathspec.contents[i] = NULL;
-	}
-	git_vector_free(&diff->pathspec);
-
+	git_pathspec_free(&diff->pathspec);
 	git_pool_clear(&diff->pool);
 	git__free(diff);
 }
@@ -499,7 +400,10 @@ static int maybe_modified(
 
 	GIT_UNUSED(old_iter);
 
-	if (!diff_path_matches_pathspec(diff, oitem->path))
+	if (!git_pathspec_match_path(
+			&diff->pathspec, oitem->path,
+			(diff->opts.flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH) != 0,
+			(diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0))
 		return 0;
 
 	/* on platforms with no symlinks, preserve mode of existing symlinks */
@@ -842,15 +746,15 @@ int git_diff_tree_to_tree(
 	git_diff_list **diff)
 {
 	git_iterator *a = NULL, *b = NULL;
-	char *prefix = opts ? diff_prefix_from_pathspec(&opts->pathspec) : NULL;
+	char *pfx = opts ? git_pathspec_prefix(&opts->pathspec) : NULL;
 
 	assert(repo && old_tree && new_tree && diff);
 
-	if (git_iterator_for_tree_range(&a, repo, old_tree, prefix, prefix) < 0 ||
-		git_iterator_for_tree_range(&b, repo, new_tree, prefix, prefix) < 0)
+	if (git_iterator_for_tree_range(&a, repo, old_tree, pfx, pfx) < 0 ||
+		git_iterator_for_tree_range(&b, repo, new_tree, pfx, pfx) < 0)
 		return -1;
 
-	git__free(prefix);
+	git__free(pfx);
 
 	return diff_from_iterators(repo, opts, a, b, diff);
 }
@@ -862,20 +766,20 @@ int git_diff_index_to_tree(
 	git_diff_list **diff)
 {
 	git_iterator *a = NULL, *b = NULL;
-	char *prefix = opts ? diff_prefix_from_pathspec(&opts->pathspec) : NULL;
+	char *pfx = opts ? git_pathspec_prefix(&opts->pathspec) : NULL;
 
 	assert(repo && diff);
 
-	if (git_iterator_for_tree_range(&a, repo, old_tree, prefix, prefix) < 0 ||
-	    git_iterator_for_index_range(&b, repo, prefix, prefix) < 0)
+	if (git_iterator_for_tree_range(&a, repo, old_tree, pfx, pfx) < 0 ||
+	    git_iterator_for_index_range(&b, repo, pfx, pfx) < 0)
 		goto on_error;
 
-	git__free(prefix);
+	git__free(pfx);
 
 	return diff_from_iterators(repo, opts, a, b, diff);
 
 on_error:
-	git__free(prefix);
+	git__free(pfx);
 	git_iterator_free(a);
 	return -1;
 }
@@ -885,23 +789,22 @@ int git_diff_workdir_to_index(
 	const git_diff_options *opts,
 	git_diff_list **diff)
 {
-	git_iterator *a = NULL, *b = NULL;
 	int error;
-
-	char *prefix = opts ? diff_prefix_from_pathspec(&opts->pathspec) : NULL;
+	git_iterator *a = NULL, *b = NULL;
+	char *pfx = opts ? git_pathspec_prefix(&opts->pathspec) : NULL;
 
 	assert(repo && diff);
 
-	if ((error = git_iterator_for_index_range(&a, repo, prefix, prefix)) < 0 ||
-	    (error = git_iterator_for_workdir_range(&b, repo, prefix, prefix)) < 0)
+	if ((error = git_iterator_for_index_range(&a, repo, pfx, pfx)) < 0 ||
+	    (error = git_iterator_for_workdir_range(&b, repo, pfx, pfx)) < 0)
 		goto on_error;
 
-	git__free(prefix);
+	git__free(pfx);
 
 	return diff_from_iterators(repo, opts, a, b, diff);
 
 on_error:
-	git__free(prefix);
+	git__free(pfx);
 	git_iterator_free(a);
 	return error;
 }
@@ -910,26 +813,25 @@ on_error:
 int git_diff_workdir_to_tree(
 	git_repository *repo,
 	const git_diff_options *opts,
-	git_tree *old_tree,
+	git_tree *tree,
 	git_diff_list **diff)
 {
-	git_iterator *a = NULL, *b = NULL;
 	int error;
+	git_iterator *a = NULL, *b = NULL;
+	char *pfx = opts ? git_pathspec_prefix(&opts->pathspec) : NULL;
 
-	char *prefix = opts ? diff_prefix_from_pathspec(&opts->pathspec) : NULL;
-
-	assert(repo && old_tree && diff);
+	assert(repo && tree && diff);
 
-	if ((error = git_iterator_for_tree_range(&a, repo, old_tree, prefix, prefix)) < 0 ||
-	    (error = git_iterator_for_workdir_range(&b, repo, prefix, prefix)) < 0)
+	if ((error = git_iterator_for_tree_range(&a, repo, tree, pfx, pfx)) < 0 ||
+	    (error = git_iterator_for_workdir_range(&b, repo, pfx, pfx)) < 0)
 		goto on_error;
 
-	git__free(prefix);
+	git__free(pfx);
 
 	return diff_from_iterators(repo, opts, a, b, diff);
 
 on_error:
-	git__free(prefix);
+	git__free(pfx);
 	git_iterator_free(a);
 	return error;
 }
diff --git a/src/pathspec.c b/src/pathspec.c
new file mode 100644
index 0000000..9632f5f
--- /dev/null
+++ b/src/pathspec.c
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "pathspec.h"
+#include "attr_file.h"
+
+/* what is the common non-wildcard prefix for all items in the pathspec */
+char *git_pathspec_prefix(const git_strarray *pathspec)
+{
+	git_buf prefix = GIT_BUF_INIT;
+	const char *scan;
+
+	if (!pathspec || !pathspec->count ||
+		git_buf_common_prefix(&prefix, pathspec) < 0)
+		return NULL;
+
+	/* diff prefix will only be leading non-wildcards */
+	for (scan = prefix.ptr; *scan; ++scan) {
+		if (git__iswildcard(*scan) &&
+			(scan == prefix.ptr || (*(scan - 1) != '\\')))
+			break;
+	}
+	git_buf_truncate(&prefix, scan - prefix.ptr);
+
+	if (prefix.size <= 0) {
+		git_buf_free(&prefix);
+		return NULL;
+	}
+
+	git_buf_unescape(&prefix);
+
+	return git_buf_detach(&prefix);
+}
+
+/* is there anything in the spec that needs to be filtered on */
+bool git_pathspec_is_interesting(const git_strarray *pathspec)
+{
+	const char *str;
+
+	if (pathspec == NULL || pathspec->count == 0)
+		return false;
+	if (pathspec->count > 1)
+		return true;
+
+	str = pathspec->strings[0];
+	if (!str || !str[0] || (!str[1] && (str[0] == '*' || str[0] == '.')))
+		return false;
+	return true;
+}
+
+/* build a vector of fnmatch patterns to evaluate efficiently */
+int git_pathspec_init(
+	git_vector *vspec, const git_strarray *strspec, git_pool *strpool)
+{
+	size_t i;
+
+	memset(vspec, 0, sizeof(*vspec));
+
+	if (!git_pathspec_is_interesting(strspec))
+		return 0;
+
+	if (git_vector_init(vspec, strspec->count, NULL) < 0)
+		return -1;
+
+	for (i = 0; i < strspec->count; ++i) {
+		int ret;
+		const char *pattern = strspec->strings[i];
+		git_attr_fnmatch *match = git__calloc(1, sizeof(git_attr_fnmatch));
+		if (!match)
+			return -1;
+
+		match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE;
+
+		ret = git_attr_fnmatch__parse(match, strpool, NULL, &pattern);
+		if (ret == GIT_ENOTFOUND) {
+			git__free(match);
+			continue;
+		} else if (ret < 0)
+			return ret;
+
+		if (git_vector_insert(vspec, match) < 0)
+			return -1;
+	}
+
+	return 0;
+}
+
+/* free data from the pathspec vector */
+void git_pathspec_free(git_vector *vspec)
+{
+	git_attr_fnmatch *match;
+	unsigned int i;
+
+	git_vector_foreach(vspec, i, match) {
+		git__free(match);
+		vspec->contents[i] = NULL;
+	}
+
+	git_vector_free(vspec);
+}
+
+/* match a path against the vectorized pathspec */
+bool git_pathspec_match_path(
+	git_vector *vspec, const char *path, bool disable_fnmatch, bool casefold)
+{
+	unsigned int i;
+	git_attr_fnmatch *match;
+	int fnmatch_flags = 0;
+	int (*use_strcmp)(const char *, const char *);
+	int (*use_strncmp)(const char *, const char *, size_t);
+
+	if (!vspec || !vspec->length)
+		return true;
+
+	if (disable_fnmatch)
+		fnmatch_flags = -1;
+	else if (casefold)
+		fnmatch_flags = FNM_CASEFOLD;
+
+	if (casefold) {
+		use_strcmp  = strcasecmp;
+		use_strncmp = strncasecmp;
+	} else {
+		use_strcmp  = strcmp;
+		use_strncmp = strncmp;
+	}
+
+	git_vector_foreach(vspec, i, match) {
+		int result = use_strcmp(match->pattern, path) ? FNM_NOMATCH : 0;
+
+		if (fnmatch_flags >= 0 && result == FNM_NOMATCH)
+			result = p_fnmatch(match->pattern, path, fnmatch_flags);
+
+		/* if we didn't match, look for exact dirname prefix match */
+		if (result == FNM_NOMATCH &&
+			(match->flags & GIT_ATTR_FNMATCH_HASWILD) == 0 &&
+			use_strncmp(path, match->pattern, match->length) == 0 &&
+			path[match->length] == '/')
+			result = 0;
+
+		if (result == 0)
+			return (match->flags & GIT_ATTR_FNMATCH_NEGATIVE) ? false : true;
+	}
+
+	return false;
+}
+
diff --git a/src/pathspec.h b/src/pathspec.h
new file mode 100644
index 0000000..31a1cda
--- /dev/null
+++ b/src/pathspec.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2011-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_pathspec_h__
+#define INCLUDE_pathspec_h__
+
+#include "common.h"
+#include "buffer.h"
+#include "vector.h"
+#include "pool.h"
+
+/* what is the common non-wildcard prefix for all items in the pathspec */
+extern char *git_pathspec_prefix(const git_strarray *pathspec);
+
+/* is there anything in the spec that needs to be filtered on */
+extern bool git_pathspec_is_interesting(const git_strarray *pathspec);
+
+/* build a vector of fnmatch patterns to evaluate efficiently */
+extern int git_pathspec_init(
+	git_vector *vspec, const git_strarray *strspec, git_pool *strpool);
+
+/* free data from the pathspec vector */
+extern void git_pathspec_free(git_vector *vspec);
+
+/* match a path against the vectorized pathspec */
+extern bool git_pathspec_match_path(
+	git_vector *vspec, const char *path, bool disable_fnmatch, bool casefold);
+
+#endif