Skip to content

Commit 0006d85

Browse files
aspiersgitster
authored andcommitted
check-ignore: move setup into cmd_check_ignore()
Initialisation of the dir_struct and path_exclude_check structs was previously done within check_ignore(). This was acceptable since check_ignore() was only called once per check-ignore invocation; however the next commit will convert it into an inner loop which is called once per line of STDIN when --stdin is given. Therefore moving the initialisation code out into cmd_check_ignore() ensures that initialisation is still only performed once per check-ignore invocation, and consequently that the output is identical whether pathspecs are provided as CLI arguments or via STDIN. Signed-off-by: Adam Spiers <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ae3caf4 commit 0006d85

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

builtin/check-ignore.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,20 @@ static void output_exclude(const char *path, struct exclude *exclude)
6363
}
6464
}
6565

66-
static int check_ignore(const char *prefix, const char **pathspec)
66+
static int check_ignore(struct path_exclude_check *check,
67+
const char *prefix, const char **pathspec)
6768
{
68-
struct dir_struct dir;
6969
const char *path, *full_path;
7070
char *seen;
7171
int num_ignored = 0, dtype = DT_UNKNOWN, i;
72-
struct path_exclude_check check;
7372
struct exclude *exclude;
7473

75-
/* read_cache() is only necessary so we can watch out for submodules. */
76-
if (read_cache() < 0)
77-
die(_("index file corrupt"));
78-
79-
memset(&dir, 0, sizeof(dir));
80-
dir.flags |= DIR_COLLECT_IGNORED;
81-
setup_standard_excludes(&dir);
82-
8374
if (!pathspec || !*pathspec) {
8475
if (!quiet)
8576
fprintf(stderr, "no pathspec given.\n");
8677
return 0;
8778
}
8879

89-
path_exclude_check_init(&check, &dir);
9080
/*
9181
* look for pathspecs matching entries in the index, since these
9282
* should not be ignored, in order to be consistent with
@@ -101,7 +91,7 @@ static int check_ignore(const char *prefix, const char **pathspec)
10191
die_if_path_beyond_symlink(full_path, prefix);
10292
exclude = NULL;
10393
if (!seen[i]) {
104-
exclude = last_exclude_matching_path(&check, full_path,
94+
exclude = last_exclude_matching_path(check, full_path,
10595
-1, &dtype);
10696
}
10797
if (!quiet && (exclude || show_non_matching))
@@ -110,13 +100,11 @@ static int check_ignore(const char *prefix, const char **pathspec)
110100
num_ignored++;
111101
}
112102
free(seen);
113-
clear_directory(&dir);
114-
path_exclude_check_clear(&check);
115103

116104
return num_ignored;
117105
}
118106

119-
static int check_ignore_stdin_paths(const char *prefix)
107+
static int check_ignore_stdin_paths(struct path_exclude_check *check, const char *prefix)
120108
{
121109
struct strbuf buf, nbuf;
122110
char **pathspec = NULL;
@@ -139,17 +127,18 @@ static int check_ignore_stdin_paths(const char *prefix)
139127
}
140128
ALLOC_GROW(pathspec, nr + 1, alloc);
141129
pathspec[nr] = NULL;
142-
num_ignored = check_ignore(prefix, (const char **)pathspec);
130+
num_ignored = check_ignore(check, prefix, (const char **)pathspec);
143131
maybe_flush_or_die(stdout, "attribute to stdout");
144132
strbuf_release(&buf);
145133
strbuf_release(&nbuf);
146-
free(pathspec);
147134
return num_ignored;
148135
}
149136

150137
int cmd_check_ignore(int argc, const char **argv, const char *prefix)
151138
{
152139
int num_ignored;
140+
struct dir_struct dir;
141+
struct path_exclude_check check;
153142

154143
git_config(git_default_config, NULL);
155144

@@ -174,12 +163,24 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
174163
if (show_non_matching && !verbose)
175164
die(_("--non-matching is only valid with --verbose"));
176165

166+
/* read_cache() is only necessary so we can watch out for submodules. */
167+
if (read_cache() < 0)
168+
die(_("index file corrupt"));
169+
170+
memset(&dir, 0, sizeof(dir));
171+
dir.flags |= DIR_COLLECT_IGNORED;
172+
setup_standard_excludes(&dir);
173+
174+
path_exclude_check_init(&check, &dir);
177175
if (stdin_paths) {
178-
num_ignored = check_ignore_stdin_paths(prefix);
176+
num_ignored = check_ignore_stdin_paths(&check, prefix);
179177
} else {
180-
num_ignored = check_ignore(prefix, argv);
178+
num_ignored = check_ignore(&check, prefix, argv);
181179
maybe_flush_or_die(stdout, "ignore to stdout");
182180
}
183181

182+
clear_directory(&dir);
183+
path_exclude_check_clear(&check);
184+
184185
return !num_ignored;
185186
}

0 commit comments

Comments
 (0)