Skip to content

Commit c0153d5

Browse files
authored
Merge pull request #3328 from thombet/fix-rmdir-with-symlinks-on-windows
rmdir: align behavior on Windows when called on symlinks
2 parents 0d71536 + 7620222 commit c0153d5

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
@@ -1536,6 +1536,12 @@ test_lazy_prereq SYMLINKS '
15361536
ln -s x y && test -h y
15371537
'
15381538

1539+
test_lazy_prereq SYMLINKS_WINDOWS '
1540+
# test whether symbolic links are enabled on Windows
1541+
test_have_prereq MINGW &&
1542+
cmd //c "mklink y x" &> /dev/null && test -h y
1543+
'
1544+
15391545
test_lazy_prereq FILEMODE '
15401546
test "$(git config --bool core.filemode)" = true
15411547
'

0 commit comments

Comments
 (0)