Skip to content

Commit 931eab6

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent f3e743a commit 931eab6

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

builtin/check-ignore.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,45 @@ static void output_exclude(const char *path, struct exclude *exclude)
6464
}
6565

6666
static int check_ignore(struct dir_struct *dir,
67-
const char *prefix, const char **pathspec)
67+
const char *prefix, int argc, const char **argv)
6868
{
69-
const char *path, *full_path;
69+
const char *full_path;
7070
char *seen;
7171
int num_ignored = 0, dtype = DT_UNKNOWN, i;
7272
struct exclude *exclude;
73+
struct pathspec pathspec;
7374

74-
if (!pathspec || !*pathspec) {
75+
if (!argc) {
7576
if (!quiet)
7677
fprintf(stderr, "no pathspec given.\n");
7778
return 0;
7879
}
7980

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+
8092
/*
8193
* look for pathspecs matching entries in the index, since these
8294
* should not be ignored, in order to be consistent with
8395
* 'git status', 'git add' etc.
8496
*/
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];
92100
exclude = NULL;
93101
if (!seen[i]) {
94102
exclude = last_exclude_matching(dir, full_path, &dtype);
95103
}
96104
if (!quiet && (exclude || show_non_matching))
97-
output_exclude(path, exclude);
105+
output_exclude(pathspec.items[i].original, exclude);
98106
if (exclude)
99107
num_ignored++;
100108
}
@@ -120,7 +128,8 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
120128
strbuf_swap(&buf, &nbuf);
121129
}
122130
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);
124133
maybe_flush_or_die(stdout, "check-ignore to stdout");
125134
}
126135
strbuf_release(&buf);
@@ -166,7 +175,7 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
166175
if (stdin_paths) {
167176
num_ignored = check_ignore_stdin_paths(&dir, prefix);
168177
} else {
169-
num_ignored = check_ignore(&dir, prefix, argv);
178+
num_ignored = check_ignore(&dir, prefix, argc, argv);
170179
maybe_flush_or_die(stdout, "ignore to stdout");
171180
}
172181

pathspec.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,13 @@ void parse_pathspec(struct pathspec *pathspec,
370370
pathspec->magic |= item[i].magic;
371371
}
372372

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");
374377
qsort(pathspec->items, pathspec->nr,
375378
sizeof(struct pathspec_item), pathspec_item_cmp);
379+
}
376380
}
377381

378382
/*

pathspec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct pathspec {
5151
*/
5252
#define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)
5353
#define PATHSPEC_PREFIX_ORIGIN (1<<6)
54+
#define PATHSPEC_KEEP_ORDER (1<<7)
5455

5556
extern int init_pathspec(struct pathspec *, const char **);
5657
extern void parse_pathspec(struct pathspec *pathspec,

t/t0008-ignores.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,15 @@ test_expect_success_multi SYMLINKS 'symlink' ':: a/symlink' '
432432

433433
test_expect_success_multi SYMLINKS 'beyond a symlink' '' '
434434
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"
436436
'
437437

438438
test_expect_success_multi SYMLINKS 'beyond a symlink from subdirectory' '' '
439439
(
440440
cd a &&
441441
test_check_ignore "symlink/foo" 128
442442
) &&
443-
test_stderr "fatal: '\''symlink/foo'\'' is beyond a symbolic link"
443+
test_stderr "fatal: pathspec '\''symlink/foo'\'' is beyond a symbolic link"
444444
'
445445

446446
############################################################################
@@ -449,15 +449,15 @@ test_expect_success_multi SYMLINKS 'beyond a symlink from subdirectory' '' '
449449

450450
test_expect_success_multi 'submodule' '' '
451451
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'\''"
453453
'
454454

455455
test_expect_success_multi 'submodule from subdirectory' '' '
456456
(
457457
cd a &&
458458
test_check_ignore "submodule/one" 128
459459
) &&
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'\''"
461461
'
462462

463463
############################################################################

0 commit comments

Comments
 (0)