Skip to content

Commit 5fc9ac3

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 5f14fd1 + f19a197 commit 5fc9ac3

File tree

2 files changed

+100
-42
lines changed

2 files changed

+100
-42
lines changed

compat/mingw.c

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,54 @@ static void process_phantom_symlinks(void)
414414
LeaveCriticalSection(&phantom_symlinks_cs);
415415
}
416416

417+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
418+
{
419+
int len;
420+
421+
/* create file symlink */
422+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
423+
errno = err_win_to_posix(GetLastError());
424+
return -1;
425+
}
426+
427+
/* convert to directory symlink if target exists */
428+
switch (process_phantom_symlink(wtarget, wlink)) {
429+
case PHANTOM_SYMLINK_RETRY: {
430+
/* if target doesn't exist, add to phantom symlinks list */
431+
wchar_t wfullpath[MAX_LONG_PATH];
432+
struct phantom_symlink_info *psi;
433+
434+
/* convert to absolute path to be independent of cwd */
435+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
436+
if (!len || len >= MAX_LONG_PATH) {
437+
errno = err_win_to_posix(GetLastError());
438+
return -1;
439+
}
440+
441+
/* over-allocate and fill phantom_symlink_info structure */
442+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
443+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
444+
psi->wlink = (wchar_t *)(psi + 1);
445+
wcscpy(psi->wlink, wfullpath);
446+
psi->wtarget = psi->wlink + len + 1;
447+
wcscpy(psi->wtarget, wtarget);
448+
449+
EnterCriticalSection(&phantom_symlinks_cs);
450+
psi->next = phantom_symlinks;
451+
phantom_symlinks = psi;
452+
LeaveCriticalSection(&phantom_symlinks_cs);
453+
break;
454+
}
455+
case PHANTOM_SYMLINK_DIRECTORY:
456+
/* if we created a dir symlink, process other phantom symlinks */
457+
process_phantom_symlinks();
458+
break;
459+
default:
460+
break;
461+
}
462+
return 0;
463+
}
464+
417465
/* Normalizes NT paths as returned by some low-level APIs. */
418466
static wchar_t *normalize_ntpath(wchar_t *wbuf)
419467
{
@@ -2481,48 +2529,7 @@ int symlink(const char *target, const char *link)
24812529
if (wtarget[len] == '/')
24822530
wtarget[len] = '\\';
24832531

2484-
/* create file symlink */
2485-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2486-
errno = err_win_to_posix(GetLastError());
2487-
return -1;
2488-
}
2489-
2490-
/* convert to directory symlink if target exists */
2491-
switch (process_phantom_symlink(wtarget, wlink)) {
2492-
case PHANTOM_SYMLINK_RETRY: {
2493-
/* if target doesn't exist, add to phantom symlinks list */
2494-
wchar_t wfullpath[MAX_LONG_PATH];
2495-
struct phantom_symlink_info *psi;
2496-
2497-
/* convert to absolute path to be independent of cwd */
2498-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2499-
if (!len || len >= MAX_LONG_PATH) {
2500-
errno = err_win_to_posix(GetLastError());
2501-
return -1;
2502-
}
2503-
2504-
/* over-allocate and fill phantom_symlink_info structure */
2505-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2506-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2507-
psi->wlink = (wchar_t *)(psi + 1);
2508-
wcscpy(psi->wlink, wfullpath);
2509-
psi->wtarget = psi->wlink + len + 1;
2510-
wcscpy(psi->wtarget, wtarget);
2511-
2512-
EnterCriticalSection(&phantom_symlinks_cs);
2513-
psi->next = phantom_symlinks;
2514-
phantom_symlinks = psi;
2515-
LeaveCriticalSection(&phantom_symlinks_cs);
2516-
break;
2517-
}
2518-
case PHANTOM_SYMLINK_DIRECTORY:
2519-
/* if we created a dir symlink, process other phantom symlinks */
2520-
process_phantom_symlinks();
2521-
break;
2522-
default:
2523-
break;
2524-
}
2525-
return 0;
2532+
return create_phantom_symlink(wtarget, wlink);
25262533
}
25272534

25282535
#ifndef _WINNT_H

t/t2040-checkout-symlink-attr.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/sh
2+
3+
test_description='checkout symlinks with `symlink` attribute on Windows
4+
5+
Ensures that Git for Windows creates symlinks of the right type,
6+
as specified by the `symlink` attribute in `.gitattributes`.'
7+
8+
# Tell MSYS to create native symlinks. Without this flag test-lib's
9+
# prerequisite detection for SYMLINKS doesn't detect the right thing.
10+
MSYS=winsymlinks:nativestrict && export MSYS
11+
12+
. ./test-lib.sh
13+
14+
if ! test_have_prereq MINGW,SYMLINKS
15+
then
16+
skip_all='skipping $0: MinGW-only test, which requires symlink support.'
17+
test_done
18+
fi
19+
20+
# Adds a symlink to the index without clobbering the work tree.
21+
cache_symlink () {
22+
sha=$(printf '%s' "$1" | git hash-object --stdin -w) &&
23+
git update-index --add --cacheinfo 120000,$sha,"$2"
24+
}
25+
26+
# MSYS2 is very forgiving, it will resolve symlinks even if the
27+
# symlink type isn't correct. To make this test meaningful, try
28+
# them with a native, non-MSYS executable.
29+
cat_native () {
30+
filename=$(cygpath -w "$1") &&
31+
cmd.exe /c "type \"$filename\""
32+
}
33+
34+
test_expect_success 'checkout symlinks with attr' '
35+
cache_symlink file1 file-link &&
36+
cache_symlink dir dir-link &&
37+
38+
printf "file-link symlink=file\ndir-link symlink=dir\n" >.gitattributes &&
39+
git add .gitattributes &&
40+
41+
git checkout . &&
42+
43+
mkdir dir &&
44+
echo "contents1" >file1 &&
45+
echo "contents2" >dir/file2 &&
46+
47+
test "$(cat_native file-link)" = "contents1" &&
48+
test "$(cat_native dir-link/file2)" = "contents2"
49+
'
50+
51+
test_done

0 commit comments

Comments
 (0)