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 <git@adamspiers.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Adam Spiers committed
Apr 11, 2013 at 13:05 UTC
0006d85c3a2fcbd803ed46cf283fab4e4fae3a7f
1 file changed
+21
-20
builtin/check-ignore.c
+21
-20
@@ -63,30 +63,20 @@ static void output_exclude(const char *path, struct exclude *exclude)
63
}
64
}
65
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)
68
{
68
- struct dir_struct dir;
69
const char *path, *full_path;
70
char *seen;
71
int num_ignored = 0, dtype = DT_UNKNOWN, i;
72
- struct path_exclude_check check;
72
struct exclude *exclude;
73
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
-
74
if (!pathspec || !*pathspec) {
75
if (!quiet)
76
fprintf(stderr, "no pathspec given.\n");
77
return 0;
78
}
79
89
- path_exclude_check_init(&check, &dir);
80
/*
81
* look for pathspecs matching entries in the index, since these
82
* should not be ignored, in order to be consistent with
@@ -101,7 +91,7 @@ static int check_ignore(const char *prefix, const char **pathspec)
91
die_if_path_beyond_symlink(full_path, prefix);
92
exclude = NULL;
93
if (!seen[i]) {
104
- exclude = last_exclude_matching_path(&check, full_path,
94
+ exclude = last_exclude_matching_path(check, full_path,
95
-1, &dtype);
96
}
97
if (!quiet && (exclude || show_non_matching))
@@ -110,13 +100,11 @@ static int check_ignore(const char *prefix, const char **pathspec)
100
num_ignored++;
101
}
102
free(seen);
113
- clear_directory(&dir);
114
- path_exclude_check_clear(&check);
103
104
return num_ignored;
105
}
106
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)
108
{
109
struct strbuf buf, nbuf;
110
char **pathspec = NULL;
@@ -139,17 +127,18 @@ static int check_ignore_stdin_paths(const char *prefix)
127
}
128
ALLOC_GROW(pathspec, nr + 1, alloc);
129
pathspec[nr] = NULL;
142
- num_ignored = check_ignore(prefix, (const char **)pathspec);
130
+ num_ignored = check_ignore(check, prefix, (const char **)pathspec);
131
maybe_flush_or_die(stdout, "attribute to stdout");
132
strbuf_release(&buf);
133
strbuf_release(&nbuf);
146
- free(pathspec);
134
return num_ignored;
135
}
136
137
int cmd_check_ignore(int argc, const char **argv, const char *prefix)
138
{
139
int num_ignored;
140
+ struct dir_struct dir;
141
+ struct path_exclude_check check;
142
143
git_config(git_default_config, NULL);
144
@@ -174,12 +163,24 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
163
if (show_non_matching && !verbose)
164
die(_("--non-matching is only valid with --verbose"));
165
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);
175
if (stdin_paths) {
178
- num_ignored = check_ignore_stdin_paths(prefix);
176
+ num_ignored = check_ignore_stdin_paths(&check, prefix);
177
} else {
180
- num_ignored = check_ignore(prefix, argv);
178
+ num_ignored = check_ignore(&check, prefix, argv);
179
maybe_flush_or_die(stdout, "ignore to stdout");
180
}
181
182
+ clear_directory(&dir);
183
+ path_exclude_check_clear(&check);
184
+
185
return !num_ignored;
186
}