Skip to content

Commit 2ceeffd

Browse files
thombetdscho
authored andcommitted
rmdir: align behavior on Windows when called on symlinks
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 symlink even if it is not empty. It generates issues when folders in .git/ are symlinks which is especially the case when git-repo[1] is used. This commit updates mingw_rmdir() so that its behavior is the same as Linux rmdir() in case of symbolic links. [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 <[email protected]>
1 parent 7d76734 commit 2ceeffd

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

compat/mingw.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,25 @@ static int is_dir_empty(const wchar_t *wpath)
555555
int mingw_rmdir(const char *pathname)
556556
{
557557
int tries = 0;
558+
struct stat sb;
559+
558560
wchar_t wpathname[MAX_LONG_PATH];
559561
if (xutftowcs_long_path(wpathname, pathname) < 0)
560562
return -1;
561563

564+
/*
565+
* Contrary to Linux rmdir(), Windows' _wrmdir() and _rmdir()
566+
* will remove the directory at the path if it is a symbolic link
567+
* which leads to issues when symlinks are used in the .git folder
568+
* (in the context of git-repo for instance). So before calling _wrmdir()
569+
* we first check if the path is a symbolic link. If it is, we exit
570+
* and return the same error as Linux rmdir() in this case (ENOTDIR).
571+
*/
572+
if (!mingw_lstat(pathname, &sb) && S_ISLNK(sb.st_mode)) {
573+
errno = ENOTDIR;
574+
return -1;
575+
}
576+
562577
do {
563578
if (!_wrmdir(wpathname)) {
564579
invalidate_lstat_cache();

t/t3400-rebase.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,14 @@ test_expect_success 'refuse to switch to branch checked out elsewhere' '
406406
test_i18ngrep "already checked out" err
407407
'
408408

409+
test_expect_success MINGW,SYMLINKS_WINDOWS 'rebase when .git/logs is a symlink' '
410+
git checkout main &&
411+
mv .git/logs actual_logs &&
412+
cmd //c "mklink /D .git\logs ..\actual_logs" &&
413+
git rebase -f HEAD^ &&
414+
test -L .git/logs &&
415+
rm .git/logs &&
416+
mv actual_logs .git/logs
417+
'
418+
409419
test_done

t/test-lib.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,12 @@ test_lazy_prereq SYMLINKS '
15681568
ln -s x y && test -h y
15691569
'
15701570

1571+
test_lazy_prereq SYMLINKS_WINDOWS '
1572+
# test whether symbolic links are enabled on Windows
1573+
test_have_prereq MINGW &&
1574+
cmd //c "mklink y x" &> /dev/null && test -h y
1575+
'
1576+
15711577
test_lazy_prereq FILEMODE '
15721578
test "$(git config --bool core.filemode)" = true
15731579
'

0 commit comments

Comments
 (0)