Skip to content

Commit d9e98cb

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 20ac84b + e95e8a9 commit d9e98cb

File tree

11 files changed

+198
-48
lines changed

11 files changed

+198
-48
lines changed

Documentation/gitattributes.txt

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

387387

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

apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4366,7 +4366,7 @@ static int try_create_file(struct apply_state *state, const char *path,
43664366
/* Although buf:size is counted string, it also is NUL
43674367
* terminated.
43684368
*/
4369-
return !!symlink(buf, path);
4369+
return !!create_symlink(state && state->repo ? state->repo->index : NULL, buf, path);
43704370

43714371
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
43724372
if (fd < 0)

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
513513
}
514514
add_path(&wtdir, wtdir_len, dst_path);
515515
if (symlinks) {
516-
if (symlink(wtdir.buf, rdir.buf)) {
516+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
517517
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
518518
goto finish;
519519
}

builtin/init-db.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
7878
if (strbuf_readlink(&lnk, template_path->buf,
7979
st_template.st_size) < 0)
8080
die_errno(_("cannot readlink '%s'"), template_path->buf);
81-
if (symlink(lnk.buf, path->buf))
81+
if (create_symlink(NULL, lnk.buf, path->buf))
8282
die_errno(_("cannot symlink '%s' '%s'"),
8383
lnk.buf, path->buf);
8484
strbuf_release(&lnk);
@@ -300,7 +300,7 @@ static int create_default_files(const char *template_path,
300300
path = git_path_buf(&buf, "tXXXXXX");
301301
if (!close(xmkstemp(path)) &&
302302
!unlink(path) &&
303-
!symlink("testing", path) &&
303+
!create_symlink(NULL, "testing", path) &&
304304
!lstat(path, &st1) &&
305305
S_ISLNK(st1.st_mode))
306306
unlink(path); /* good */

compat/mingw.c

Lines changed: 103 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SECURITY_WIN32
1515
#include <sspi.h>
1616
#include "win32/fscache.h"
17+
#include "../attr.h"
1718

1819
#define HCAST(type, handle) ((type)(intptr_t)handle)
1920

@@ -444,6 +445,54 @@ static void process_phantom_symlinks(void)
444445
LeaveCriticalSection(&phantom_symlinks_cs);
445446
}
446447

448+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
449+
{
450+
int len;
451+
452+
/* create file symlink */
453+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
454+
errno = err_win_to_posix(GetLastError());
455+
return -1;
456+
}
457+
458+
/* convert to directory symlink if target exists */
459+
switch (process_phantom_symlink(wtarget, wlink)) {
460+
case PHANTOM_SYMLINK_RETRY: {
461+
/* if target doesn't exist, add to phantom symlinks list */
462+
wchar_t wfullpath[MAX_LONG_PATH];
463+
struct phantom_symlink_info *psi;
464+
465+
/* convert to absolute path to be independent of cwd */
466+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
467+
if (!len || len >= MAX_LONG_PATH) {
468+
errno = err_win_to_posix(GetLastError());
469+
return -1;
470+
}
471+
472+
/* over-allocate and fill phantom_symlink_info structure */
473+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
474+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
475+
psi->wlink = (wchar_t *)(psi + 1);
476+
wcscpy(psi->wlink, wfullpath);
477+
psi->wtarget = psi->wlink + len + 1;
478+
wcscpy(psi->wtarget, wtarget);
479+
480+
EnterCriticalSection(&phantom_symlinks_cs);
481+
psi->next = phantom_symlinks;
482+
phantom_symlinks = psi;
483+
LeaveCriticalSection(&phantom_symlinks_cs);
484+
break;
485+
}
486+
case PHANTOM_SYMLINK_DIRECTORY:
487+
/* if we created a dir symlink, process other phantom symlinks */
488+
process_phantom_symlinks();
489+
break;
490+
default:
491+
break;
492+
}
493+
return 0;
494+
}
495+
447496
/* Normalizes NT paths as returned by some low-level APIs. */
448497
static wchar_t *normalize_ntpath(wchar_t *wbuf)
449498
{
@@ -2858,7 +2907,38 @@ int link(const char *oldpath, const char *newpath)
28582907
return 0;
28592908
}
28602909

