Skip to content

Commit 1fb3289

Browse files
dmpotgitster
authored andcommitted
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>
1 parent a734d0b commit 1fb3289

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

builtin-clean.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "cache.h"
1111
#include "dir.h"
1212
#include "parse-options.h"
13+
#include "quote.h"
1314

1415
static int force = -1; /* unset */
1516

@@ -34,7 +35,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
3435
struct dir_struct dir;
3536
const char *path, *base;
3637
static const char **pathspec;
37-
int prefix_offset = 0;
38+
struct strbuf buf;
39+
const char *qname;
3840
char *seen = NULL;
3941
struct option options[] = {
4042
OPT__QUIET(&quiet),
@@ -56,6 +58,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
5658

5759
argc = parse_options(argc, argv, options, builtin_clean_usage, 0);
5860

61+
strbuf_init(&buf, 0);
5962
memset(&dir, 0, sizeof(dir));
6063
if (ignored_only)
6164
dir.show_ignored = 1;
@@ -72,8 +75,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
7275
if (!ignored)
7376
setup_standard_excludes(&dir);
7477

75-
if (prefix)
76-
prefix_offset = strlen(prefix);
7778
pathspec = get_pathspec(prefix, argv);
7879
read_cache();
7980

@@ -134,39 +135,34 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
134135

135136
if (S_ISDIR(st.st_mode)) {
136137
strbuf_addstr(&directory, ent->name);
138+
qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
137139
if (show_only && (remove_directories || matches)) {
138-
printf("Would remove %s\n",
139-
directory.buf + prefix_offset);
140+
printf("Would remove %s\n", qname);
140141
} else if (remove_directories || matches) {
141142
if (!quiet)
142-
printf("Removing %s\n",
143-
directory.buf + prefix_offset);
143+
printf("Removing %s\n", qname);
144144
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);
147146
errors++;
148147
}
149148
} else if (show_only) {
150-
printf("Would not remove %s\n",
151-
directory.buf + prefix_offset);
149+
printf("Would not remove %s\n", qname);
152150
} else {
153-
printf("Not removing %s\n",
154-
directory.buf + prefix_offset);
151+
printf("Not removing %s\n", qname);
155152
}
156153
strbuf_reset(&directory);
157154
} else {
158155
if (pathspec && !matches)
159156
continue;
157+
qname = quote_path_relative(ent->name, -1, &buf, prefix);
160158
if (show_only) {
161-
printf("Would remove %s\n",
162-
ent->name + prefix_offset);
159+
printf("Would remove %s\n", qname);
163160
continue;
164161
} else if (!quiet) {
165-
printf("Removing %s\n",
166-
ent->name + prefix_offset);
162+
printf("Removing %s\n", qname);
167163
}
168164
if (unlink(ent->name) != 0) {
169-
warning("failed to remove '%s'", ent->name);
165+
warning("failed to remove '%s'", qname);
170166
errors++;
171167
}
172168
}

0 commit comments

Comments
 (0)