Skip to content

Commit dcf0c16

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 <gitster@pobox.com>
1 parent 2587d67 commit dcf0c16

File tree

7 files changed

+26
-34
lines changed

7 files changed

+26
-34
lines changed

builtin-add.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ static const char builtin_add_usage[] =
1717
"git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--refresh] [--] <filepattern>...";
1818

1919
static int take_worktree_changes;
20-
static const char *excludes_file;
2120

2221
static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
2322
{
@@ -57,12 +56,7 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec,
5756
memset(dir, 0, sizeof(*dir));
5857
if (!ignored_too) {
5958
dir->collect_ignored = 1;
60-
dir->exclude_per_dir = ".gitignore";
61-
path = git_path("info/exclude");
62-
if (!access(path, R_OK))
63-
add_excludes_from_file(dir, path);
64-
if (excludes_file != NULL && !access(excludes_file, R_OK))
65-
add_excludes_from_file(dir, excludes_file);
59+
setup_standard_excludes(dir);
6660
}
6761

6862
/*
@@ -144,18 +138,6 @@ static void refresh(int verbose, const char **pathspec)
144138
free(seen);
145139
}
146140

147-
static int git_add_config(const char *var, const char *value)
148-
{
149-
if (!strcmp(var, "core.excludesfile")) {
150-
if (!value)
151-
die("core.excludesfile without value");
152-
excludes_file = xstrdup(value);
153-
return 0;
154-
}
155-
156-
return git_default_config(var, value);
157-
}
158-
159141
static struct lock_file lock_file;
160142

161143
static const char ignore_error[] =
@@ -183,7 +165,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
183165
exit(1);
184166
}
185167

186-
git_config(git_add_config);
168+
git_config(git_default_config);
187169

188170
newfd = hold_locked_index(&lock_file, 1);
189171

cache.h

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

573573
extern char *editor_program;
574+
extern char *excludes_file;
574575

575576
/* base85 */
576577
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
@@ -709,3 +709,15 @@ int is_inside_dir(const char *dir)
709709
char buffer[PATH_MAX];
710710
return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
711711
}
712+
713+
void setup_standard_excludes(struct dir_struct *dir)
714+
{
715+
const char *path;
716+
717+
dir->exclude_per_dir = ".gitignore";
718+
path = git_path("info/exclude");
719+
if (!access(path, R_OK))
720+
add_excludes_from_file(dir, path);
721+
if (excludes_file && !access(excludes_file, R_OK))
722+
add_excludes_from_file(dir, excludes_file);
723+
}

dir.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ extern struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathna
6464
extern char *get_relative_cwd(char *buffer, int size, const char *dir);
6565
extern int is_inside_dir(const char *dir);
6666

67+
extern void setup_standard_excludes(struct dir_struct *dir);
68+
6769
#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
{
@@ -247,22 +246,16 @@ static void wt_status_print_changed(struct wt_status *s)
247246
static void wt_status_print_untracked(struct wt_status *s)
248247
{
249248
struct dir_struct dir;
250-
const char *x;
251249
int i;
252250
int shown_header = 0;
253251

254252
memset(&dir, 0, sizeof(dir));
255253

256-
dir.exclude_per_dir = ".gitignore";
257254
if (!s->untracked) {
258255
dir.show_other_directories = 1;
259256
dir.hide_empty_directories = 1;
260257
}
261-
x = git_path("info/exclude");
262-
if (file_exists(x))
263-
add_excludes_from_file(&dir, x);
264-
if (excludes_file && file_exists(excludes_file))
265-
add_excludes_from_file(&dir, excludes_file);
258+
setup_standard_excludes(&dir);
266259

267260
read_directory(&dir, ".", "", 0, NULL);
268261
for(i = 0; i < dir.nr; i++) {
@@ -360,11 +353,5 @@ int git_status_config(const char *k, const char *v)
360353
int slot = parse_status_slot(k, 13);
361354
color_parse(v, k, wt_status_colors[slot]);
362355
}
363-
if (!strcmp(k, "core.excludesfile")) {
364-
if (!v)
365-
die("core.excludesfile without value");
366-
excludes_file = xstrdup(v);
367-
return 0;
368-
}
369356
return git_default_config(k, v);
370357
}

0 commit comments

Comments
 (0)