Skip to content

Commit f58d993

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 2b9298a + 0d2c612 commit f58d993

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

@@ -429,6 +430,54 @@ static void process_phantom_symlinks(void)
429430
LeaveCriticalSection(&phantom_symlinks_cs);
430431
}
431432

433+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
434+
{
435+
int len;
436+
437+
/* create file symlink */
438+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
439+
errno = err_win_to_posix(GetLastError());
440+
return -1;
441+
}
442+
443+
/* convert to directory symlink if target exists */
444+
switch (process_phantom_symlink(wtarget, wlink)) {
445+
case PHANTOM_SYMLINK_RETRY: {
446+
/* if target doesn't exist, add to phantom symlinks list */
447+
wchar_t wfullpath[MAX_LONG_PATH];
448+
struct phantom_symlink_info *psi;
449+
450+
/* convert to absolute path to be independent of cwd */
451+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
452+
if (!len || len >= MAX_LONG_PATH) {
453+
errno = err_win_to_posix(GetLastError());
454+
return -1;
455+
}
456+
457+
/* over-allocate and fill phantom_symlink_info structure */
458+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
459+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
460+
psi->wlink = (wchar_t *)(psi + 1);
461+
wcscpy(psi->wlink, wfullpath);
462+
psi->wtarget = psi->wlink + len + 1;
463+
wcscpy(psi->wtarget, wtarget);
464+
465+
EnterCriticalSection(&phantom_symlinks_cs);
466+
psi->next = phantom_symlinks;
467+
phantom_symlinks = psi;
468+
LeaveCriticalSection(&phantom_symlinks_cs);
469+
break;
470+
}
471+
case PHANTOM_SYMLINK_DIRECTORY:
472+
/* if we created a dir symlink, process other phantom symlinks */
473+
process_phantom_symlinks();
474+
break;
475+
default:
476+
break;
477+
}
478+
return 0;
479+
}
480+
432481
/* Normalizes NT paths as returned by some low-level APIs. */
433482
static wchar_t *normalize_ntpath(wchar_t *wbuf)
434483
{
@@ -2818,7 +2867,38 @@ int link(const char *oldpath, const char *newpath)
28182867
return 0;
28192868
}
28202869

2821-
int symlink(const char *target, const char *link)
2870+
enum symlink_type {
2871+
SYMLINK_TYPE_UNSPECIFIED = 0,
2872+
SYMLINK_TYPE_FILE,
2873+
SYMLINK_TYPE_DIRECTORY,
2874+
};
2875+
2876+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2877+
{
2878+
static struct attr_check *check;
2879+
const char *value;
2880+
2881+
if (!index)
2882+
return SYMLINK_TYPE_UNSPECIFIED;
2883+
2884+
if (!check)
2885+
check = attr_check_initl("symlink", NULL);
2886+
2887+
git_check_attr(index, link, check);
2888+
2889+
value = check->items[0].value;
2890+
if (ATTR_UNSET(value))
2891+
return SYMLINK_TYPE_UNSPECIFIED;
2892+
if (!strcmp(value, "file"))
2893+
return SYMLINK_TYPE_FILE;
2894+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2895+
return SYMLINK_TYPE_DIRECTORY;
2896+
2897+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2898+
return SYMLINK_TYPE_UNSPECIFIED;
2899+
}
2900+
2901+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
28222902
{
28232903
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
28242904
int len;
@@ -2838,48 +2918,31 @@ int symlink(const char *target, const char *link)
28382918
if (wtarget[len] == '/')
28392919
wtarget[len] = '\\';
28402920

2841-
/* create file symlink */
2842-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2843-
errno = err_win_to_posix(GetLastError());
2844-
return -1;
2845-
}
2846-
2847-
/* convert to directory symlink if target exists */
2848-
switch (process_phantom_symlink(wtarget, wlink)) {
2849-
case PHANTOM_SYMLINK_RETRY: {
2850-
/* if target doesn't exist, add to phantom symlinks list */
2851-
wchar_t wfullpath[MAX_LONG_PATH];
2852-
struct phantom_symlink_info *psi;
2853-
2854-
/* convert to absolute path to be independent of cwd */
2855-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2856-
if (!len || len >= MAX_LONG_PATH) {
2857-
errno = err_win_to_posix(GetLastError());
2858-
return -1;
2859-
}
2860-
2861-
/* over-allocate and fill phantom_symlink_info structure */
2862-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2863-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2864-
psi->wlink = (wchar_t *)(psi + 1);
2865-
wcscpy(psi->wlink, wfullpath);
2866-
psi->wtarget = psi->wlink + len + 1;
2867-
wcscpy(psi->wtarget, wtarget);
2868-
2869-
EnterCriticalSection(&phantom_symlinks_cs);
2870-
psi->next = phantom_symlinks;
2871-
phantom_symlinks = psi;
2872-
LeaveCriticalSection(&phantom_symlinks_cs);
2873-
break;
2874-
}
2875-
case PHANTOM_SYMLINK_DIRECTORY:
2876-
/* if we created a dir symlink, process other phantom symlinks */
2921+
switch (check_symlink_attr(index, link)) {
2922+
case SYMLINK_TYPE_UNSPECIFIED:
2923+
/* Create a phantom symlink: it is initially created as a file
2924+
* symlink, but may change to a directory symlink later if/when
2925+
* the target exists. */
2926+
return create_phantom_symlink(wtarget, wlink);
2927+
case SYMLINK_TYPE_FILE:
2928+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2929+
break;
2930+
return 0;
2931+
case SYMLINK_TYPE_DIRECTORY:
2932+
if (!CreateSymbolicLinkW(wlink, wtarget,
2933+
symlink_directory_flags))
2934+
break;
2935+
/* There may be dangling phantom symlinks that point at this
2936+
* one, which should now morph into directory symlinks. */
28772937
process_phantom_symlinks();
2878-
break;
2938+
return 0;
28792939
default:
2880-
break;
2940+
BUG("unhandled symlink type");
28812941
}
2882-
return 0;
2942+
2943+
/* CreateSymbolicLinkW failed. */
2944+
errno = err_win_to_posix(GetLastError());
2945+
return -1;
28832946
}
28842947

28852948
#ifndef _WINNT_H

compat/mingw.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,10 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
213213
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
214214
int link(const char *oldpath, const char *newpath);
215215
int uname(struct utsname *buf);
216-
int symlink(const char *target, const char *link);
217216
int readlink(const char *path, char *buf, size_t bufsiz);
217+
struct index_state;
218+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link);
219+
#define create_symlink mingw_create_symlink
218220

219221
/*
220222
* replacements of existing functions

entry.c

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

308-
ret = symlink(new_blob, path);
308+
ret = create_symlink(state->istate, new_blob, path);
309309
free(new_blob);
310310
if (ret)
311311
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
@@ -532,6 +532,15 @@ static inline int git_has_dir_sep(const char *path)
532532
#define is_mount_point is_mount_point_via_stat
533533
#endif
534534

535+
#ifndef create_symlink
536+
struct index_state;
537+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
538+
{
539+
return symlink(target, link);
540+
}
541+
#define create_symlink git_create_symlink
542+
#endif
543+
535544
#ifndef query_user_email
536545
#define query_user_email() NULL
537546
#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)