Skip to content

Commit c8cd69c

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 ae6c8d7 + bc5e0f9 commit c8cd69c

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

abspath.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,
9595
goto error_out;
9696
}
9797

98+
if (platform_strbuf_realpath(resolved, path))
99+
return resolved->buf;
100+
98101
strbuf_addstr(&remaining, path);
99102
get_root_part(resolved, &remaining);
100103

compat/mingw.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,37 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
10801080
return NULL;
10811081
}
10821082

1083+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1084+
{
1085+
wchar_t wpath[MAX_PATH];
1086+
HANDLE h;
1087+
DWORD ret;
1088+
int len;
1089+
1090+
if (xutftowcs_path(wpath, path) < 0)
1091+
return NULL;
1092+
1093+
h = CreateFileW(wpath, 0,
1094+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1095+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1096+
if (h == INVALID_HANDLE_VALUE)
1097+
return NULL;
1098+
1099+
ret = GetFinalPathNameByHandleW(h, wpath, ARRAY_SIZE(wpath), 0);
1100+
CloseHandle(h);
1101+
if (!ret || ret >= ARRAY_SIZE(wpath))
1102+
return NULL;
1103+
1104+
len = wcslen(wpath) * 3;
1105+
strbuf_grow(resolved, len);
1106+
len = xwcstoutf(resolved->buf, normalize_ntpath(wpath), len);
1107+
if (len < 0)
1108+
return NULL;
1109+
resolved->len = len;
1110+
return resolved->buf;
1111+
1112+
}
1113+
10831114
char *mingw_getcwd(char *pointer, int len)
10841115
{
10851116
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ int mingw_is_mount_point(struct strbuf *path);
449449
#define PATH_SEP ';'
450450
char *mingw_query_user_email(void);
451451
#define query_user_email mingw_query_user_email
452+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path);
453+
#define platform_strbuf_realpath mingw_strbuf_realpath
452454
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
453455
#define PRIuMAX "I64u"
454456
#define PRId64 "I64d"

git-compat-util.h

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

412+
#ifndef platform_strbuf_realpath
413+
#define platform_strbuf_realpath(resolved, path) NULL
414+
#endif
415+
412416
#ifdef __TANDEM
413417
#include <floss.h(floss_execl,floss_execlp,floss_execv,floss_execvp)>
414418
#include <floss.h(floss_getpwuid)>

t/t3700-add.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,15 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
423423
git add "$downcased"
424424
'
425425

426+
test_expect_success MINGW 'can add files via NTFS junctions' '
427+
test_when_finished "cmd //c rmdir junction && rm -rf target" &&
428+
test_create_repo target &&
429+
cmd //c "mklink /j junction target" &&
430+
>target/via-junction &&
431+
git -C junction add "$(pwd)/junction/via-junction" &&
432+
echo via-junction >expect &&
433+
git -C target diff --cached --name-only >actual &&
434+
test_cmp expect actual
435+
'
436+
426437
test_done

0 commit comments

Comments
 (0)