Skip to content

Commit e181fd5

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 4f0f368 + 9d3497b commit e181fd5

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
@@ -383,6 +383,36 @@ sign `$` upon checkout. Any byte sequence that begins with
383383
with `$Id$` upon check-in.
384384

385385

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

apply.c

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

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

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
505505
}
506506
add_path(&wtdir, wtdir_len, dst_path);
507507
if (symlinks) {
508-
if (symlink(wtdir.buf, rdir.buf)) {
508+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
509509
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
510510
goto finish;
511511
}

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
@@ -10,6 +10,7 @@
1010
#include "../config.h"
1111
#include "dir.h"
1212
#include "win32/fscache.h"
13+
#include "../attr.h"
1314

1415
#define HCAST(type, handle) ((type)(intptr_t)handle)
1516

@@ -425,6 +426,54 @@ static void process_phantom_symlinks(void)
425426
LeaveCriticalSection(&phantom_symlinks_cs);
426427
}
427428

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

2775-
int symlink(const char *target, const char *link)
2824+
enum symlink_type {
2825+
SYMLINK_TYPE_UNSPECIFIED = 0,
2826+
SYMLINK_TYPE_FILE,
2827+
SYMLINK_TYPE_DIRECTORY,
2828+
};
2829+
2830+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2831+
{
2832+
static struct attr_check *check;
2833+
const char *value;
2834+
2835+
if (!index)
2836+
return SYMLINK_TYPE_UNSPECIFIED;
2837+
2838+
if (!check)
2839+
check = attr_check_initl("symlink", NULL);
2840+
2841+
git_check_attr(index, link, check);
2842+
2843+
value = check->items[0].value;
2844+
if (ATTR_UNSET(value))
2845+
return SYMLINK_TYPE_UNSPECIFIED;
2846+
if (!strcmp(value, "file"))
2847+
return SYMLINK_TYPE_FILE;
2848+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2849+
return SYMLINK_TYPE_DIRECTORY;
2850+
2851+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2852+
return SYMLINK_TYPE_UNSPECIFIED;
2853+
}
2854+
2855+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
27762856
{
27772857
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
27782858
int len;
@@ -2792,48 +2872,31 @@ int symlink(const char *target, const char *link)
27922872
if (wtarget[len] == '/')
27932873
wtarget[len] = '\\';
27942874

2795-
/* create file symlink */
2796-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2797-
errno = err_win_to_posix(GetLastError());
2798-
return -1;
2799-
}
2800-
2801-
/* convert to directory symlink if target exists */
2802-
switch (process_phantom_symlink(wtarget, wlink)) {
2803-
case PHANTOM_SYMLINK_RETRY: {
2804-
/* if target doesn't exist, add to phantom symlinks list */
2805-
wchar_t wfullpath[MAX_LONG_PATH];
2806-
struct phantom_symlink_info *psi;
2807-
2808-
/* convert to absolute path to be independent of cwd */
2809-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2810-
if (!len || len >= MAX_LONG_PATH) {
2811-
errno = err_win_to_posix(GetLastError());
2812-
return -1;
2813-
}
2814-
2815-
/* over-allocate and fill phantom_symlink_info structure */
2816-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2817-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2818-
psi->wlink = (wchar_t *)(psi + 1);
2819-
wcscpy(psi->wlink, wfullpath);
2820-
psi->wtarget = psi->wlink + len + 1;
2821-
wcscpy(psi->wtarget, wtarget);
2822-
2823-
EnterCriticalSection(&phantom_symlinks_cs);
2824-
psi->next = phantom_symlinks;
2825-
phantom_symlinks = psi;
2826-
LeaveCriticalSection(&phantom_symlinks_cs);
2827-
break;
2828-
}
2829-
case PHANTOM_SYMLINK_DIRECTORY:
2830-
/* if we created a dir symlink, process other phantom symlinks */
2875+
switch (check_symlink_attr(index, link)) {
2876+
case SYMLINK_TYPE_UNSPECIFIED:
2877+
/* Create a phantom symlink: it is initially created as a file
2878+
* symlink, but may change to a directory symlink later if/when
2879+
* the target exists. */
2880+
return create_phantom_symlink(wtarget, wlink);
2881+
case SYMLINK_TYPE_FILE:
2882+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2883+
break;
2884+
return 0;
2885+
case SYMLINK_TYPE_DIRECTORY:
2886+
if (!CreateSymbolicLinkW(wlink, wtarget,
2887+
symlink_directory_flags))
2888+
break;
2889+
/* There may be dangling phantom symlinks that point at this
2890+
* one, which should now morph into directory symlinks. */
28312891
process_phantom_symlinks();
2832-
break;
2892+
return 0;
28332893
default:
2834-
break;
2894+
BUG("unhandled symlink type");
28352895
}
2836-
return 0;
2896+
2897+
/* CreateSymbolicLinkW failed. */
2898+
errno = err_win_to_posix(GetLastError());
2899+
return -1;
28372900
}
28382901

28392902
#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
@@ -302,7 +302,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
302302
if (!has_symlinks || to_tempfile)
303303
goto write_file_entry;
304304

305-
ret = symlink(new_blob, path);
305+
ret = create_symlink(state->istate, new_blob, path);
306306
free(new_blob);
307307
if (ret)
308308
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
@@ -417,6 +417,15 @@ static inline int git_has_dir_sep(const char *path)
417417
#define is_mount_point is_mount_point_via_stat
418418
#endif
419419

420+
#ifndef create_symlink
421+
struct index_state;
422+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
423+
{
424+
return symlink(target, link);
425+
}
426+
#define create_symlink git_create_symlink
427+
#endif
428+
420429
#ifndef query_user_email
421430
#define query_user_email() NULL
422431
#endif

merge-recursive.c

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

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
17881788
#ifndef NO_SYMLINK_HEAD
17891789
char *ref_path = get_locked_file_path(&lock->lk);
17901790
unlink(ref_path);
1791-
ret = symlink(target, ref_path);
1791+
ret = create_symlink(NULL, target, ref_path);
17921792
free(ref_path);
17931793

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