Skip to content

Commit 039bc64

Browse files
committed
core.excludesfile clean-up
There are inconsistencies in the way commands currently handle the core.excludesfile configuration variable. The problem is the variable is too new to be noticed by anything other than git-add and git-status. * git-ls-files does not notice any of the "ignore" files by default, as it predates the standardized set of ignore files. The calling scripts established the convention to use .git/info/exclude, .gitignore, and later core.excludesfile. * git-add and git-status know about it because they call add_excludes_from_file() directly with their own notion of which standard set of ignore files to use. This is just a stupid duplication of code that need to be updated every time the definition of the standard set of ignore files is changed. * git-read-tree takes --exclude-per-directory=<gitignore>, not because the flexibility was needed. Again, this was because the option predates the standardization of the ignore files. * git-merge-recursive uses hardcoded per-directory .gitignore and nothing else. git-clean (scripted version) does not honor core.* because its call to underlying ls-files does not know about it. git-clean in C (parked in 'pu') doesn't either. We probably could change git-ls-files to use the standard set when no excludes are specified on the command line and ignore processing was asked, or something like that, but that will be a change in semantics and might break people's scripts in a subtle way. I am somewhat reluctant to make such a change. On the other hand, I think it makes perfect sense to fix git-read-tree, git-merge-recursive and git-clean to follow the same rule as other commands. I do not think of a valid use case to give an exclude-per-directory that is nonstandard to read-tree command, outside a "negative" test in the t1004 test script. This patch is the first step to untangle this mess. The next step would be to teach read-tree, merge-recursive and clean (in C) to use setup_standard_excludes(). Signed-off-by: Junio C Hamano <[email protected]>
1 parent f5f6cb8 commit 039bc64

File tree

7 files changed

+25
-34
lines changed

7 files changed

+25
-34
lines changed

builtin-add.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ static const char * const builtin_add_usage[] = {
2121
};
2222

2323
static int take_worktree_changes;
24-
static const char *excludes_file;
2524

2625
static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
2726
{
@@ -61,12 +60,7 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec,
6160
memset(dir, 0, sizeof(*dir));
6261
if (!ignored_too) {
6362
dir->collect_ignored = 1;
64-
dir->exclude_per_dir = ".gitignore";
65-
path = git_path("info/exclude");
66-
if (!access(path, R_OK))
67-
add_excludes_from_file(dir, path);
68-
if (excludes_file != NULL && !access(excludes_file, R_OK))
69-
add_excludes_from_file(dir, excludes_file);
63+
setup_standard_excludes(dir);
7064
}
7165

7266
/*
@@ -141,18 +135,6 @@ static void refresh(int verbose, const char **pathspec)
141135
free(seen);
142136
}
143137

144-
static int git_add_config(const char *var, const char *value)
145-
{
146-
if (!strcmp(var, "core.excludesfile")) {
147-
if (!value)
148-
die("core.excludesfile without value");
149-
excludes_file = xstrdup(value);
150-
return 0;
151-
}
152-
153-
return git_default_config(var, value);
154-
}
155-
156138
int interactive_add(void)
157139
{
158140
const char *argv[2] = { "add--interactive", NULL };
@@ -193,7 +175,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
193175
exit(interactive_add());
194176
}
195177

196-
git_config(git_add_config);
178+
git_config(git_default_config);
197179

198180
newfd = hold_locked_index(&lock_file, 1);
199181

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ extern int pager_in_use;
581581
extern int pager_use_color;
582582

583583
extern char *editor_program;
584+
extern char *excludes_file;
584585

585586
/* base85 */
586587
int decode_85(char *dst, const char *line, int linelen);

config.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,13 @@ int git_default_config(const char *var, const char *value)
431431
return 0;
432432
}
433433

434+
if (!strcmp(var, "core.excludesfile")) {
435+
if (!value)
436+
die("core.excludesfile without value");
437+
excludes_file = xstrdup(value);
438+
return 0;
439+
}
440+
434441
/* Add other config variables here and to Documentation/config.txt. */
435442
return 0;
436443
}

dir.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,3 +778,15 @@ int remove_dir_recursively(struct strbuf *path, int only_empty)
778778
ret = rmdir(path->buf);
779779
return ret;
780780
}
781+
782+
void setup_standard_excludes(struct dir_struct *dir)
783+
{
784+
const char *path;
785+
786+
dir->exclude_per_dir = ".gitignore";
787+
path = git_path("info/exclude");
788+
if (!access(path, R_OK))
789+
add_excludes_from_file(dir, path);
790+
if (excludes_file && !access(excludes_file, R_OK))
791+
add_excludes_from_file(dir, excludes_file);
792+
}

dir.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ extern struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathna
7171
extern char *get_relative_cwd(char *buffer, int size, const char *dir);
7272
extern int is_inside_dir(const char *dir);
7373

74+
extern void setup_standard_excludes(struct dir_struct *dir);
7475
extern int remove_dir_recursively(struct strbuf *path, int only_empty);
7576

7677
#endif

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ char *pager_program;
3434
int pager_in_use;
3535
int pager_use_color = 1;
3636
char *editor_program;
37+
char *excludes_file;
3738
int auto_crlf = 0; /* 1: both ways, -1: only when adding git objects */
3839

3940
/* This is set by setup_git_dir_gently() and/or git_default_config() */

wt-status.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ static const char use_add_rm_msg[] =
2222
"use \"git add/rm <file>...\" to update what will be committed";
2323
static const char use_add_to_include_msg[] =
2424
"use \"git add <file>...\" to include in what will be committed";
25-
static const char *excludes_file;
2625

2726
static int parse_status_slot(const char *var, int offset)
2827
{
@@ -251,22 +250,16 @@ static void wt_status_print_changed(struct wt_status *s)
251250
static void wt_status_print_untracked(struct wt_status *s)
252251
{
253252
struct dir_struct dir;
254-
const char *x;
255253
int i;
256254
int shown_header = 0;
257255

258256
memset(&dir, 0, sizeof(dir));
259257

260-
dir.exclude_per_dir = ".gitignore";
261258
if (!s->untracked) {
262259
dir.show_other_directories = 1;
263260
dir.hide_empty_directories = 1;
264261
}
265-
x = git_path("info/exclude");
266-
if (file_exists(x))
267-
add_excludes_from_file(&dir, x);
268-
if (excludes_file && file_exists(excludes_file))
269-
add_excludes_from_file(&dir, excludes_file);
262+
setup_standard_excludes(&dir);
270263

271264
read_directory(&dir, ".", "", 0, NULL);
272265
for(i = 0; i < dir.nr; i++) {
@@ -364,11 +357,5 @@ int git_status_config(const char *k, const char *v)
364357
int slot = parse_status_slot(k, 13);
365358
color_parse(v, k, wt_status_colors[slot]);
366359
}
367-
if (!strcmp(k, "core.excludesfile")) {
368-
if (!v)
369-
die("core.excludesfile without value");
370-
excludes_file = xstrdup(v);
371-
return 0;
372-
}
373360
return git_default_config(k, v);
374361
}

0 commit comments

Comments
 (0)