Skip to content

Commit 4c02531

Browse files
committed
Merge pull request #2504 from dscho/access-repo-via-junction
Handle `git add <file>` where <file> traverses an NTFS junction
2 parents 709283d + d4d3aee commit 4c02531

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed

abspath.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ static char *strbuf_realpath_1(struct strbuf *resolved, const char *path,
9393
goto error_out;
9494
}
9595

96+
if (platform_strbuf_realpath(resolved, path))
97+
return resolved->buf;
98+
9699
strbuf_addstr(&remaining, path);
97100
get_root_part(resolved, &remaining);
98101

compat/mingw.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,82 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
11461146
}
11471147
#endif
11481148

1149+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1150+
{
1151+
wchar_t wpath[MAX_PATH];
1152+
HANDLE h;
1153+
DWORD ret;
1154+
int len;
1155+
const char *last_component = NULL;
1156+
char *append = NULL;
1157+
1158+
if (xutftowcs_path(wpath, path) < 0)
1159+
return NULL;
1160+
1161+
h = CreateFileW(wpath, 0,
1162+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1163+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1164+
1165+
/*
1166+
* strbuf_realpath() allows the last path component to not exist. If
1167+
* that is the case, now it's time to try without last component.
1168+
*/
1169+
if (h == INVALID_HANDLE_VALUE &&
1170+
GetLastError() == ERROR_FILE_NOT_FOUND) {
1171+
/* cut last component off of `wpath` */
1172+
wchar_t *p = wpath + wcslen(wpath);
1173+
1174+
while (p != wpath)
1175+
if (*(--p) == L'/' || *p == L'\\')
1176+
break; /* found start of last component */
1177+
1178+
if (p != wpath && (last_component = find_last_dir_sep(path))) {
1179+
append = xstrdup(last_component + 1); /* skip directory separator */
1180+
/*
1181+
* Do not strip the trailing slash at the drive root, otherwise
1182+
* the path would be e.g. `C:` (which resolves to the
1183+
* _current_ directory on that drive).
1184+
*/
1185+
if (p[-1] == L':')
1186+
p[1] = L'\0';
1187+
else
1188+
*p = L'\0';
1189+
h = CreateFileW(wpath, 0, FILE_SHARE_READ |
1190+
FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1191+
NULL, OPEN_EXISTING,
1192+
FILE_FLAG_BACKUP_SEMANTICS, NULL);
1193+
}
1194+
}
1195+
1196+
if (h == INVALID_HANDLE_VALUE) {
1197+
realpath_failed:
1198+
FREE_AND_NULL(append);
1199+
return NULL;
1200+
}
1201+
1202+
ret = GetFinalPathNameByHandleW(h, wpath, ARRAY_SIZE(wpath), 0);
1203+
CloseHandle(h);
1204+
if (!ret || ret >= ARRAY_SIZE(wpath))
1205+
goto realpath_failed;
1206+
1207+
len = wcslen(wpath) * 3;
1208+
strbuf_grow(resolved, len);
1209+
len = xwcstoutf(resolved->buf, normalize_ntpath(wpath), len);
1210+
if (len < 0)
1211+
goto realpath_failed;
1212+
resolved->len = len;
1213+
1214+
if (append) {
1215+
/* Use forward-slash, like `normalize_ntpath()` */
1216+
strbuf_complete(resolved, '/');
1217+
strbuf_addstr(resolved, append);
1218+
FREE_AND_NULL(append);
1219+
}
1220+
1221+
return resolved->buf;
1222+
1223+
}
1224+
11491225
char *mingw_getcwd(char *pointer, int len)
11501226
{
11511227
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ int mingw_is_mount_point(struct strbuf *path);
461461
#define PATH_SEP ';'
462462
char *mingw_query_user_email(void);
463463
#define query_user_email mingw_query_user_email
464+
struct strbuf;
465+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path);
466+
#define platform_strbuf_realpath mingw_strbuf_realpath
464467
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
465468
#define PRIuMAX "I64u"
466469
#define PRId64 "I64d"

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,10 @@ static inline int git_has_dir_sep(const char *path)
602602
#define query_user_email() NULL
603603
#endif
604604

605+
#ifndef platform_strbuf_realpath
606+
#define platform_strbuf_realpath(resolved, path) NULL
607+
#endif
608+
605609
#ifdef __TANDEM
606610
#include <floss.h(floss_execl,floss_execlp,floss_execv,floss_execvp)>
607611
#include <floss.h(floss_getpwuid)>

t/t0060-path-utils.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ test_expect_success SYMLINKS 'real path works on symlinks' '
282282
test_cmp expect actual
283283
'
284284

285+
test_expect_success MINGW 'real path works near drive root' '
286+
# we need a non-existing path at the drive root; simply skip if C:/xyz exists
287+
if test ! -e C:/xyz
288+
then
289+
test C:/xyz = $(test-tool path-utils real_path C:/xyz)
290+
fi
291+
'
292+
285293
test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
286294
ln -s target symlink &&
287295
echo "symlink" >expect &&

t/t3700-add.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,15 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
506506
git add "$downcased"
507507
'
508508

509+
test_expect_success MINGW 'can add files via NTFS junctions' '
510+
test_when_finished "cmd //c rmdir junction && rm -rf target" &&
511+
test_create_repo target &&
512+
cmd //c "mklink /j junction target" &&
513+
>target/via-junction &&
514+
git -C junction add "$(pwd)/junction/via-junction" &&
515+
echo via-junction >expect &&
516+
git -C target diff --cached --name-only >actual &&
517+
test_cmp expect actual
518+
'
519+
509520
test_done

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)