Skip to content

Commit dbaf0e4

Browse files
dschomjcheetham
authored andcommitted
Merge pull request #2504 from dscho/access-repo-via-junction
Handle `git add <file>` where <file> traverses an NTFS junction
2 parents ebe257c + 5b3c9c9 commit dbaf0e4

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
@@ -1133,6 +1133,82 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
11331133
}
11341134
#endif
11351135

1136+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1137+
{
1138+
wchar_t wpath[MAX_PATH];
1139+
HANDLE h;
1140+
DWORD ret;
1141+
int len;
1142+
const char *last_component = NULL;
1143+
char *append = NULL;
1144+
1145+
if (xutftowcs_path(wpath, path) < 0)
1146+
return NULL;
1147+
1148+
h = CreateFileW(wpath, 0,
1149+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1150+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1151+
1152+
/*
1153+
* strbuf_realpath() allows the last path component to not exist. If
1154+
* that is the case, now it's time to try without last component.
1155+
*/
1156+
if (h == INVALID_HANDLE_VALUE &&
1157+
GetLastError() == ERROR_FILE_NOT_FOUND) {
1158+
/* cut last component off of `wpath` */
1159+
wchar_t *p = wpath + wcslen(wpath);
1160+
1161+
while (p != wpath)
1162+
if (*(--p) == L'/' || *p == L'\\')
1163+
break; /* found start of last component */
1164+
1165+
if (p != wpath && (last_component = find_last_dir_sep(path))) {
1166+
append = xstrdup(last_component + 1); /* skip directory separator */
1167+
/*
1168+
* Do not strip the trailing slash at the drive root, otherwise
1169+
* the path would be e.g. `C:` (which resolves to the
1170+
* _current_ directory on that drive).
1171+
*/
1172+
if (p[-1] == L':')
1173+
p[1] = L'\0';
1174+
else
1175+
*p = L'\0';
1176+
h = CreateFileW(wpath, 0, FILE_SHARE_READ |
1177+
FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1178+
NULL, OPEN_EXISTING,
1179+
FILE_FLAG_BACKUP_SEMANTICS, NULL);
1180+
}
1181+
}
1182+
1183+
if (h == INVALID_HANDLE_VALUE) {
1184+
realpath_failed:
1185+
FREE_AND_NULL(append);
1186+
return NULL;
1187+
}
1188+
1189+
ret = GetFinalPathNameByHandleW(h, wpath, ARRAY_SIZE(wpath), 0);
1190+
CloseHandle(h);
1191+
if (!ret || ret >= ARRAY_SIZE(wpath))
1192+
goto realpath_failed;
1193+
1194+
len = wcslen(wpath) * 3;
1195+
strbuf_grow(resolved, len);
1196+
len = xwcstoutf(resolved->buf, normalize_ntpath(wpath), len);
1197+
if (len < 0)
1198+
goto realpath_failed;
1199+
resolved->len = len;
1200+
1201+
if (append) {
1202+
/* Use forward-slash, like `normalize_ntpath()` */
1203+
strbuf_complete(resolved, '/');
1204+
strbuf_addstr(resolved, append);
1205+
FREE_AND_NULL(append);
1206+
}
1207+
1208+
return resolved->buf;
1209+
1210+
}
1211+
11361212
char *mingw_getcwd(char *pointer, int len)
11371213
{
11381214
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
@@ -456,6 +456,9 @@ int mingw_is_mount_point(struct strbuf *path);
456456
#define PATH_SEP ';'
457457
char *mingw_query_user_email(void);
458458
#define query_user_email mingw_query_user_email
459+
struct strbuf;
460+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path);
461+
#define platform_strbuf_realpath mingw_strbuf_realpath
459462
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
460463
#define PRIuMAX "I64u"
461464
#define PRId64 "I64d"

git-compat-util.h

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

598+
#ifndef platform_strbuf_realpath
599+
#define platform_strbuf_realpath(resolved, path) NULL
600+
#endif
601+
598602
#ifdef __TANDEM
599603
#include <floss.h(floss_execl,floss_execlp,floss_execv,floss_execvp)>
600604
#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)