Skip to content

Commit 4492f44

Browse files
committed
Merge pull request #2731 from SyntevoAlex/#312(win)_clone_adds_worktree
Fix problem where clone adds core.worktree due to path case differences
2 parents a6c4038 + 9bbadbe commit 4492f44

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

compat/mingw.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,13 +1271,38 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
12711271
HANDLE h;
12721272
DWORD ret;
12731273
int len;
1274+
const char *last_component = NULL;
12741275

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

12781279
h = CreateFileW(wpath, 0,
12791280
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
12801281
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1282+
1283+
/*
1284+
* strbuf_realpath() allows the last path component to not exist. If
1285+
* that is the case, now it's time to try without last component.
1286+
*/
1287+
if (h == INVALID_HANDLE_VALUE &&
1288+
GetLastError() == ERROR_FILE_NOT_FOUND) {
1289+
/* cut last component off of `wpath` */
1290+
wchar_t *p = wpath + wcslen(wpath);
1291+
1292+
while (p != wpath)
1293+
if (*(--p) == L'/' || *p == L'\\')
1294+
break; /* found start of last component */
1295+
1296+
if (p != wpath && (last_component = find_last_dir_sep(path))) {
1297+
last_component++; /* skip directory separator */
1298+
*p = L'\0';
1299+
h = CreateFileW(wpath, 0, FILE_SHARE_READ |
1300+
FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1301+
NULL, OPEN_EXISTING,
1302+
FILE_FLAG_BACKUP_SEMANTICS, NULL);
1303+
}
1304+
}
1305+
12811306
if (h == INVALID_HANDLE_VALUE)
12821307
return NULL;
12831308

@@ -1292,6 +1317,13 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
12921317
if (len < 0)
12931318
return NULL;
12941319
resolved->len = len;
1320+
1321+
if (last_component) {
1322+
/* Use forward-slash, like `normalize_ntpath()` */
1323+
strbuf_addch(resolved, '/');
1324+
strbuf_addstr(resolved, last_component);
1325+
}
1326+
12951327
return resolved->buf;
12961328

12971329
}

t/t5601-clone.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ test_expect_success 'clone respects GIT_WORK_TREE' '
6868
6969
'
7070

71+
test_expect_success CASE_INSENSITIVE_FS 'core.worktree is not added due to path case' '
72+
73+
mkdir UPPERCASE &&
74+
git clone src "$(pwd)/uppercase" &&
75+
test "unset" = "$(git -C UPPERCASE config --default unset core.worktree)"
76+
'
77+
7178
test_expect_success 'clone from hooks' '
7279
7380
test_create_repo r0 &&

0 commit comments

Comments
 (0)