Skip to content

Commit 76163fa

Browse files
committed
Merge pull request git-for-windows#1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents f2e6ad2 + 059a694 commit 76163fa

File tree

3 files changed

+181
-39
lines changed

3 files changed

+181
-39
lines changed

Documentation/gitattributes.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,36 @@ sign `$` upon checkout. Any byte sequence that begins with
378378
with `$Id$` upon check-in.
379379

380380

381+
`symlink`
382+
^^^^^^^^^
383+
384+
On Windows, symbolic links have a type: a "file symlink" must point at
385+
a file, and a "directory symlink" must point at a directory. If the
386+
type of symlink does not match its target, it doesn't work.
387+
388+
Git does not record the type of symlink in the index or in a tree. On
389+
checkout it'll guess the type, which only works if the target exists
390+
at the time the symlink is created. This may often not be the case,
391+
for example when the link points at a directory inside a submodule.
392+
393+
The `symlink` attribute allows you to explicitly set the type of symlink
394+
to `file` or `dir`, so Git doesn't have to guess. If you have a set of
395+
symlinks that point at other files, you can do:
396+
397+
------------------------
398+
*.gif symlink=file
399+
------------------------
400+
401+
To tell Git that a symlink points at a directory, use:
402+
403+
------------------------
404+
tools_folder symlink=dir
405+
------------------------
406+
407+
The `symlink` attribute is ignored on platforms other than Windows,
408+
since they don't distinguish between different types of symlinks.
409+
410+
381411
`filter`
382412
^^^^^^^^
383413

compat/mingw.c

Lines changed: 100 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../cache.h"
99
#include "win32/lazyload.h"
1010
#include "../config.h"
11+
#include "../attr.h"
1112

1213
#define HCAST(type, handle) ((type)(intptr_t)handle)
1314

@@ -399,6 +400,54 @@ static void process_phantom_symlinks(void)
399400
LeaveCriticalSection(&phantom_symlinks_cs);
400401
}
401402

403+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
404+
{
405+
int len;
406+
407+
/* create file symlink */
408+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
409+
errno = err_win_to_posix(GetLastError());
410+
return -1;
411+
}
412+
413+
/* convert to directory symlink if target exists */
414+
switch (process_phantom_symlink(wtarget, wlink)) {
415+
case PHANTOM_SYMLINK_RETRY: {
416+
/* if target doesn't exist, add to phantom symlinks list */
417+
wchar_t wfullpath[MAX_LONG_PATH];
418+
struct phantom_symlink_info *psi;
419+
420+
/* convert to absolute path to be independent of cwd */
421+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
422+
if (!len || len >= MAX_LONG_PATH) {
423+
errno = err_win_to_posix(GetLastError());
424+
return -1;
425+
}
426+
427+
/* over-allocate and fill phantom_symlink_info structure */
428+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
429+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
430+
psi->wlink = (wchar_t *)(psi + 1);
431+
wcscpy(psi->wlink, wfullpath);
432+
psi->wtarget = psi->wlink + len + 1;
433+
wcscpy(psi->wtarget, wtarget);
434+
435+
EnterCriticalSection(&phantom_symlinks_cs);
436+
psi->next = phantom_symlinks;
437+
phantom_symlinks = psi;
438+
LeaveCriticalSection(&phantom_symlinks_cs);
439+
break;
440+
}
441+
case PHANTOM_SYMLINK_DIRECTORY:
442+
/* if we created a dir symlink, process other phantom symlinks */
443+
process_phantom_symlinks();
444+
break;
445+
default:
446+
break;
447+
}
448+
return 0;
449+
}
450+
402451
/* Normalizes NT paths as returned by some low-level APIs. */
403452
static wchar_t *normalize_ntpath(wchar_t *wbuf)
404453
{
@@ -2448,6 +2497,35 @@ int link(const char *oldpath, const char *newpath)
24482497
return 0;
24492498
}
24502499

