git-clean: handle errors if removing files fails
git-clean simply ignored errors if removing a file or directory failed. This patch makes it raise a warning and the exit code also greater than zero if there are remaining files. Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Miklos Vajna committed
Feb 21, 2008 at 02:44 UTC
aa9c83c2197cafb884face711ab7235790f9f265
2 files changed
+24
-8
builtin-clean.c
+14
-8
@@ -29,7 +29,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
29
{
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;
32
+ int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
33
struct strbuf directory;
34
struct dir_struct dir;
35
const char *path, *base;
@@ -137,12 +137,15 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
137
if (show_only && (remove_directories || matches)) {
138
printf("Would remove %s\n",
139
directory.buf + prefix_offset);
140
- } else if (quiet && (remove_directories || matches)) {
141
- remove_dir_recursively(&directory, 0);
140
} else if (remove_directories || matches) {
143
- printf("Removing %s\n",
144
- directory.buf + prefix_offset);
145
- remove_dir_recursively(&directory, 0);
141
+ if (!quiet)
142
+ printf("Removing %s\n",
143
+ directory.buf + prefix_offset);
144
+ if (remove_dir_recursively(&directory, 0) != 0) {
145
+ warning("failed to remove '%s'",
146
+ directory.buf + prefix_offset);
147
+ errors++;
148
+ }
149
} else if (show_only) {
150
printf("Would not remove %s\n",
151
directory.buf + prefix_offset);
@@ -162,11 +165,14 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
165
printf("Removing %s\n",
166
ent->name + prefix_offset);
167
}
165
- unlink(ent->name);
168
+ if (unlink(ent->name) != 0) {
169
+ warning("failed to remove '%s'", ent->name);
170
+ errors++;
171
+ }
172
}
173
}
174
free(seen);
175
176
strbuf_release(&directory);
171
- return 0;
177
+ return (errors != 0);
178
}
t/t7300-clean.sh
+10
@@ -316,4 +316,14 @@ test_expect_success 'core.excludesfile' '
316
317
'
318
319
+test_expect_success 'removal failure' '
320
+
321
+ mkdir foo &&
322
+ touch foo/bar &&
323
+ chmod 0 foo &&
324
+ ! git clean -f -d
325
+
326
+'
327
+chmod 755 foo
328
+
329
test_done