Skip to content

Commit

Permalink
Merge pull request #2731 from SyntevoAlex/#312(win)_clone_adds_worktree
Browse files Browse the repository at this point in the history
Fix problem where clone adds core.worktree due to path case differences
  • Loading branch information
dscho committed Jul 10, 2020
2 parents d8257be + 37f9146 commit 3d77973
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
32 changes: 32 additions & 0 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1293,13 +1293,38 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
HANDLE h;
DWORD ret;
int len;
const char *last_component = NULL;

if (xutftowcs_path(wpath, path) < 0)
return NULL;

h = CreateFileW(wpath, 0,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);

/*
* strbuf_realpath() allows the last path component to not exist. If
* that is the case, now it's time to try without last component.
*/
if (h == INVALID_HANDLE_VALUE &&
GetLastError() == ERROR_FILE_NOT_FOUND) {
/* cut last component off of `wpath` */
wchar_t *p = wpath + wcslen(wpath);

while (p != wpath)
if (*(--p) == L'/' || *p == L'\\')
break; /* found start of last component */

if (p != wpath && (last_component = find_last_dir_sep(path))) {
last_component++; /* skip directory separator */
*p = L'\0';
h = CreateFileW(wpath, 0, FILE_SHARE_READ |
FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
}
}

if (h == INVALID_HANDLE_VALUE)
return NULL;

Expand All @@ -1314,6 +1339,13 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
if (len < 0)
return NULL;
resolved->len = len;

if (last_component) {
/* Use forward-slash, like `normalize_ntpath()` */
strbuf_addch(resolved, '/');
strbuf_addstr(resolved, last_component);
}

return resolved->buf;

}
Expand Down
7 changes: 7 additions & 0 deletions t/t5601-clone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ test_expect_success 'clone respects GIT_WORK_TREE' '
'

test_expect_success CASE_INSENSITIVE_FS 'core.worktree is not added due to path case' '
mkdir UPPERCASE &&
git clone src "$(pwd)/uppercase" &&
test "unset" = "$(git -C UPPERCASE config --default unset core.worktree)"
'

test_expect_success 'clone from hooks' '
test_create_repo r0 &&
Expand Down

0 comments on commit 3d77973

Please sign in to comment.