Skip to content

Commit b817e54

Browse files
newrengitster
authored andcommitted
unpack-trees: refuse to remove startup_info->original_cwd
In the past, when a directory needs to be removed to make room for a file, we have always errored out when that directory contains any untracked (but not ignored) files. Add an extra condition on that: also error out if the directory is the current working directory we inherited from our parent process. 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 e6f8861 commit b817e54

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

t/t2501-cwd-empty.sh

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ test_expect_success 'checkout does not clean cwd incidentally' '
113113
'
114114

115115
test_expect_success 'checkout fails if cwd needs to be removed' '
116-
test_required_dir_removal failure git checkout fd_conflict
116+
test_required_dir_removal success git checkout fd_conflict
117117
'
118118

119119
test_expect_success 'reset --hard does not clean cwd incidentally' '
@@ -144,31 +144,25 @@ test_expect_success 'merge fails if cwd needs to be removed; recursive friendly'
144144
(
145145
cd dirORfile &&
146146
147-
# We would rather this failed, but we test for existing
148-
# rather than desired behavior
149-
git merge fd_conflict 2>../error
147+
test_must_fail git merge fd_conflict 2>../error
150148
) &&
151149
152-
## Here is the behavior we would rather have:
153-
#test_path_is_dir dirORfile &&
154-
#grep "Refusing to remove the current working directory" error
155-
## But instead we test for existing behavior
156-
test_path_is_file dirORfile &&
157-
test_must_be_empty error
150+
test_path_is_dir dirORfile &&
151+
grep "Refusing to remove the current working directory" error
158152
'
159153

160154
GIT_TEST_MERGE_ALGORITHM=ort
161155

162156
test_expect_success 'merge fails if cwd needs to be removed' '
163-
test_required_dir_removal failure git merge fd_conflict
157+
test_required_dir_removal success git merge fd_conflict
164158
'
165159

166160
test_expect_success 'cherry-pick does not clean cwd incidentally' '
167161
test_incidental_dir_removal failure git cherry-pick reverted
168162
'
169163

170164
test_expect_success 'cherry-pick fails if cwd needs to be removed' '
171-
test_required_dir_removal failure git cherry-pick fd_conflict
165+
test_required_dir_removal success git cherry-pick fd_conflict
172166
'
173167

174168
test_expect_success 'rebase does not clean cwd incidentally' '
@@ -184,7 +178,7 @@ test_expect_success 'revert does not clean cwd incidentally' '
184178
'
185179

186180
test_expect_success 'revert fails if cwd needs to be removed' '
187-
test_required_dir_removal failure git revert undo_fd_conflict
181+
test_required_dir_removal success git revert undo_fd_conflict
188182
'
189183

190184
test_expect_success 'rm does not clean cwd incidentally' '

unpack-trees.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = {
3636
/* ERROR_NOT_UPTODATE_DIR */
3737
"Updating '%s' would lose untracked files in it",
3838

39+
/* ERROR_CWD_IN_THE_WAY */
40+
"Refusing to remove '%s' since it is the current working directory.",
41+
3942
/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
4043
"Untracked working tree file '%s' would be overwritten by merge.",
4144

@@ -131,6 +134,9 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
131134
msgs[ERROR_NOT_UPTODATE_DIR] =
132135
_("Updating the following directories would lose untracked files in them:\n%s");
133136

137+
msgs[ERROR_CWD_IN_THE_WAY] =
138+
_("Refusing to remove the current working directory:\n%s");
139+
134140
if (!strcmp(cmd, "checkout"))
135141
msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
136142
? _("The following untracked working tree files would be removed by checkout:\n%%s"
@@ -2146,10 +2152,7 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
21462152
cnt++;
21472153
}
21482154

2149-
/*
2150-
* Then we need to make sure that we do not lose a locally
2151-
* present file that is not ignored.
2152-
*/
2155+
/* Do not lose a locally present file that is not ignored. */
21532156
pathbuf = xstrfmt("%.*s/", namelen, ce->name);
21542157

21552158
memset(&d, 0, sizeof(d));
@@ -2160,6 +2163,12 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
21602163
free(pathbuf);
21612164
if (i)
21622165
return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
2166+
2167+
/* Do not lose startup_info->original_cwd */
2168+
if (startup_info->original_cwd &&
2169+
!strcmp(startup_info->original_cwd, ce->name))
2170+
return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, ce->name);
2171+
21632172
return cnt;
21642173
}
21652174

unpack-trees.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ enum unpack_trees_error_types {
1919
ERROR_WOULD_OVERWRITE = 0,
2020
ERROR_NOT_UPTODATE_FILE,
2121
ERROR_NOT_UPTODATE_DIR,
22+
ERROR_CWD_IN_THE_WAY,
2223
ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN,
2324
ERROR_WOULD_LOSE_UNTRACKED_REMOVED,
2425
ERROR_BIND_OVERLAP,

0 commit comments

Comments
 (0)