@matheusbsbs / matheus-bagliano / commits / 1fb328947c
Fork 0

git-clean: correct printing relative path

When the given path contains '..' then git-clean incorrectly printed names of files. This patch changes cmd_clean to use quote_path_relative(). Also, "failed to remove ..." message used absolutely path, but not it is corrected to use relative path. Signed-off-by: Dmitry Potapov <dpotapov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Dmitry Potapov committed Mar 7, 2008 at 04:13 UTC 1fb328947c8e3ace9df7d2d5374e26e2510a4e93
1 file changed +14 -18
builtin-clean.c
+14 -18
@@ -10,6 +10,7 @@
10 #include "cache.h"
11 #include "dir.h"
12 #include "parse-options.h"
13 +#include "quote.h"
14
15 static int force = -1; /* unset */
16
@@ -34,7 +35,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
35 struct dir_struct dir;
36 const char *path, *base;
37 static const char **pathspec;
37 - int prefix_offset = 0;
38 + struct strbuf buf;
39 + const char *qname;
40 char *seen = NULL;
41 struct option options[] = {
42 OPT__QUIET(&quiet),
@@ -56,6 +58,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
58
59 argc = parse_options(argc, argv, options, builtin_clean_usage, 0);
60
61 + strbuf_init(&buf, 0);
62 memset(&dir, 0, sizeof(dir));
63 if (ignored_only)
64 dir.show_ignored = 1;
@@ -72,8 +75,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
75 if (!ignored)
76 setup_standard_excludes(&dir);
77
75 - if (prefix)
76 - prefix_offset = strlen(prefix);
78 pathspec = get_pathspec(prefix, argv);
79 read_cache();
80
@@ -134,39 +135,34 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
135
136 if (S_ISDIR(st.st_mode)) {
137 strbuf_addstr(&directory, ent->name);
138 + qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
139 if (show_only && (remove_directories || matches)) {
138 - printf("Would remove %s\n",
139 - directory.buf + prefix_offset);
140 + printf("Would remove %s\n", qname);
141 } else if (remove_directories || matches) {
142 if (!quiet)
142 - printf("Removing %s\n",
143 - directory.buf + prefix_offset);
143 + printf("Removing %s\n", qname);
144 if (remove_dir_recursively(&directory, 0) != 0) {
145 - warning("failed to remove '%s'",
146 - directory.buf + prefix_offset);
145 + warning("failed to remove '%s'", qname);
146 errors++;
147 }
148 } else if (show_only) {
150 - printf("Would not remove %s\n",
151 - directory.buf + prefix_offset);
149 + printf("Would not remove %s\n", qname);
150 } else {
153 - printf("Not removing %s\n",
154 - directory.buf + prefix_offset);
151 + printf("Not removing %s\n", qname);
152 }
153 strbuf_reset(&directory);
154 } else {
155 if (pathspec && !matches)
156 continue;
157 + qname = quote_path_relative(ent->name, -1, &buf, prefix);
158 if (show_only) {
161 - printf("Would remove %s\n",
162 - ent->name + prefix_offset);
159 + printf("Would remove %s\n", qname);
160 continue;
161 } else if (!quiet) {
165 - printf("Removing %s\n",
166 - ent->name + prefix_offset);
162 + printf("Removing %s\n", qname);
163 }
164 if (unlink(ent->name) != 0) {
169 - warning("failed to remove '%s'", ent->name);
165 + warning("failed to remove '%s'", qname);
166 errors++;
167 }
168 }