Skip to content

Commit 0b40774

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 5a93880 commit 0b40774

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

compat/mingw.c

+63
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,69 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
11181118
}
11191119
#endif
11201120

1121+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1122+
{
1123+
wchar_t wpath[MAX_PATH];
1124+
HANDLE h;
1125+
DWORD ret;
1126+
int len;
1127+
const char *last_component = NULL;
1128+
1129+
if (xutftowcs_path(wpath, path) < 0)
1130+
return NULL;
1131+
1132+
h = CreateFileW(wpath, 0,
1133+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1134+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1135+
1136+
/*
1137+
* strbuf_realpath() allows the last path component to not exist. If
1138+
* that is the case, now it's time to try without last component.
1139+
*/
1140+
if (h == INVALID_HANDLE_VALUE &&
1141+
GetLastError() == ERROR_FILE_NOT_FOUND) {
1142+
/* cut last component off of `wpath` */
1143+
wchar_t *p = wpath + wcslen(wpath);
1144+
1145+
while (p != wpath)
1146+
if (*(--p) == L'/' || *p == L'\\')
1147+
break; /* found start of last component */
1148+
1149+
if (p != wpath && (last_component = find_last_dir_sep(path))) {
1150+
last_component++; /* skip directory separator */
1151+
*p = L'\0';
1152+
h = CreateFileW(wpath, 0, FILE_SHARE_READ |
1153+
FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1154+
NULL, OPEN_EXISTING,
1155+
FILE_FLAG_BACKUP_SEMANTICS, NULL);
1156+
}
1157+
}
1158+
1159+
if (h == INVALID_HANDLE_VALUE)
1160+
return NULL;
1161+
1162+
ret = GetFinalPathNameByHandleW(h, wpath, ARRAY_SIZE(wpath), 0);
1163+
CloseHandle(h);
1164+
if (!ret || ret >= ARRAY_SIZE(wpath))
1165+
return NULL;
1166+
1167+
len = wcslen(wpath) * 3;
1168+
strbuf_grow(resolved, len);
1169+
len = xwcstoutf(resolved->buf, normalize_ntpath(wpath), len);
1170+
if (len < 0)
1171+
return NULL;
1172+
resolved->len = len;
1173+
1174+
if (last_component) {
1175+
/* Use forward-slash, like `normalize_ntpath()` */
1176+
strbuf_addch(resolved, '/');
1177+
strbuf_addstr(resolved, last_component);
1178+
}
1179+
1180+
return resolved->buf;
1181+
1182+
}
1183+
11211184
char *mingw_getcwd(char *pointer, int len)
11221185
{
11231186
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];

compat/mingw.h

+3
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ static inline void convert_slashes(char *path)
452452
#define PATH_SEP ';'
453453
char *mingw_query_user_email(void);
454454
#define query_user_email mingw_query_user_email
455+
struct strbuf;
456+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path);
457+
#define platform_strbuf_realpath mingw_strbuf_realpath
455458
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
456459
#define PRIuMAX "I64u"
457460
#define PRId64 "I64d"

t/t3700-add.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
498498
git add "$downcased"
499499
'
500500

501-
test_expect_failure MINGW 'can add files via NTFS junctions' '
501+
test_expect_success MINGW 'can add files via NTFS junctions' '
502502
test_when_finished "cmd //c rmdir junction && rm -rf target" &&
503503
test_create_repo target &&
504504
cmd //c "mklink /j junction target" &&

t/t5601-clone.sh

+7
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)