Skip to content

Commit

Permalink
Merge pull request #3328 from thombet/fix-rmdir-with-symlinks-on-windows
Browse files Browse the repository at this point in the history
rmdir: align behavior on Windows when called on symlinks
  • Loading branch information
dscho committed Aug 11, 2021
2 parents 7965e41 + 752e6f4 commit d019b3f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 10 additions & 0 deletions t/t3400-rebase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions t/test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
'
Expand Down

0 comments on commit d019b3f

Please sign in to comment.