Skip to content

Commit 7aa8a0c

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 86ebf87 + b680f09 commit 7aa8a0c

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
@@ -1148,6 +1148,82 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
11481148
}
11491149
#endif
11501150

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

639+
#ifndef platform_strbuf_realpath
640+
#define platform_strbuf_realpath(resolved, path) NULL
641+
#endif
642+
639643
#ifdef __TANDEM
640644
#include <floss.h(floss_execl,floss_execlp,floss_execv,floss_execvp)>
641645
#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
@@ -549,4 +549,15 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
549549
git add "$downcased"
550550
'
551551

552+
test_expect_success MINGW 'can add files via NTFS junctions' '
553+
test_when_finished "cmd //c rmdir junction && rm -rf target" &&
554+
test_create_repo target &&
555+
cmd //c "mklink /j junction target" &&
556+
>target/via-junction &&
557+
git -C junction add "$(pwd)/junction/via-junction" &&
558+
echo via-junction >expect &&
559+
git -C target diff --cached --name-only >actual &&
560+
test_cmp expect actual
561+
'
562+
552563
test_done

t/t5601-clone.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ test_expect_success 'clone respects GIT_WORK_TREE' '
7878
7979
'
8080

81+
test_expect_success CASE_INSENSITIVE_FS 'core.worktree is not added due to path case' '
82+
83+
mkdir UPPERCASE &&
84+
git clone src "$(pwd)/uppercase" &&
85+
test "unset" = "$(git -C UPPERCASE config --default unset core.worktree)"
86+
'
87+
8188
test_expect_success 'clone from hooks' '
8289
8390
test_create_repo r0 &&

0 commit comments

Comments
 (0)