Skip to content

Commit 7535a0e

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents bd186a3 + 66f21dc commit 7535a0e

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
@@ -4376,7 +4376,7 @@ static int try_create_file(struct apply_state *state, const char *path,
43764376
/* Although buf:size is counted string, it also is NUL
43774377
* terminated.
43784378
*/
4379-
return !!symlink(buf, path);
4379+
return !!create_symlink(state && state->repo ? state->repo->index : NULL, buf, path);
43804380

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

builtin/difftool.c

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

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

@@ -447,6 +448,54 @@ static void process_phantom_symlinks(void)
447448
LeaveCriticalSection(&phantom_symlinks_cs);
448449
}
449450

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

2873-
int symlink(const char *target, const char *link)
2922+
enum symlink_type {
2923+
SYMLINK_TYPE_UNSPECIFIED = 0,
2924+
SYMLINK_TYPE_FILE,
2925+
SYMLINK_TYPE_DIRECTORY,
2926+
};
2927+
2928+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2929+
{
2930+
static struct attr_check *check;
2931+
const char *value;
2932+
2933+
if (!index)
2934+
return SYMLINK_TYPE_UNSPECIFIED;
2935+
2936+
if (!check)
2937+
check = attr_check_initl("symlink", NULL);
2938+
2939+
git_check_attr(index, link, check);
2940+
2941+
value = check->items[0].value;
2942+
if (ATTR_UNSET(value))
2943+
return SYMLINK_TYPE_UNSPECIFIED;
2944+
if (!strcmp(value, "file"))
2945+
return SYMLINK_TYPE_FILE;
2946+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2947+
return SYMLINK_TYPE_DIRECTORY;
2948+
2949+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2950+
return SYMLINK_TYPE_UNSPECIFIED;
2951+
}
2952+
2953+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
28742954
{
28752955
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
28762956
int len;
@@ -2890,48 +2970,31 @@ int symlink(const char *target, const char *link)
28902970
if (wtarget[len] == '/')
28912971
wtarget[len] = '\\';
28922972

2893-
/* create file symlink */
2894-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2895-
errno = err_win_to_posix(GetLastError());
2896-
return -1;
2897-
}
2898-
2899-
/* convert to directory symlink if target exists */
2900-
switch (process_phantom_symlink(wtarget, wlink)) {
2901-
case PHANTOM_SYMLINK_RETRY: {
2902-
/* if target doesn't exist, add to phantom symlinks list */
2903-
wchar_t wfullpath[MAX_LONG_PATH];
2904-
struct phantom_symlink_info *psi;
2905-
2906-
/* convert to absolute path to be independent of cwd */
2907-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2908-
if (!len || len >= MAX_LONG_PATH) {
2909-
errno = err_win_to_posix(GetLastError());
2910-
return -1;
2911-
}
2912-
2913-
/* over-allocate and fill phantom_symlink_info structure */
2914-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2915-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2916-
psi->wlink = (wchar_t *)(psi + 1);
2917-
wcscpy(psi->wlink, wfullpath);
2918-
psi->wtarget = psi->wlink + len + 1;
2919-
wcscpy(psi->wtarget, wtarget);
2920-
2921-
EnterCriticalSection(&phantom_symlinks_cs);
2922-
psi->next = phantom_symlinks;
2923-
phantom_symlinks = psi;
2924-
LeaveCriticalSection(&phantom_symlinks_cs);
2925-
break;
2926-
}
2927-
case PHANTOM_SYMLINK_DIRECTORY:
2928-
/* if we created a dir symlink, process other phantom symlinks */
2973+
switch (check_symlink_attr(index, link)) {
2974+
case SYMLINK_TYPE_UNSPECIFIED:
2975+
/* Create a phantom symlink: it is initially created as a file
2976+
* symlink, but may change to a directory symlink later if/when
2977+
* the target exists. */
2978+
return create_phantom_symlink(wtarget, wlink);
2979+
case SYMLINK_TYPE_FILE:
2980+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2981+
break;
2982+
return 0;
2983+
case SYMLINK_TYPE_DIRECTORY:
2984+
if (!CreateSymbolicLinkW(wlink, wtarget,
2985+
symlink_directory_flags))
2986+
break;
2987+
/* There may be dangling phantom symlinks that point at this
2988+
* one, which should now morph into directory symlinks. */
29292989
process_phantom_symlinks();
2930-
break;
2990+
return 0;
29312991
default:
2932-
break;
2992+
BUG("unhandled symlink type");
29332993
}
2934-
return 0;
2994+
2995+
/* CreateSymbolicLinkW failed. */
2996+
errno = err_win_to_posix(GetLastError());
2997+
return -1;
29352998
}
29362999

29373000
#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
@@ -574,6 +574,15 @@ static inline int git_has_dir_sep(const char *path)
574574
#define is_mount_point is_mount_point_via_stat
575575
#endif
576576

577+
#ifndef create_symlink
578+
struct index_state;
579+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
580+
{
581+
return symlink(target, link);
582+
}
583+
#define create_symlink git_create_symlink
584+
#endif
585+
577586
#ifndef query_user_email
578587
#define query_user_email() NULL
579588
#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
@@ -1871,7 +1871,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
18711871
#ifndef NO_SYMLINK_HEAD
18721872
char *ref_path = get_locked_file_path(&lock->lk);
18731873
unlink(ref_path);
1874-
ret = symlink(target, ref_path);
1874+
ret = create_symlink(NULL, target, ref_path);
18751875
free(ref_path);
18761876

18771877
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)