check-ignore: convert to use parse_pathspec
check-ignore (at least the test suite) seems to rely on the pattern order. PATHSPEC_KEEP_ORDER is introduced to explictly express this. The lack of PATHSPEC_MAXDEPTH_VALID is sufficient because it's the only flag that reorders pathspecs, but it's less obvious that way. Cc: Adam Spiers <git@adamspiers.org> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Jul 14, 2013 at 15:35 UTC
931eab64ad24a742182c4e946a1138e677a8bd4e
4 files changed
+32
-18
builtin/check-ignore.c
+22
-13
@@ -64,37 +64,45 @@ static void output_exclude(const char *path, struct exclude *exclude)
64
}
65
66
static int check_ignore(struct dir_struct *dir,
67
- const char *prefix, const char **pathspec)
67
+ const char *prefix, int argc, const char **argv)
68
{
69
- const char *path, *full_path;
69
+ const char *full_path;
70
char *seen;
71
int num_ignored = 0, dtype = DT_UNKNOWN, i;
72
struct exclude *exclude;
73
+ struct pathspec pathspec;
74
74
- if (!pathspec || !*pathspec) {
75
+ if (!argc) {
76
if (!quiet)
77
fprintf(stderr, "no pathspec given.\n");
78
return 0;
79
}
80
81
+ /*
82
+ * check-ignore just needs paths. Magic beyond :/ is really
83
+ * irrelevant.
84
+ */
85
+ parse_pathspec(&pathspec,
86
+ PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
87
+ PATHSPEC_SYMLINK_LEADING_PATH |
88
+ PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE |
89
+ PATHSPEC_KEEP_ORDER,
90
+ prefix, argv);
91
+
92
/*
93
* look for pathspecs matching entries in the index, since these
94
* should not be ignored, in order to be consistent with
95
* 'git status', 'git add' etc.
96
*/
85
- seen = find_pathspecs_matching_against_index(pathspec);
86
- for (i = 0; pathspec[i]; i++) {
87
- path = pathspec[i];
88
- full_path = prefix_path(prefix, prefix
89
- ? strlen(prefix) : 0, path);
90
- full_path = check_path_for_gitlink(full_path);
91
- die_if_path_beyond_symlink(full_path, prefix);
97
+ seen = find_pathspecs_matching_against_index(pathspec.raw);
98
+ for (i = 0; i < pathspec.nr; i++) {
99
+ full_path = pathspec.raw[i];
100
exclude = NULL;
101
if (!seen[i]) {
102
exclude = last_exclude_matching(dir, full_path, &dtype);
103
}
104
if (!quiet && (exclude || show_non_matching))
97
- output_exclude(path, exclude);
105
+ output_exclude(pathspec.items[i].original, exclude);
106
if (exclude)
107
num_ignored++;
108
}
@@ -120,7 +128,8 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
128
strbuf_swap(&buf, &nbuf);
129
}
130
pathspec[0] = buf.buf;
123
- num_ignored += check_ignore(dir, prefix, (const char **)pathspec);
131
+ num_ignored += check_ignore(dir, prefix,
132
+ 1, (const char **)pathspec);
133
maybe_flush_or_die(stdout, "check-ignore to stdout");
134
}
135
strbuf_release(&buf);
@@ -166,7 +175,7 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
175
if (stdin_paths) {
176
num_ignored = check_ignore_stdin_paths(&dir, prefix);
177
} else {
169
- num_ignored = check_ignore(&dir, prefix, argv);
178
+ num_ignored = check_ignore(&dir, prefix, argc, argv);
179
maybe_flush_or_die(stdout, "ignore to stdout");
180
}
181
pathspec.c
+5
-1
@@ -370,9 +370,13 @@ void parse_pathspec(struct pathspec *pathspec,
370
pathspec->magic |= item[i].magic;
371
}
372
373
- if (pathspec->magic & PATHSPEC_MAXDEPTH)
373
+
374
+ if (pathspec->magic & PATHSPEC_MAXDEPTH) {
375
+ if (flags & PATHSPEC_KEEP_ORDER)
376
+ die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
377
qsort(pathspec->items, pathspec->nr,
378
sizeof(struct pathspec_item), pathspec_item_cmp);
379
+ }
380
}
381
382
/*
pathspec.h
+1
@@ -51,6 +51,7 @@ struct pathspec {
51
*/
52
#define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)
53
#define PATHSPEC_PREFIX_ORIGIN (1<<6)
54
+#define PATHSPEC_KEEP_ORDER (1<<7)
55
56
extern int init_pathspec(struct pathspec *, const char **);
57
extern void parse_pathspec(struct pathspec *pathspec,
t/t0008-ignores.sh
+4
-4
@@ -432,7 +432,7 @@ test_expect_success_multi SYMLINKS 'symlink' ':: a/symlink' '
432
433
test_expect_success_multi SYMLINKS 'beyond a symlink' '' '
434
test_check_ignore "a/symlink/foo" 128 &&
435
- test_stderr "fatal: '\''a/symlink/foo'\'' is beyond a symbolic link"
435
+ test_stderr "fatal: pathspec '\''a/symlink/foo'\'' is beyond a symbolic link"
436
'
437
438
test_expect_success_multi SYMLINKS 'beyond a symlink from subdirectory' '' '
@@ -440,7 +440,7 @@ test_expect_success_multi SYMLINKS 'beyond a symlink from subdirectory' '' '
440
cd a &&
441
test_check_ignore "symlink/foo" 128
442
) &&
443
- test_stderr "fatal: '\''symlink/foo'\'' is beyond a symbolic link"
443
+ test_stderr "fatal: pathspec '\''symlink/foo'\'' is beyond a symbolic link"
444
'
445
446
############################################################################
@@ -449,7 +449,7 @@ test_expect_success_multi SYMLINKS 'beyond a symlink from subdirectory' '' '
449
450
test_expect_success_multi 'submodule' '' '
451
test_check_ignore "a/submodule/one" 128 &&
452
- test_stderr "fatal: Path '\''a/submodule/one'\'' is in submodule '\''a/submodule'\''"
452
+ test_stderr "fatal: Pathspec '\''a/submodule/one'\'' is in submodule '\''a/submodule'\''"
453
'
454
455
test_expect_success_multi 'submodule from subdirectory' '' '
@@ -457,7 +457,7 @@ test_expect_success_multi 'submodule from subdirectory' '' '
457
cd a &&
458
test_check_ignore "submodule/one" 128
459
) &&
460
- test_stderr "fatal: Path '\''a/submodule/one'\'' is in submodule '\''a/submodule'\''"
460
+ test_stderr "fatal: Pathspec '\''submodule/one'\'' is in submodule '\''a/submodule'\''"
461
'
462
463
############################################################################