Skip to content

Commit c04318e

Browse files
aspiersgitster
authored andcommitted
dir.c: keep track of where patterns came from
For exclude patterns read in from files, the filename is stored in the exclude list, and the originating line number is stored in the individual exclude (counting starting at 1). For exclude patterns provided on the command line, a string describing the source of the patterns is stored in the exclude list, and the sequence number assigned to each exclude pattern is negative, with counting starting at -1. So for example the 2nd pattern provided via --exclude would be numbered -2. This allows any future consumers of that data to easily distinguish between exclude patterns from files vs. from the CLI. Signed-off-by: Adam Spiers <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c082df2 commit c04318e

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

builtin/clean.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
9797
if (!ignored)
9898
setup_standard_excludes(&dir);
9999

100-
add_exclude_list(&dir, EXC_CMDL);
100+
add_exclude_list(&dir, EXC_CMDL, "--exclude option");
101101
for (i = 0; i < exclude_list.nr; i++)
102102
add_exclude(exclude_list.items[i].string, "", 0,
103-
&dir.exclude_list_group[EXC_CMDL].el[0]);
103+
&dir.exclude_list_group[EXC_CMDL].el[0], -(i+1));
104104

105105
pathspec = get_pathspec(prefix, argv);
106106

builtin/ls-files.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static int error_unmatch;
3535
static char *ps_matched;
3636
static const char *with_tree;
3737
static int exc_given;
38+
static int exclude_args;
3839

3940
static const char *tag_cached = "";
4041
static const char *tag_unmerged = "";
@@ -423,7 +424,7 @@ static int option_parse_exclude(const struct option *opt,
423424
struct exclude_list_group *group = opt->value;
424425

425426
exc_given = 1;
426-
add_exclude(arg, "", 0, &group->el[0]);
427+
add_exclude(arg, "", 0, &group->el[0], --exclude_args);
427428

428429
return 0;
429430
}
@@ -524,7 +525,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
524525
if (read_cache() < 0)
525526
die("index file corrupt");
526527

527-
add_exclude_list(&dir, EXC_CMDL);
528+
add_exclude_list(&dir, EXC_CMDL, "--exclude option");
528529
argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
529530
ls_files_usage, 0);
530531
if (show_tag || show_valid_bit) {

dir.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ void parse_exclude_pattern(const char **pattern,
349349
}
350350

351351
void add_exclude(const char *string, const char *base,
352-
int baselen, struct exclude_list *el)
352+
int baselen, struct exclude_list *el, int srcpos)
353353
{
354354
struct exclude *x;
355355
int patternlen;
@@ -373,8 +373,10 @@ void add_exclude(const char *string, const char *base,
373373
x->base = base;
374374
x->baselen = baselen;
375375
x->flags = flags;
376+
x->srcpos = srcpos;
376377
ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
377378
el->excludes[el->nr++] = x;
379+
x->el = el;
378380
}
379381

380382
static void *read_skip_worktree_file_from_index(const char *path, size_t *size)
@@ -425,7 +427,7 @@ int add_excludes_from_file_to_list(const char *fname,
425427
int check_index)
426428
{
427429
struct stat st;
428-
int fd, i;
430+
int fd, i, lineno = 1;
429431
size_t size = 0;
430432
char *buf, *entry;
431433

@@ -467,15 +469,17 @@ int add_excludes_from_file_to_list(const char *fname,
467469
if (buf[i] == '\n') {
468470
if (entry != buf + i && entry[0] != '#') {
469471
buf[i - (i && buf[i-1] == '\r')] = 0;
470-
add_exclude(entry, base, baselen, el);
472+
add_exclude(entry, base, baselen, el, lineno);
471473
}
474+
lineno++;
472475
entry = buf + i + 1;
473476
}
474477
}
475478
return 0;
476479
}
477480

478-
struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type)
481+
struct exclude_list *add_exclude_list(struct dir_struct *dir,
482+
int group_type, const char *src)
479483
{
480484
struct exclude_list *el;
481485
struct exclude_list_group *group;
@@ -484,6 +488,7 @@ struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type)
484488
ALLOC_GROW(group->el, group->nr + 1, group->alloc);
485489
el = &group->el[group->nr++];
486490
memset(el, 0, sizeof(*el));
491+
el->src = src;
487492
return el;
488493
}
489494