2861-
int symlink(const char *target, const char *link)
2910+
enum symlink_type {
2911+
SYMLINK_TYPE_UNSPECIFIED = 0,
2912+
SYMLINK_TYPE_FILE,
2913+
SYMLINK_TYPE_DIRECTORY,
2914+
};
2915+
2916+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2917+
{
2918+
static struct attr_check *check;
2919+
const char *value;
2920+
2921+
if (!index)
2922+
return SYMLINK_TYPE_UNSPECIFIED;
2923+
2924+
if (!check)
2925+
check = attr_check_initl("symlink", NULL);
2926+
2927+
git_check_attr(index, link, check);
2928+
2929+
value = check->items[0].value;
2930+
if (ATTR_UNSET(value))
2931+
return SYMLINK_TYPE_UNSPECIFIED;
2932+
if (!strcmp(value, "file"))
2933+
return SYMLINK_TYPE_FILE;
2934+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2935+
return SYMLINK_TYPE_DIRECTORY;
2936+
2937+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2938+
return SYMLINK_TYPE_UNSPECIFIED;
2939+
}
2940+
2941+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
28622942
{
28632943
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
28642944
int len;
@@ -2878,48 +2958,31 @@ int symlink(const char *target, const char *link)
28782958
if (wtarget[len] == '/')
28792959
wtarget[len] = '\\';
28802960

2881-
/* create file symlink */
2882-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2883-
errno = err_win_to_posix(GetLastError());
2884-
return -1;
2885-
}
2886-
2887-
/* convert to directory symlink if target exists */
2888-
switch (process_phantom_symlink(wtarget, wlink)) {
2889-
case PHANTOM_SYMLINK_RETRY: {
2890-
/* if target doesn't exist, add to phantom symlinks list */
2891-
wchar_t wfullpath[MAX_LONG_PATH];
2892-
struct phantom_symlink_info *psi;
2893-
2894-
/* convert to absolute path to be independent of cwd */
2895-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2896-
if (!len || len >= MAX_LONG_PATH) {
2897-
errno = err_win_to_posix(GetLastError());
2898-
return -1;
2899-
}
2900-
2901-
/* over-allocate and fill phantom_symlink_info structure */
2902-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2903-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2904-
psi->wlink = (wchar_t *)(psi + 1);
2905-
wcscpy(psi->wlink, wfullpath);
2906-
psi->wtarget = psi->wlink + len + 1;
2907-
wcscpy(psi->wtarget, wtarget);
2908-
2909-
EnterCriticalSection(&phantom_symlinks_cs);
2910-
psi->next = phantom_symlinks;
2911-
phantom_symlinks = psi;
2912-
LeaveCriticalSection(&phantom_symlinks_cs);
2913-
break;
2914-
}
2915-
case PHANTOM_SYMLINK_DIRECTORY:
2916-
/* if we created a dir symlink, process other phantom symlinks */
2961+
switch (check_symlink_attr(index, link)) {
2962+
case SYMLINK_TYPE_UNSPECIFIED:
2963+
/* Create a phantom symlink: it is initially created as a file
2964+
* symlink, but may change to a directory symlink later if/when
2965+
* the target exists. */
2966+
return create_phantom_symlink(wtarget, wlink);
2967+
case SYMLINK_TYPE_FILE:
2968+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2969+
break;
2970+
return 0;
2971+
case SYMLINK_TYPE_DIRECTORY:
2972+
if (!CreateSymbolicLinkW(wlink, wtarget,
2973+
symlink_directory_flags))
2974+
break;
2975+
/* There may be dangling phantom symlinks that point at this
2976+
* one, which should now morph into directory symlinks. */
29172977
process_phantom_symlinks();
2918-
break;
2978+
return 0;
29192979
default:
2920-
break;
2980+
BUG("unhandled symlink type");
29212981
}
2922-
return 0;
2982+
2983+
/* CreateSymbolicLinkW failed. */
2984+
errno = err_win_to_posix(GetLastError());
2985+
return -1;
29232986
}
29242987

29252988
#ifndef _WINNT_H

compat/mingw.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
215215
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
216216
int link(const char *oldpath, const char *newpath);
217217
int uname(struct utsname *buf);
218-
int symlink(const char *target, const char *link);
219218
int readlink(const char *path, char *buf, size_t bufsiz);
219+
struct index_state;
220+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link);
221+
#define create_symlink mingw_create_symlink
220222

221223
/*
222224
* replacements of existing functions

entry.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
306306
if (!has_symlinks || to_tempfile)
307307
goto write_file_entry;
308308

309-
ret = symlink(new_blob, path);
309+
ret = create_symlink(state->istate, new_blob, path);
310310
free(new_blob);
311311
if (ret)
312312
return error_errno("unable to create symlink %s", path);

git-compat-util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,15 @@ static inline int git_has_dir_sep(const char *path)
558558
#define is_mount_point is_mount_point_via_stat
559559
#endif
560560

561+
#ifndef create_symlink
562+
struct index_state;
563+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
564+
{
565+
return symlink(target, link);
566+
}
567+
#define create_symlink git_create_symlink
568+
#endif
569+
561570
#ifndef query_user_email
562571
#define query_user_email() NULL
563572
#endif

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ static int update_file_flags(struct merge_options *opt,
993993
char *lnk = xmemdupz(buf, size);
994994
safe_create_leading_directories_const(path);
995995
unlink(path);
996-
if (symlink(lnk, path))
996+
if (create_symlink(&opt->priv->orig_index, lnk, path))
997997
ret = err(opt, _("failed to symlink '%s': %s"),
998998
path, strerror(errno));
999999
free(lnk);

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
18801880
#ifndef NO_SYMLINK_HEAD
18811881
char *ref_path = get_locked_file_path(&lock->lk);
18821882
unlink(ref_path);
1883-
ret = symlink(target, ref_path);
1883+
ret = create_symlink(NULL, target, ref_path);
18841884
free(ref_path);
18851885

18861886
if (ret)

t/t2040-checkout-symlink-attr.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
test_expect_success 'checkout symlinks with attr' '
27+
cache_symlink file1 file-link &&
28+
cache_symlink dir dir-link &&
29+
30+
printf "file-link symlink=file\ndir-link symlink=dir\n" >.gitattributes &&
31+
git add .gitattributes &&
32+
33+
git checkout . &&
34+
35+
mkdir dir &&
36+
echo "[a]b=c" >file1 &&
37+
echo "[x]y=z" >dir/file2 &&
38+
39+
# MSYS2 is very forgiving, it will resolve symlinks even if the
40+
# symlink type is incorrect. To make this test meaningful, try
41+
# them with a native, non-MSYS executable, such as `git config`.
42+
test "$(git config -f file-link a.b)" = "c" &&
43+
test "$(git config -f dir-link/file2 x.y)" = "z"
44+
'
45+
46+
test_done

0 commit comments

Comments
 (0)