]> git.ipfire.org Git - thirdparty/git.git/commitdiff
mingw: align symlinks-related rmdir() behavior with Linux
authorThomas Bétous <tomspycell@gmail.com>
Mon, 2 Aug 2021 21:07:30 +0000 (21:07 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 2 Aug 2021 22:10:58 +0000 (15:10 -0700)
When performing a rebase, rmdir() is called on the folder .git/logs. On
Unix rmdir() exits without deleting anything in case .git/logs is a
symbolic link but the equivalent functions on Windows (_rmdir, _wrmdir
and RemoveDirectoryW) do not behave the same and remove the folder if it
is symlinked even if it is not empty.

This creates issues when folders in .git/ are symlinks which is
especially the case when git-repo[1] is used: It replaces `.git/logs/`
with a symlink.

One such issue is that the _target_ of that symlink is removed e.g.
during a `git rebase`, where `delete_reflog("REBASE_HEAD")` will not
only try to remove `.git/logs/REBASE_HEAD` but then recursively try to
remove the parent directories until an error occurs, a technique that
obviously relies on `rmdir()` refusing to remove a symlink.

This was reported in https://github.com/git-for-windows/git/issues/2967.

This commit updates mingw_rmdir() so that its behavior is the same as
Linux rmdir() in case of symbolic links.

To verify that Git does not regress on the reported issue, this patch
adds a regression test for the `git rebase` symptom, even if the same
`rmdir()` behavior is quite likely to cause potential problems in other
Git commands as well.

[1]: git-repo is a python tool built on top of Git which helps manage
many Git repositories. It stores all the .git/ folders in a central
place by taking advantage of symbolic links.
More information: https://gerrit.googlesource.com/git-repo/

Signed-off-by: Thomas Bétous <tomspycell@gmail.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
compat/mingw.c
t/t3400-rebase.sh
t/test-lib.sh

index aa647b367b0fab393f5309bbb1dac3627383b1e5..9e0cd1e097f25f59ebc15fe583cdcd215c82bfcd 100644 (file)
@@ -341,6 +341,27 @@ int mingw_rmdir(const char *pathname)
 {
        int ret, tries = 0;
        wchar_t wpathname[MAX_PATH];
+       struct stat st;
+
+       /*
+        * Contrary to Linux' `rmdir()`, Windows' _wrmdir() and _rmdir()
+        * (and `RemoveDirectoryW()`) will attempt to remove the target of a
+        * symbolic link (if it points to a directory).
+        *
+        * This behavior breaks the assumption of e.g. `remove_path()` which
+        * upon successful deletion of a file will attempt to remove its parent
+        * directories recursively until failure (which usually happens when
+        * the directory is not empty).
+        *
+        * Therefore, before calling `_wrmdir()`, we first check if the path is
+        * a symbolic link. If it is, we exit and return the same error as
+        * Linux' `rmdir()` would, i.e. `ENOTDIR`.
+        */
+       if (!mingw_lstat(pathname, &st) && S_ISLNK(st.st_mode)) {
+               errno = ENOTDIR;
+               return -1;
+       }
+
        if (xutftowcs_path(wpathname, pathname) < 0)
                return -1;
 
index 0bb88aa982beeaab4d03f8d8b36093caa8cd7265..23dbd3c82ed6a21ff7bd883c20a7c45822421250 100755 (executable)
@@ -406,4 +406,14 @@ test_expect_success 'refuse to switch to branch checked out elsewhere' '
        test_i18ngrep "already checked out" err
 '
 
+test_expect_success MINGW,SYMLINKS_WINDOWS 'rebase when .git/logs is a symlink' '
+       git checkout main &&
+       mv .git/logs actual_logs &&
+       cmd //c "mklink /D .git\logs ..\actual_logs" &&
+       git rebase -f HEAD^ &&
+       test -L .git/logs &&
+       rm .git/logs &&
+       mv actual_logs .git/logs
+'
+
 test_done
index adaf03543e810ea26bdbb0c7add0f3a06b992bed..73f6d645b665bd54fbe4b82d5027d1689bae1378 100644 (file)
@@ -1513,6 +1513,12 @@ test_lazy_prereq SYMLINKS '
        ln -s x y && test -h y
 '
 
+test_lazy_prereq SYMLINKS_WINDOWS '
+       # test whether symbolic links are enabled on Windows
+       test_have_prereq MINGW &&
+       cmd //c "mklink y x" &> /dev/null && test -h y
+'
+
 test_lazy_prereq FILEMODE '
        test "$(git config --bool core.filemode)" = true
 '