Skip to content

Commit 16dd42f

Browse files
committed
mingw: implement a platform-specific strbuf_realpath()
There is a Win32 API function to resolve symbolic links, and we can use that instead of resolving them manually. Even better, this function also resolves NTFS junction points (which are somewhat similar to bind mounts). This fixes #2481. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 32a7ed7 commit 16dd42f

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

compat/mingw.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,69 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
10721072
return NULL;
10731073
}
10741074

1075+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1076+
{
1077+
wchar_t wpath[MAX_PATH];
1078+
HANDLE h;
1079+
DWORD ret;
1080+
int len;
1081+
const char *last_component = NULL;
1082+
1083+
if (xutftowcs_path(wpath, path) < 0)
1084+
return NULL;
1085+
1086+
h = CreateFileW(wpath, 0,
1087+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1088+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1089+
1090+
/*
1091+
* strbuf_realpath() allows the last path component to not exist. If
1092+
* that is the case, now it's time to try without last component.
1093+
*/
1094+
if (h == INVALID_HANDLE_VALUE &&
1095+
GetLastError() == ERROR_FILE_NOT_FOUND) {
1096+
/* cut last component off of `wpath` */
1097+
wchar_t *p = wpath + wcslen(wpath);
1098+
1099+
while (p != wpath)
1100+
if (*(--p) == L'/' || *p == L'\\')
1101+
break; /* found start of last component */
1102+
1103+
if (p != wpath && (last_component = find_last_dir_sep(path))) {
1104+
last_component++; /* skip directory separator */
1105+
*p = L'\0';
1106+
h = CreateFileW(wpath, 0, FILE_SHARE_READ |
1107+
FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1108+
NULL, OPEN_EXISTING,
1109+
FILE_FLAG_BACKUP_SEMANTICS, NULL);
1110+
}
1111+
}
1112+
1113+
if (h == INVALID_HANDLE_VALUE)
1114+
return NULL;
1115+
1116+
ret = GetFinalPathNameByHandleW(h, wpath, ARRAY_SIZE(wpath), 0);
1117+
CloseHandle(h);
1118+
if (!ret || ret >= ARRAY_SIZE(wpath))
1119+
return NULL;
1120+
1121+
len = wcslen(wpath) * 3;
1122+
strbuf_grow(resolved, len);
1123+
len = xwcstoutf(resolved->buf, normalize_ntpath(wpath), len);
1124+
if (len < 0)
1125+
return NULL;
1126+
resolved->len = len;
1127+
1128+
if (last_component) {
1129+
/* Use forward-slash, like `normalize_ntpath()` */
1130+
strbuf_addch(resolved, '/');
1131+
strbuf_addstr(resolved, last_component);
1132+
}
1133+
1134+
return resolved->buf;
1135+
1136+
}
1137+
10751138
char *mingw_getcwd(char *pointer, int len)
10761139
{
10771140
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ static inline void convert_slashes(char *path)
445445
#define PATH_SEP ';'
446446
char *mingw_query_user_email(void);
447447
#define query_user_email mingw_query_user_email
448+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path);
449+
#define platform_strbuf_realpath mingw_strbuf_realpath
448450
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
449451
#define PRIuMAX "I64u"
450452
#define PRId64 "I64d"

t/t3700-add.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
423423
git add "$downcased"
424424
'
425425

426-
test_expect_failure MINGW 'can add files via NTFS junctions' '
426+
test_expect_success MINGW 'can add files via NTFS junctions' '
427427
test_when_finished "cmd //c rmdir junction && rm -rf target" &&
428428
test_create_repo target &&
429429
cmd //c "mklink /j junction target" &&

t/t5601-clone.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ test_expect_success 'clone respects GIT_WORK_TREE' '
7171
7272
'
7373

74+
test_expect_success CASE_INSENSITIVE_FS 'core.worktree is not added due to path case' '
75+
76+
mkdir UPPERCASE &&
77+
git clone src "$(pwd)/uppercase" &&
78+
test "unset" = "$(git -C UPPERCASE config --default unset core.worktree)"
79+
'
80+
7481
test_expect_success 'clone from hooks' '
7582
7683
test_create_repo r0 &&

0 commit comments

Comments
 (0)