@@ -493,7 +498,7 @@ struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type)
493498
void add_excludes_from_file(struct dir_struct *dir, const char *fname)
494499
{
495500
struct exclude_list *el;
496-
el = add_exclude_list(dir, EXC_FILE);
501+
el = add_exclude_list(dir, EXC_FILE, fname);
497502
if (add_excludes_from_file_to_list(fname, "", 0, el, 0) < 0)
498503
die("cannot use %s as an exclude file", fname);
499504
}
@@ -524,6 +529,7 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
524529
break;
525530
el = &group->el[dir->exclude_stack->exclude_ix];
526531
dir->exclude_stack = stk->prev;
532+
free((char *)el->src); /* see strdup() below */
527533
clear_exclude_list(el);
528534
free(stk);
529535
group->nr--;
@@ -550,7 +556,15 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
550556
memcpy(dir->basebuf + current, base + current,
551557
stk->baselen - current);
552558
strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
553-
el = add_exclude_list(dir, EXC_DIRS);
559+
/*
560+
* dir->basebuf gets reused by the traversal, but we
561+
* need fname to remain unchanged to ensure the src
562+
* member of each struct exclude correctly
563+
* back-references its source file. Other invocations
564+
* of add_exclude_list provide stable strings, so we
565+
* strdup() and free() here in the caller.
566+
*/
567+
el = add_exclude_list(dir, EXC_DIRS, strdup(dir->basebuf));
554568
stk->exclude_ix = group->nr - 1;
555569
add_excludes_from_file_to_list(dir->basebuf,
556570
dir->basebuf, stk->baselen,

dir.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,32 @@ struct dir_entry {
2525
struct exclude_list {
2626
int nr;
2727
int alloc;
28+
2829
/* remember pointer to exclude file contents so we can free() */
2930
char *filebuf;
3031

32+
/* origin of list, e.g. path to filename, or descriptive string */
33+
const char *src;
34+
3135
struct exclude {
36+
/*
37+
* This allows callers of last_exclude_matching() etc.
38+
* to determine the origin of the matching pattern.
39+
*/
40+
struct exclude_list *el;
41+
3242
const char *pattern;
3343
int patternlen;
3444
int nowildcardlen;
3545
const char *base;
3646
int baselen;
3747
int flags;
48+
49+
/*
50+
* Counting starts from 1 for line numbers in ignore files,
51+
* and from -1 decrementing for patterns from CLI args.
52+
*/
53+
int srcpos;
3854
} **excludes;
3955
};
4056

@@ -144,13 +160,14 @@ extern struct exclude *last_exclude_matching_path(struct path_exclude_check *, c
144160
extern int is_path_excluded(struct path_exclude_check *, const char *, int namelen, int *dtype);
145161

146162

147-
extern struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type);
163+
extern struct exclude_list *add_exclude_list(struct dir_struct *dir,
164+
int group_type, const char *src);
148165
extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen,
149166
struct exclude_list *el, int check_index);
150167
extern void add_excludes_from_file(struct dir_struct *, const char *fname);
151168
extern void parse_exclude_pattern(const char **string, int *patternlen, int *flags, int *nowildcardlen);
152169
extern void add_exclude(const char *string, const char *base,
153-
int baselen, struct exclude_list *el);
170+
int baselen, struct exclude_list *el, int srcpos);
154171
extern void clear_exclude_list(struct exclude_list *el);
155172
extern int file_exists(const char *);
156173

0 commit comments

Comments
 (0)