Skip to content

Commit 09bd504

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Merge pull request #2504 from dscho/access-repo-via-junction
Handle `git add <file>` where <file> traverses an NTFS junction
2 parents 3810455 + 1f77753 commit 09bd504

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
@@ -1241,6 +1241,82 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
12411241
}
12421242
#endif
12431243

1244+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1245+
{
1246+
wchar_t wpath[MAX_PATH];
1247+
HANDLE h;
1248+
DWORD ret;
1249+
int len;
1250+
const char *last_component = NULL;
1251+
char *append = NULL;
1252+
1253+
if (xutftowcs_path(wpath, path) < 0)
1254+
return NULL;
1255+
1256+
h = CreateFileW(wpath, 0,
1257+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1258+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1259+
1260+
/*
1261+
* strbuf_realpath() allows the last path component to not exist. If
1262+
* that is the case, now it's time to try without last component.
1263+
*/
1264+
if (h == INVALID_HANDLE_VALUE &&
1265+
GetLastError() == ERROR_FILE_NOT_FOUND) {
1266+
/* cut last component off of `wpath` */
1267+
wchar_t *p = wpath + wcslen(wpath);
1268+
1269+
while (p != wpath)
1270+
if (*(--p) == L'/' || *p == L'\\')
1271+
break; /* found start of last component */
1272+
1273+
if (p != wpath && (last_component = find_last_dir_sep(path))) {
1274+
append = xstrdup(last_component + 1); /* skip directory separator */
1275+
/*
1276+
* Do not strip the trailing slash at the drive root, otherwise
1277+
* the path would be e.g. `C:` (which resolves to the
1278+
* _current_ directory on that drive).
1279+
*/
1280+
if (p[-1] == L':')
1281+
p[1] = L'\0';
1282+
else
1283+
*p = L'\0';
1284+
h = CreateFileW(wpath, 0, FILE_SHARE_READ |
1285+
FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1286+
NULL, OPEN_EXISTING,
1287+
FILE_FLAG_BACKUP_SEMANTICS, NULL);
1288+
}
1289+
}
1290+
1291+
if (h == INVALID_HANDLE_VALUE) {
1292+
realpath_failed:
1293+
FREE_AND_NULL(append);
1294+
return NULL;
1295+
}
1296+
1297+
ret = GetFinalPathNameByHandleW(h, wpath, ARRAY_SIZE(wpath), 0);
1298+
CloseHandle(h);
1299+
if (!ret || ret >= ARRAY_SIZE(wpath))
1300+
goto realpath_failed;
1301+
1302+
len = wcslen(wpath) * 3;
1303+
strbuf_grow(resolved, len);
1304+
len = xwcstoutf(resolved->buf, normalize_ntpath(wpath), len);
1305+
if (len < 0)
1306+
goto realpath_failed;
1307+
resolved->len = len;
1308+
1309+
if (append) {
1310+
/* Use forward-slash, like `normalize_ntpath()` */
1311+
strbuf_complete(resolved, '/');
1312+
strbuf_addstr(resolved, append);
1313+
FREE_AND_NULL(append);
1314+
}
1315+
1316+
return resolved->buf;
1317+
1318+
}
1319+
12441320
char *mingw_getcwd(char *pointer, int len)
12451321
{
12461322
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
@@ -43,6 +43,9 @@ int mingw_is_mount_point(struct strbuf *path);
4343
#define PATH_SEP ';'
4444
char *mingw_query_user_email(void);
4545
#define query_user_email mingw_query_user_email
46+
struct strbuf;
47+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path);
48+
#define platform_strbuf_realpath mingw_strbuf_realpath
4649

4750
/**
4851
* Verifies that the specified path is owned by the user running the

git-compat-util.h

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

394+
#ifndef platform_strbuf_realpath
395+
#define platform_strbuf_realpath(resolved, path) NULL
396+
#endif
397+
394398
#ifdef __TANDEM
395399
#include <floss.h(floss_execl,floss_execlp,floss_execv,floss_execvp)>
396400
#include <floss.h(floss_getpwuid)>

t/t0060-path-utils.sh

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

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

t/t3700-add.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,4 +574,15 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
574574
git add "$downcased"
575575
'
576576

577+
test_expect_success MINGW 'can add files via NTFS junctions' '
578+
test_when_finished "cmd //c rmdir junction && rm -rf target" &&
579+
test_create_repo target &&
580+
cmd //c "mklink /j junction target" &&
581+
>target/via-junction &&
582+
git -C junction add "$(pwd)/junction/via-junction" &&
583+
echo via-junction >expect &&
584+
git -C target diff --cached --name-only >actual &&
585+
test_cmp expect actual
586+
'
587+
577588
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)