Skip to content

Commit 74f406f

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 6fdd1c8 + a2f18f9 commit 74f406f

12 files changed

+200
-48
lines changed

Documentation/gitattributes.txt

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

405405

406+
`symlink`
407+
^^^^^^^^^
408+
409+
On Windows, symbolic links have a type: a "file symlink" must point at
410+
a file, and a "directory symlink" must point at a directory. If the
411+
type of symlink does not match its target, it doesn't work.
412+
413+
Git does not record the type of symlink in the index or in a tree. On
414+
checkout it'll guess the type, which only works if the target exists
415+
at the time the symlink is created. This may often not be the case,
416+
for example when the link points at a directory inside a submodule.
417+
418+
The `symlink` attribute allows you to explicitly set the type of symlink
419+
to `file` or `dir`, so Git doesn't have to guess. If you have a set of
420+
symlinks that point at other files, you can do:
421+
422+
------------------------
423+
*.gif symlink=file
424+
------------------------
425+
426+
To tell Git that a symlink points at a directory, use:
427+
428+
------------------------
429+
tools_folder symlink=dir
430+
------------------------
431+
432+
The `symlink` attribute is ignored on platforms other than Windows,
433+
since they don't distinguish between different types of symlinks.
434+
435+
406436
`filter`
407437
^^^^^^^^
408438

apply.c

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

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

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
526526
}
527527
add_path(&wtdir, wtdir_len, dst_path);
528528
if (symlinks) {
529-
if (symlink(wtdir.buf, rdir.buf)) {
529+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
530530
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
531531
goto finish;
532532
}

compat/mingw.c

Lines changed: 103 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "../write-or-die.h"
2525
#include "../repository.h"
2626
#include "win32/fscache.h"
27+
#include "../attr.h"
2728

2829
#define HCAST(type, handle) ((type)(intptr_t)handle)
2930

@@ -460,6 +461,54 @@ static void process_phantom_symlinks(void)
460461
LeaveCriticalSection(&phantom_symlinks_cs);
461462
}
462463

464+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
465+
{
466+
int len;
467+
468+
/* create file symlink */
469+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
470+
errno = err_win_to_posix(GetLastError());
471+
return -1;
472+
}
473+
474+
/* convert to directory symlink if target exists */
475+
switch (process_phantom_symlink(wtarget, wlink)) {
476+
case PHANTOM_SYMLINK_RETRY: {
477+
/* if target doesn't exist, add to phantom symlinks list */
478+
wchar_t wfullpath[MAX_LONG_PATH];
479+
struct phantom_symlink_info *psi;
480+
481+
/* convert to absolute path to be independent of cwd */
482+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
483+
if (!len || len >= MAX_LONG_PATH) {
484+
errno = err_win_to_posix(GetLastError());
485+
return -1;
486+
}
487+
488+
/* over-allocate and fill phantom_symlink_info structure */
489+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
490+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
491+
psi->wlink = (wchar_t *)(psi + 1);
492+
wcscpy(psi->wlink, wfullpath);
493+
psi->wtarget = psi->wlink + len + 1;
494+
wcscpy(psi->wtarget, wtarget);
495+
496+
EnterCriticalSection(&phantom_symlinks_cs);
497+
psi->next = phantom_symlinks;
498+
phantom_symlinks = psi;
499+
LeaveCriticalSection(&phantom_symlinks_cs);
500+
break;
501+
}
502+
case PHANTOM_SYMLINK_DIRECTORY:
503+
/* if we created a dir symlink, process other phantom symlinks */
504+
process_phantom_symlinks();
505+
break;
506+
default:
507+
break;
508+
}
509+
return 0;
510+
}
511+
463512
/* Normalizes NT paths as returned by some low-level APIs. */
464513
static wchar_t *normalize_ntpath(wchar_t *wbuf)
465514
{
@@ -3063,7 +3112,38 @@ int link(const char *oldpath, const char *newpath)
30633112
return 0;
30643113
}
30653114

