diff --git a/compat/mingw.c b/compat/mingw.c index 64e0c86579a5f0..72dcbd733cab1a 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -555,10 +555,25 @@ static int is_dir_empty(const wchar_t *wpath) int mingw_rmdir(const char *pathname) { int tries = 0; + struct stat sb; + wchar_t wpathname[MAX_LONG_PATH]; if (xutftowcs_long_path(wpathname, pathname) < 0) return -1; + /* + * Contrary to Linux rmdir(), Windows' _wrmdir() and _rmdir() + * will remove the directory at the path if it is a symbolic link + * which leads to issues when symlinks are used in the .git folder + * (in the context of git-repo for instance). So 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() in this case (ENOTDIR). + */ + if (!mingw_lstat(pathname, &sb) && S_ISLNK(sb.st_mode)) { + errno = ENOTDIR; + return -1; + } + do { if (!_wrmdir(wpathname)) { invalidate_lstat_cache(); diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh index 0bb88aa982beea..23dbd3c82ed6a2 100755 --- a/t/t3400-rebase.sh +++ b/t/t3400-rebase.sh @@ -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 diff --git a/t/test-lib.sh b/t/test-lib.sh index 916d0a6b62fc13..d7e2315b4b9b61 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -1568,6 +1568,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 '