Skip to content

Commit f2d0df7

Browse files
sbohrergitster
authored andcommitted
git clean: Don't automatically remove directories when run within subdirectory
When git clean is run from a subdirectory it should follow the normal policy and only remove directories if they are passed in as a pathspec, or -d is specified. The fix is to send len which could be shorter than ent->len because we have stripped the trailing '/' that read_directory adds. Additionaly match_one() was modified to allow a name[] that is not NUL terminated. This allows us to check if the name matched the pathspec exactly instead of recursively. Signed-off-by: Shawn Bohrer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f669ac0 commit f2d0df7

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

builtin-clean.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
9595

9696
for (i = 0; i < dir.nr; i++) {
9797
struct dir_entry *ent = dir.entries[i];
98-
int len, pos, matches;
98+
int len, pos;
99+
int matches = 0;
99100
struct cache_entry *ce;
100101
struct stat st;
101102

@@ -127,18 +128,18 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
127128

128129
if (pathspec) {
129130
memset(seen, 0, argc > 0 ? argc : 1);
130-
matches = match_pathspec(pathspec, ent->name, ent->len,
131+
matches = match_pathspec(pathspec, ent->name, len,
131132
baselen, seen);
132-
} else {
133-
matches = 0;
134133
}
135134

136135
if (S_ISDIR(st.st_mode)) {
137136
strbuf_addstr(&directory, ent->name);
138137
qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
139-
if (show_only && (remove_directories || matches)) {
138+
if (show_only && (remove_directories ||
139+
(matches == MATCHED_EXACTLY))) {
140140
printf("Would remove %s\n", qname);
141-
} else if (remove_directories || matches) {
141+
} else if (remove_directories ||
142+
(matches == MATCHED_EXACTLY)) {
142143
if (!quiet)
143144
printf("Removing %s\n", qname);
144145
if (remove_dir_recursively(&directory, 0) != 0) {

dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static int match_one(const char *match, const char *name, int namelen)
8080
if (strncmp(match, name, matchlen))
8181
return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
8282

83-
if (!name[matchlen])
83+
if (namelen == matchlen)
8484
return MATCHED_EXACTLY;
8585
if (match[matchlen-1] == '/' || name[matchlen] == '/')
8686
return MATCHED_RECURSIVELY;

0 commit comments

Comments
 (0)