Skip to content

Commit

Permalink
rmdir: align behavior on Windows when called on symlinks
Browse files Browse the repository at this point in the history
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 <tomspycell@gmail.com>
  • Loading branch information
thombet authored and Git for Windows Build Agent committed Jul 29, 2021
1 parent 4d8a542 commit 1a6f48e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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 @@ -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
'
Expand Down

0 comments on commit 1a6f48e

Please sign in to comment.