git-clean: Honor pathspec.
git-clean "*.rej" should attempt to look at only paths that match pattern "*.rej", but rewrite to C broke it. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Dec 4, 2007 at 23:55 UTC
d871c86935bf16fed541c7df6d1b153ca3c64496
1 file changed
+31
-21
builtin-clean.c
+31
-21
@@ -27,13 +27,14 @@ static int git_clean_config(const char *var, const char *value)
27
28
int cmd_clean(int argc, const char **argv, const char *prefix)
29
{
30
- int j;
30
+ int i;
31
int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
32
int ignored_only = 0, baselen = 0, config_set = 0;
33
struct strbuf directory;
34
struct dir_struct dir;
35
const char *path, *base;
36
static const char **pathspec;
37
+ char *seen = NULL;
38
struct option options[] = {
39
OPT__QUIET(&quiet),
40
OPT__DRY_RUN(&show_only),
@@ -85,12 +86,14 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
86
read_directory(&dir, path, base, baselen, pathspec);
87
strbuf_init(&directory, 0);
88
88
- for (j = 0; j < dir.nr; ++j) {
89
- struct dir_entry *ent = dir.entries[j];
90
- int len, pos, specs;
89
+ if (pathspec)
90
+ seen = xmalloc(argc);
91
+
92
+ for (i = 0; i < dir.nr; i++) {
93
+ struct dir_entry *ent = dir.entries[i];
94
+ int len, pos, matches;
95
struct cache_entry *ce;
96
struct stat st;
93
- char *seen;
97
98
/*
99
* Remove the '/' at the end that directory
@@ -110,25 +113,29 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
113
continue; /* Yup, this one exists unmerged */
114
}
115
113
- if (!lstat(ent->name, &st) && (S_ISDIR(st.st_mode))) {
114
- int matched_path = 0;
116
+ /*
117
+ * we might have removed this as part of earlier
118
+ * recursive directory removal, so lstat() here could
119
+ * fail with ENOENT.
120
+ */
121
+ if (lstat(ent->name, &st))
122
+ continue;
123
+
124
+ if (pathspec) {
125
+ memset(seen, 0, argc);
126
+ matches = match_pathspec(pathspec, ent->name, ent->len,
127
+ baselen, seen);
128
+ } else {
129
+ matches = 0;
130
+ }
131
+
132
+ if (S_ISDIR(st.st_mode)) {
133
strbuf_addstr(&directory, ent->name);
116
- if (pathspec) {
117
- for (specs =0; pathspec[specs]; ++specs)
118
- /* nothing */;
119
- seen = xcalloc(specs, 1);
120
- /* Check if directory was explictly passed as
121
- * pathspec. If so we want to remove it */
122
- if (match_pathspec(pathspec, ent->name, ent->len,
123
- baselen, seen))
124
- matched_path = 1;
125
- free(seen);
126
- }
127
- if (show_only && (remove_directories || matched_path)) {
134
+ if (show_only && (remove_directories || matches)) {
135
printf("Would remove %s\n", directory.buf);
129
- } else if (quiet && (remove_directories || matched_path)) {
136
+ } else if (quiet && (remove_directories || matches)) {
137
remove_dir_recursively(&directory, 0);
131
- } else if (remove_directories || matched_path) {
138
+ } else if (remove_directories || matches) {
139
printf("Removing %s\n", directory.buf);
140
remove_dir_recursively(&directory, 0);
141
} else if (show_only) {
@@ -138,6 +145,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
145
}
146
strbuf_reset(&directory);
147
} else {
148
+ if (pathspec && !matches)
149
+ continue;
150
if (show_only) {
151
printf("Would remove %s\n", ent->name);
152
continue;
@@ -147,6 +156,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
156
unlink(ent->name);
157
}
158
}
159
+ free(seen);
160
161
strbuf_release(&directory);
162
return 0;