]> git.ipfire.org Git - thirdparty/git.git/commitdiff
unpack-trees: refuse to remove startup_info->original_cwd
authorElijah Newren <newren@gmail.com>
Thu, 9 Dec 2021 05:08:27 +0000 (05:08 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 9 Dec 2021 21:33:12 +0000 (13:33 -0800)
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 <stolee@gmail.com>
Acked-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t2501-cwd-empty.sh
unpack-trees.c
unpack-trees.h

index a05abd181874328ba0a0cbf7f73c94cd25dd6e4c..398908dfc936c3d53c4add00f333c10fdf11caf8 100755 (executable)
@@ -113,7 +113,7 @@ test_expect_success 'checkout does not clean cwd incidentally' '
 '
 
 test_expect_success 'checkout fails if cwd needs to be removed' '
-       test_required_dir_removal failure git checkout fd_conflict
+       test_required_dir_removal success git checkout fd_conflict
 '
 
 test_expect_success 'reset --hard does not clean cwd incidentally' '
@@ -144,23 +144,17 @@ test_expect_success 'merge fails if cwd needs to be removed; recursive friendly'
        (
                cd dirORfile &&
 
-               # We would rather this failed, but we test for existing
-               # rather than desired behavior
-               git merge fd_conflict 2>../error
+               test_must_fail git merge fd_conflict 2>../error
        ) &&
 
-       ## Here is the behavior we would rather have:
-       #test_path_is_dir dirORfile &&
-       #grep "Refusing to remove the current working directory" error
-       ## But instead we test for existing behavior
-       test_path_is_file dirORfile &&
-       test_must_be_empty error
+       test_path_is_dir dirORfile &&
+       grep "Refusing to remove the current working directory" error
 '
 
 GIT_TEST_MERGE_ALGORITHM=ort
 
 test_expect_success 'merge fails if cwd needs to be removed' '
-       test_required_dir_removal failure git merge fd_conflict
+       test_required_dir_removal success git merge fd_conflict
 '
 
 test_expect_success 'cherry-pick does not clean cwd incidentally' '
@@ -168,7 +162,7 @@ test_expect_success 'cherry-pick does not clean cwd incidentally' '
 '
 
 test_expect_success 'cherry-pick fails if cwd needs to be removed' '
-       test_required_dir_removal failure git cherry-pick fd_conflict
+       test_required_dir_removal success git cherry-pick fd_conflict
 '
 
 test_expect_success 'rebase does not clean cwd incidentally' '
@@ -184,7 +178,7 @@ test_expect_success 'revert does not clean cwd incidentally' '
 '
 
 test_expect_success 'revert fails if cwd needs to be removed' '
-       test_required_dir_removal failure git revert undo_fd_conflict
+       test_required_dir_removal success git revert undo_fd_conflict
 '
 
 test_expect_success 'rm does not clean cwd incidentally' '
index 89ca95ce90b369bc521fc52fc9b071c467a74022..6bc16f3a714bde74f468608d98988d87caf43765 100644 (file)
@@ -36,6 +36,9 @@ static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = {
        /* ERROR_NOT_UPTODATE_DIR */
        "Updating '%s' would lose untracked files in it",
 
+       /* ERROR_CWD_IN_THE_WAY */
+       "Refusing to remove '%s' since it is the current working directory.",
+
        /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
        "Untracked working tree file '%s' would be overwritten by merge.",
 
@@ -131,6 +134,9 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
        msgs[ERROR_NOT_UPTODATE_DIR] =
                _("Updating the following directories would lose untracked files in them:\n%s");
 
+       msgs[ERROR_CWD_IN_THE_WAY] =
+               _("Refusing to remove the current working directory:\n%s");
+
        if (!strcmp(cmd, "checkout"))
                msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
                      ? _("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,
                cnt++;
        }
 
-       /*
-        * Then we need to make sure that we do not lose a locally
-        * present file that is not ignored.
-        */
+       /* Do not lose a locally present file that is not ignored. */
        pathbuf = xstrfmt("%.*s/", namelen, ce->name);
 
        memset(&d, 0, sizeof(d));
@@ -2160,6 +2163,12 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
        free(pathbuf);
        if (i)
                return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
+
+       /* Do not lose startup_info->original_cwd */
+       if (startup_info->original_cwd &&
+           !strcmp(startup_info->original_cwd, ce->name))
+               return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, ce->name);
+
        return cnt;
 }
 
index 71ffb7eeb0c0d1df8539cdff4d6383116d4ff1b4..efb9edfbb2717b4739247ecdd58a104e1d44cfd2 100644 (file)
@@ -19,6 +19,7 @@ enum unpack_trees_error_types {
        ERROR_WOULD_OVERWRITE = 0,
        ERROR_NOT_UPTODATE_FILE,
        ERROR_NOT_UPTODATE_DIR,
+       ERROR_CWD_IN_THE_WAY,
        ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN,
        ERROR_WOULD_LOSE_UNTRACKED_REMOVED,
        ERROR_BIND_OVERLAP,