Skip to content

Commit 341abd6

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents cb90430 + 522baa3 commit 341abd6

File tree

11 files changed

+203
-48
lines changed

11 files changed

+203
-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
@@ -4340,7 +4340,7 @@ static int try_create_file(struct apply_state *state, const char *path,
43404340
/* Although buf:size is counted string, it also is NUL
43414341
* terminated.
43424342
*/
4343-
return !!symlink(buf, path);
4343+
return !!create_symlink(state && state->repo ? state->repo->index : NULL, buf, path);
43444344

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

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
498498
}
499499
add_path(&wtdir, wtdir_len, dst_path);
500500
if (symlinks) {
501-
if (symlink(wtdir.buf, rdir.buf)) {
501+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
502502
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
503503
goto finish;
504504
}

builtin/init-db.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
7676
if (strbuf_readlink(&lnk, template_path->buf,
7777
st_template.st_size) < 0)
7878
die_errno(_("cannot readlink '%s'"), template_path->buf);
79-
if (symlink(lnk.buf, path->buf))
79+
if (create_symlink(NULL, lnk.buf, path->buf))
8080
die_errno(_("cannot symlink '%s' '%s'"),
8181
lnk.buf, path->buf);
8282
strbuf_release(&lnk);
@@ -280,7 +280,7 @@ static int create_default_files(const char *template_path,
280280
path = git_path_buf(&buf, "tXXXXXX");
281281
if (!close(xmkstemp(path)) &&
282282
!unlink(path) &&
283-
!symlink("testing", path) &&
283+
!create_symlink(NULL, "testing", path) &&
284284
!lstat(path, &st1) &&
285285
S_ISLNK(st1.st_mode))
286286
unlink(path); /* good */

compat/mingw.c

Lines changed: 103 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "../config.h"
1313
#include "dir.h"
1414
#include "win32/fscache.h"
15+
#include "../attr.h"
1516

1617
#define HCAST(type, handle) ((type)(intptr_t)handle)
1718

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

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

2496-
int symlink(const char *target, const char *link)
2545+
enum symlink_type {
2546+
SYMLINK_TYPE_UNSPECIFIED = 0,
2547+
SYMLINK_TYPE_FILE,
2548+
SYMLINK_TYPE_DIRECTORY,
2549+
};
2550+
2551+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2552+
{
2553+
static struct attr_check *check;
2554+
const char *value;
2555+
2556+
if (!index)
2557+
return SYMLINK_TYPE_UNSPECIFIED;
2558+
2559+
if (!check)
2560+
check = attr_check_initl("symlink", NULL);
2561+
2562+
git_check_attr(index, link, check);
2563+
2564+
value = check->items[0].value;
2565+
if (ATTR_UNSET(value))
2566+
return SYMLINK_TYPE_UNSPECIFIED;
2567+
if (!strcmp(value, "file"))
2568+
return SYMLINK_TYPE_FILE;
2569+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2570+
return SYMLINK_TYPE_DIRECTORY;
2571+
2572+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2573+
return SYMLINK_TYPE_UNSPECIFIED;
2574+
}
2575+
2576+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
24972577
{
24982578
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
24992579
int len;
@@ -2513,48 +2593,31 @@ int symlink(const char *target, const char *link)
25132593
if (wtarget[len] == '/')
25142594
wtarget[len] = '\\';
25152595

2516-
/* create file symlink */
2517-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2518-
errno = err_win_to_posix(GetLastError());
2519-
return -1;
2520-
}
2521-
2522-
/* convert to directory symlink if target exists */
2523-
switch (process_phantom_symlink(wtarget, wlink)) {
2524-
case PHANTOM_SYMLINK_RETRY: {
2525-
/* if target doesn't exist, add to phantom symlinks list */
2526-
wchar_t wfullpath[MAX_LONG_PATH];
2527-
struct phantom_symlink_info *psi;
2528-
2529-
/* convert to absolute path to be independent of cwd */
2530-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2531-
if (!len || len >= MAX_LONG_PATH) {
2532-
errno = err_win_to_posix(GetLastError());
2533-
return -1;
2534-
}
2535-
2536-
/* over-allocate and fill phantom_symlink_info structure */
2537-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2538-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2539-
psi->wlink = (wchar_t *)(psi + 1);
2540-
wcscpy(psi->wlink, wfullpath);
2541-
psi->wtarget = psi->wlink + len + 1;
2542-
wcscpy(psi->wtarget, wtarget);
2543-
2544-
EnterCriticalSection(&phantom_symlinks_cs);
2545-
psi->next = phantom_symlinks;
2546-
phantom_symlinks = psi;
2547-
LeaveCriticalSection(&phantom_symlinks_cs);
2548-
break;
2549-
}
2550-
case PHANTOM_SYMLINK_DIRECTORY:
2551-
/* if we created a dir symlink, process other phantom symlinks */
2596+
switch (check_symlink_attr(index, link)) {
2597+
case SYMLINK_TYPE_UNSPECIFIED:
2598+
/* Create a phantom symlink: it is initially created as a file
2599+
* symlink, but may change to a directory symlink later if/when
2600+
* the target exists. */
2601+
return create_phantom_symlink(wtarget, wlink);
2602+
case SYMLINK_TYPE_FILE:
2603+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2604+
break;
2605+
return 0;
2606+
case SYMLINK_TYPE_DIRECTORY:
2607+
if (!CreateSymbolicLinkW(wlink, wtarget,
2608+
symlink_directory_flags))
2609+
break;
2610+
/* There may be dangling phantom symlinks that point at this
2611+
* one, which should now morph into directory symlinks. */
25522612
process_phantom_symlinks();
2553-
break;
2613+
return 0;
25542614
default:
2555-
break;
2615+
BUG("unhandled symlink type");
25562616
}
2557-
return 0;
2617+
2618+
/* CreateSymbolicLinkW failed. */
2619+
errno = err_win_to_posix(GetLastError());
2620+
return -1;
25582621
}
25592622

25602623
#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
@@ -289,7 +289,7 @@ static int write_entry(struct cache_entry *ce,
289289
if (!has_symlinks || to_tempfile)
290290
goto write_file_entry;
291291

292-
ret = symlink(new_blob, path);
292+
ret = create_symlink(state->istate, new_blob, path);
293293
free(new_blob);
294294
if (ret)
295295
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
@@ -410,6 +410,15 @@ static inline char *git_find_last_dir_sep(const char *path)
410410
#define is_mount_point is_mount_point_via_stat
411411
#endif
412412

413+
#ifndef create_symlink
414+
struct index_state;
415+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
416+
{
417+
return symlink(target, link);
418+
}
419+
#define create_symlink git_create_symlink
420+
#endif
421+
413422
#ifndef query_user_email
414423
#define query_user_email() NULL
415424
#endif

merge-recursive.c

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

refs/files-backend.c

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

17981798
if (ret)

t/t2040-checkout-symlink-attr.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
# MSYS2 is very forgiving, it will resolve symlinks even if the
27+
# symlink type isn't correct. To make this test meaningful, try
28+
# them with a native, non-MSYS executable.
29+
cat_native () {
30+
filename=$(cygpath -w "$1") &&
31+
cmd.exe /c "type \"$filename\""
32+
}
33+
34+
test_expect_success 'checkout symlinks with attr' '
35+
cache_symlink file1 file-link &&
36+
cache_symlink dir dir-link &&
37+
38+
printf "file-link symlink=file\ndir-link symlink=dir\n" >.gitattributes &&
39+
git add .gitattributes &&
40+
41+
git checkout . &&
42+
43+
mkdir dir &&
44+
echo "contents1" >file1 &&
45+
echo "contents2" >dir/file2 &&
46+
47+
test "$(cat_native file-link)" = "contents1" &&
48+
test "$(cat_native dir-link/file2)" = "contents2"
49+
'
50+
51+
test_done

0 commit comments

Comments
 (0)