3066-
int symlink(const char *target, const char *link)
3115+
enum symlink_type {
3116+
SYMLINK_TYPE_UNSPECIFIED = 0,
3117+
SYMLINK_TYPE_FILE,
3118+
SYMLINK_TYPE_DIRECTORY,
3119+
};
3120+
3121+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
3122+
{
3123+
static struct attr_check *check;
3124+
const char *value;
3125+
3126+
if (!index)
3127+
return SYMLINK_TYPE_UNSPECIFIED;
3128+
3129+
if (!check)
3130+
check = attr_check_initl("symlink", NULL);
3131+
3132+
git_check_attr(index, link, check);
3133+
3134+
value = check->items[0].value;
3135+
if (ATTR_UNSET(value))
3136+
return SYMLINK_TYPE_UNSPECIFIED;
3137+
if (!strcmp(value, "file"))
3138+
return SYMLINK_TYPE_FILE;
3139+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
3140+
return SYMLINK_TYPE_DIRECTORY;
3141+
3142+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
3143+
return SYMLINK_TYPE_UNSPECIFIED;
3144+
}
3145+
3146+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
30673147
{
30683148
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
30693149
int len;
@@ -3083,48 +3163,31 @@ int symlink(const char *target, const char *link)
30833163
if (wtarget[len] == '/')
30843164
wtarget[len] = '\\';
30853165

3086-
/* create file symlink */
3087-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
3088-
errno = err_win_to_posix(GetLastError());
3089-
return -1;
3090-
}
3091-
3092-
/* convert to directory symlink if target exists */
3093-
switch (process_phantom_symlink(wtarget, wlink)) {
3094-
case PHANTOM_SYMLINK_RETRY: {
3095-
/* if target doesn't exist, add to phantom symlinks list */
3096-
wchar_t wfullpath[MAX_LONG_PATH];
3097-
struct phantom_symlink_info *psi;
3098-
3099-
/* convert to absolute path to be independent of cwd */
3100-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
3101-
if (!len || len >= MAX_LONG_PATH) {
3102-
errno = err_win_to_posix(GetLastError());
3103-
return -1;
3104-
}
3105-
3106-
/* over-allocate and fill phantom_symlink_info structure */
3107-
psi = xmalloc(sizeof(struct phantom_symlink_info)
3108-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
3109-
psi->wlink = (wchar_t *)(psi + 1);
3110-
wcscpy(psi->wlink, wfullpath);
3111-
psi->wtarget = psi->wlink + len + 1;
3112-
wcscpy(psi->wtarget, wtarget);
3113-
3114-
EnterCriticalSection(&phantom_symlinks_cs);
3115-
psi->next = phantom_symlinks;
3116-
phantom_symlinks = psi;
3117-
LeaveCriticalSection(&phantom_symlinks_cs);
3118-
break;
3119-
}
3120-
case PHANTOM_SYMLINK_DIRECTORY:
3121-
/* if we created a dir symlink, process other phantom symlinks */
3166+
switch (check_symlink_attr(index, link)) {
3167+
case SYMLINK_TYPE_UNSPECIFIED:
3168+
/* Create a phantom symlink: it is initially created as a file
3169+
* symlink, but may change to a directory symlink later if/when
3170+
* the target exists. */
3171+
return create_phantom_symlink(wtarget, wlink);
3172+
case SYMLINK_TYPE_FILE:
3173+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
3174+
break;
3175+
return 0;
3176+
case SYMLINK_TYPE_DIRECTORY:
3177+
if (!CreateSymbolicLinkW(wlink, wtarget,
3178+
symlink_directory_flags))
3179+
break;
3180+
/* There may be dangling phantom symlinks that point at this
3181+
* one, which should now morph into directory symlinks. */
31223182
process_phantom_symlinks();
3123-
break;
3183+
return 0;
31243184
default:
3125-
break;
3185+
BUG("unhandled symlink type");
31263186
}
3127-
return 0;
3187+
3188+
/* CreateSymbolicLinkW failed. */
3189+
errno = err_win_to_posix(GetLastError());
3190+
return -1;
31283191
}
31293192

31303193
#ifndef _WINNT_H

compat/mingw.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,10 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
218218
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
219219
int link(const char *oldpath, const char *newpath);
220220
int uname(struct utsname *buf);
221-
int symlink(const char *target, const char *link);
222221
int readlink(const char *path, char *buf, size_t bufsiz);
222+
struct index_state;
223+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link);
224+
#define create_symlink mingw_create_symlink
223225

224226
/*
225227
* replacements of existing functions

entry.c

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

325-
ret = symlink(new_blob, path);
325+
ret = create_symlink(state->istate, new_blob, path);
326326
free(new_blob);
327327
if (ret)
328328
return error_errno("unable to create symlink %s", path);

git-compat-util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,16 @@ static inline int git_has_dir_sep(const char *path)
641641
#define is_mount_point is_mount_point_via_stat
642642
#endif
643643

644+
#ifndef create_symlink
645+
struct index_state;
646+
static inline int git_create_symlink(struct index_state *index UNUSED,
647+
const char *target, const char *link)
648+
{
649+
return symlink(target, link);
650+
}
651+
#define create_symlink git_create_symlink
652+
#endif
653+
644654
#ifndef query_user_email
645655
#define query_user_email() NULL
646656
#endif

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ static int update_file_flags(struct merge_options *opt,
10041004
char *lnk = xmemdupz(buf, size);
10051005
safe_create_leading_directories_const(path);
10061006
unlink(path);
1007-
if (symlink(lnk, path))
1007+
if (create_symlink(&opt->priv->orig_index, lnk, path))
10081008
ret = err(opt, _("failed to symlink '%s': %s"),
10091009
path, strerror(errno));
10101010
free(lnk);

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
20352035

20362036
char *ref_path = get_locked_file_path(&lock->lk);
20372037
unlink(ref_path);
2038-
ret = symlink(target, ref_path);
2038+
ret = create_symlink(NULL, target, ref_path);
20392039
free(ref_path);
20402040

20412041
if (ret)

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
21342134
if (strbuf_readlink(&lnk, template_path->buf,
21352135
st_template.st_size) < 0)
21362136
die_errno(_("cannot readlink '%s'"), template_path->buf);
2137-
if (symlink(lnk.buf, path->buf))
2137+
if (create_symlink(NULL, lnk.buf, path->buf))
21382138
die_errno(_("cannot symlink '%s' '%s'"),
21392139
lnk.buf, path->buf);
21402140
strbuf_release(&lnk);
@@ -2395,7 +2395,7 @@ static int create_default_files(const char *template_path,
23952395
path = git_path_buf(&buf, "tXXXXXX");
23962396
if (!close(xmkstemp(path)) &&
23972397
!unlink(path) &&
2398-
!symlink("testing", path) &&
2398+
!create_symlink(NULL, "testing", path) &&
23992399
!lstat(path, &st1) &&
24002400
S_ISLNK(st1.st_mode))
24012401
unlink(path); /* good */

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ integration_tests = [
275275
't2027-checkout-track.sh',
276276
't2030-unresolve-info.sh',
277277
't2031-checkout-long-paths.sh',
278+
't2040-checkout-symlink-attr.sh',
278279
't2050-git-dir-relative.sh',
279280
't2060-switch.sh',
280281
't2070-restore.sh',

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)