2500+
enum symlink_type {
2501+
SYMLINK_TYPE_UNSPECIFIED = 0,
2502+
SYMLINK_TYPE_FILE,
2503+
SYMLINK_TYPE_DIRECTORY,
2504+
};
2505+
2506+
static enum symlink_type check_symlink_attr(const char *link)
2507+
{
2508+
static struct attr_check *check;
2509+
const char *value;
2510+
int r;
2511+
2512+
if (!check)
2513+
check = attr_check_initl("symlink", NULL);
2514+
2515+
r = git_check_attr(&the_index, link, check);
2516+
assert(!r);
2517+
2518+
value = check->items[0].value;
2519+
if (value == NULL)
2520+
;
2521+
else if (!strcmp(value, "file"))
2522+
return SYMLINK_TYPE_FILE;
2523+
else if (!strcmp(value, "dir"))
2524+
return SYMLINK_TYPE_DIRECTORY;
2525+
2526+
return SYMLINK_TYPE_UNSPECIFIED;
2527+
}
2528+
24512529
int symlink(const char *target, const char *link)
24522530
{
24532531
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
@@ -2468,48 +2546,31 @@ int symlink(const char *target, const char *link)
24682546
if (wtarget[len] == '/')
24692547
wtarget[len] = '\\';
24702548

2471-
/* create file symlink */
2472-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2473-
errno = err_win_to_posix(GetLastError());
2474-
return -1;
2475-
}
2476-
2477-
/* convert to directory symlink if target exists */
2478-
switch (process_phantom_symlink(wtarget, wlink)) {
2479-
case PHANTOM_SYMLINK_RETRY: {
2480-
/* if target doesn't exist, add to phantom symlinks list */
2481-
wchar_t wfullpath[MAX_LONG_PATH];
2482-
struct phantom_symlink_info *psi;
2483-
2484-
/* convert to absolute path to be independent of cwd */
2485-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2486-
if (!len || len >= MAX_LONG_PATH) {
2487-
errno = err_win_to_posix(GetLastError());
2488-
return -1;
2489-
}
2490-
2491-
/* over-allocate and fill phantom_symlink_info structure */
2492-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2493-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2494-
psi->wlink = (wchar_t *)(psi + 1);
2495-
wcscpy(psi->wlink, wfullpath);
2496-
psi->wtarget = psi->wlink + len + 1;
2497-
wcscpy(psi->wtarget, wtarget);
2498-
2499-
EnterCriticalSection(&phantom_symlinks_cs);
2500-
psi->next = phantom_symlinks;
2501-
phantom_symlinks = psi;
2502-
LeaveCriticalSection(&phantom_symlinks_cs);
2503-
break;
2504-
}
2505-
case PHANTOM_SYMLINK_DIRECTORY:
2506-
/* if we created a dir symlink, process other phantom symlinks */
2549+
switch (check_symlink_attr(link)) {
2550+
case SYMLINK_TYPE_UNSPECIFIED:
2551+
/* Create a phantom symlink: it is initially created as a file
2552+
* symlink, but may change to a directory symlink later if/when
2553+
* the target exists. */
2554+
return create_phantom_symlink(wtarget, wlink);
2555+
case SYMLINK_TYPE_FILE:
2556+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2557+
break;
2558+
return 0;
2559+
case SYMLINK_TYPE_DIRECTORY:
2560+
if (!CreateSymbolicLinkW(wlink, wtarget,
2561+
symlink_directory_flags))
2562+
break;
2563+
/* There may be dangling phantom symlinks that point at this
2564+
* one, which should now morph into directory symlinks. */
25072565
process_phantom_symlinks();
2508-
break;
2566+
return 0;
25092567
default:
2510-
break;
2568+
BUG("unhandled symlink type");
25112569
}
2512-
return 0;
2570+
2571+
/* CreateSymbolicLinkW failed. */
2572+
errno = err_win_to_posix(GetLastError());
2573+
return -1;
25132574
}
25142575

25152576
#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)