Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 9526473

Browse files
committed
Merge branch 'jk/clean-d-pathspec' into maint
"git clean -d pathspec" did not use the given pathspec correctly and ended up cleaning too much. * jk/clean-d-pathspec: clean: simplify dir/not-dir logic clean: respect pathspecs with "-d"
2 parents 01e13d0 + 1f2e108 commit 9526473

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

builtin/clean.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -946,17 +946,15 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
946946
if (pathspec.nr)
947947
matches = dir_path_match(ent, &pathspec, 0, NULL);
948948

949-
if (S_ISDIR(st.st_mode)) {
950-
if (remove_directories || (matches == MATCHED_EXACTLY)) {
951-
rel = relative_path(ent->name, prefix, &buf);
952-
string_list_append(&del_list, rel);
953-
}
954-
} else {
955-
if (pathspec.nr && !matches)
956-
continue;
957-
rel = relative_path(ent->name, prefix, &buf);
958-
string_list_append(&del_list, rel);
959-
}
949+
if (pathspec.nr && !matches)
950+
continue;
951+
952+
if (S_ISDIR(st.st_mode) && !remove_directories &&
953+
matches != MATCHED_EXACTLY)
954+
continue;
955+
956+
rel = relative_path(ent->name, prefix, &buf);
957+
string_list_append(&del_list, rel);
960958
}
961959

962960
if (interactive && del_list.nr > 0)

t/t7300-clean.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,4 +511,20 @@ test_expect_success SANITY 'git clean -d with an unreadable empty directory' '
511511
! test -d foo
512512
'
513513

514+
test_expect_success 'git clean -d respects pathspecs (dir is prefix of pathspec)' '
515+
mkdir -p foo &&
516+
mkdir -p foobar &&
517+
git clean -df foobar &&
518+
test_path_is_dir foo &&
519+
test_path_is_missing foobar
520+
'
521+
522+
test_expect_success 'git clean -d respects pathspecs (pathspec is prefix of dir)' '
523+
mkdir -p foo &&
524+
mkdir -p foobar &&
525+
git clean -df foo &&
526+
test_path_is_missing foo &&
527+
test_path_is_dir foobar
528+
'
529+
514530
test_done

0 commit comments

Comments
 (0)