Skip to content

Commit 0b0ee33

Browse files
newrengitster
authored andcommitted
unpack-trees: add special cwd handling
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_original_cwd 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. Acked-by: Derrick Stolee <[email protected]> Acked-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b817e54 commit 0b0ee33

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

t/t2501-cwd-empty.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ test_expect_success 'reset --hard does not clean cwd incidentally' '
121121
'
122122

123123
test_expect_success 'reset --hard fails if cwd needs to be removed' '
124-
test_required_dir_removal failure git reset --hard fd_conflict
124+
test_required_dir_removal success git reset --hard fd_conflict
125125
'
126126

127127
test_expect_success 'merge does not clean cwd incidentally' '

unpack-trees.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,10 +2261,19 @@ static int verify_absent_1(const struct cache_entry *ce,
22612261
int len;
22622262
struct stat st;
22632263

2264-
if (o->index_only || !o->update ||
2265-
o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED)
2264+
if (o->index_only || !o->update)
22662265
return 0;
22672266

2267+
if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) {
2268+
/* Avoid nuking startup_info->original_cwd... */
2269+
if (startup_info->original_cwd &&
2270+
!strcmp(startup_info->original_cwd, ce->name))
2271+
return add_rejected_path(o, ERROR_CWD_IN_THE_WAY,
2272+
ce->name);
2273+
/* ...but nuke anything else. */
2274+
return 0;
2275+
}
2276+
22682277
len = check_leading_path(ce->name, ce_namelen(ce), 0);
22692278
if (!len)
22702279
return 0;

0 commit comments

Comments
 (0)