Skip to content

Commit 351ea1c

Browse files
newrengitster
authored andcommitted
dir, clean: avoid disallowed behavior
dir.h documented quite clearly that DIR_SHOW_IGNORED and DIR_SHOW_IGNORED_TOO are mutually exclusive, with a big comment to this effect by the definition of both enum values. However, a command like git clean -fx $DIR would set both values for dir.flags. I _think_ it happened to work because: * As dir.h points out, DIR_KEEP_UNTRACKED_CONTENTS only takes effect if DIR_SHOW_IGNORED_TOO is set. * As coded, I believe DIR_SHOW_IGNORED would just happen to take precedence over DIR_SHOW_IGNORED_TOO in the code as currently constructed. Which is a long way of saying "we just got lucky". Fix clean.c to avoid setting these mutually exclusive values at the same time, and add a check to dir.c that will throw a BUG() to prevent anyone else from making this mistake. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e6c0be9 commit 351ea1c

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

builtin/clean.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
954954
remove_directories = 1;
955955
}
956956

957-
if (remove_directories)
957+
if (remove_directories && !ignored_only)
958958
dir.flags |= DIR_SHOW_IGNORED_TOO | DIR_KEEP_UNTRACKED_CONTENTS;
959959

960960
if (read_cache() < 0)

dir.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ int fill_directory(struct dir_struct *dir,
193193
const char *prefix;
194194
size_t prefix_len;
195195

196+
unsigned exclusive_flags = DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO;
197+
if ((dir->flags & exclusive_flags) == exclusive_flags)
198+
BUG("DIR_SHOW_IGNORED and DIR_SHOW_IGNORED_TOO are exclusive");
199+
196200
/*
197201
* Calculate common prefix for the pathspec, and
198202
* use that to optimize the directory walk

0 commit comments

Comments
 (0)