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 <shawn.bohrer@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Shawn Bohrer
2008-04-14 22:14:09 -05:00
committed by Junio C Hamano
parent f669ac0be9
commit f2d0df7148
2 changed files with 8 additions and 7 deletions

View File

@ -95,7 +95,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
for (i = 0; i < dir.nr; i++) { for (i = 0; i < dir.nr; i++) {
struct dir_entry *ent = dir.entries[i]; struct dir_entry *ent = dir.entries[i];
int len, pos, matches; int len, pos;
int matches = 0;
struct cache_entry *ce; struct cache_entry *ce;
struct stat st; struct stat st;
@ -127,18 +128,18 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
if (pathspec) { if (pathspec) {
memset(seen, 0, argc > 0 ? argc : 1); memset(seen, 0, argc > 0 ? argc : 1);
matches = match_pathspec(pathspec, ent->name, ent->len, matches = match_pathspec(pathspec, ent->name, len,
baselen, seen); baselen, seen);
} else {
matches = 0;
} }
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
strbuf_addstr(&directory, ent->name); strbuf_addstr(&directory, ent->name);
qname = quote_path_relative(directory.buf, directory.len, &buf, prefix); qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
if (show_only && (remove_directories || matches)) { if (show_only && (remove_directories ||
(matches == MATCHED_EXACTLY))) {
printf("Would remove %s\n", qname); printf("Would remove %s\n", qname);
} else if (remove_directories || matches) { } else if (remove_directories ||
(matches == MATCHED_EXACTLY)) {
if (!quiet) if (!quiet)
printf("Removing %s\n", qname); printf("Removing %s\n", qname);
if (remove_dir_recursively(&directory, 0) != 0) { if (remove_dir_recursively(&directory, 0) != 0) {

2
dir.c
View File

@ -80,7 +80,7 @@ static int match_one(const char *match, const char *name, int namelen)
if (strncmp(match, name, matchlen)) if (strncmp(match, name, matchlen))
return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0; return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
if (!name[matchlen]) if (namelen == matchlen)
return MATCHED_EXACTLY; return MATCHED_EXACTLY;
if (match[matchlen-1] == '/' || name[matchlen] == '/') if (match[matchlen-1] == '/' || name[matchlen] == '/')
return MATCHED_RECURSIVELY; return MATCHED_RECURSIVELY;