From: Elijah Newren <newren@xxxxxxxxx> When running commands such as `git reset --hard` from a subdirectory, if that subdirectory is in the way of adding needed files, bail with an error message. Note that this change looks kind of like it duplicates the new lines of code from the previous commit in verify_clean_subdirectory(). However, when we are preserving untracked files, we would rather any error messages about untracked files being in the way take precedence over error messages about a subdirectory that happens to be the current working directory being in the way. But in the UNPACK_RESET_OVERWRITE_UNTRACKED case, there is no untracked checking to be done, so we simply add a special case near the top of verify_absent_1. Signed-off-by: Elijah Newren <newren@xxxxxxxxx> --- t/t2501-cwd-empty.sh | 2 +- unpack-trees.c | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/t/t2501-cwd-empty.sh b/t/t2501-cwd-empty.sh index 212676d71c3..526d8ec2ee3 100755 --- a/t/t2501-cwd-empty.sh +++ b/t/t2501-cwd-empty.sh @@ -67,7 +67,7 @@ test_expect_failure 'reset --hard does not clean cwd incidentally' ' test_path_is_dir foo ' -test_expect_failure 'reset --hard fails if cwd needs to be removed' ' +test_expect_success 'reset --hard fails if cwd needs to be removed' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && diff --git a/unpack-trees.c b/unpack-trees.c index 8879b8d6c8c..d1ce4f4a35b 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -2260,10 +2260,18 @@ static int verify_absent_1(const struct cache_entry *ce, int len; struct stat st; - if (o->index_only || !o->update || - o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) + if (o->index_only || !o->update) return 0; + if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) { + /* Avoid nuking cwd... */ + if (the_cwd && !strcmp(the_cwd, ce->name)) + return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, + ce->name); + /* ...but nuke anything else. */ + return 0; + } + len = check_leading_path(ce->name, ce_namelen(ce), 0); if (!len) return 0; -- gitgitgadget