Skip to content

Commit 56a2b67

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 7dff356 + a9ed70c commit 56a2b67

File tree

11 files changed

+199
-48
lines changed

11 files changed

+199
-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
@@ -525,7 +525,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
525525
}
526526
add_path(&wtdir, wtdir_len, dst_path);
527527
if (symlinks) {
528-
if (symlink(wtdir.buf, rdir.buf)) {
528+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
529529
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
530530
goto finish;
531531
}

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
{
@@ -2917,7 +2966,38 @@ int link(const char *oldpath, const char *newpath)
29172966
return 0;
29182967
}
29192968

2920-
int symlink(const char *target, const char *link)
2969+
enum symlink_type {
2970+
SYMLINK_TYPE_UNSPECIFIED = 0,
2971+
SYMLINK_TYPE_FILE,
2972+
SYMLINK_TYPE_DIRECTORY,
2973+
};
2974+
2975+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2976+
{
2977+
static struct attr_check *check;
2978+
const char *value;
2979+
2980+
if (!index)
2981+
return SYMLINK_TYPE_UNSPECIFIED;
2982+
2983+
if (!check)
2984+
check = attr_check_initl("symlink", NULL);
2985+
2986+
git_check_attr(index, link, check);
2987+
2988+
value = check->items[0].value;
2989+
if (ATTR_UNSET(value))
2990+
return SYMLINK_TYPE_UNSPECIFIED;
2991+
if (!strcmp(value, "file"))
2992+
return SYMLINK_TYPE_FILE;
2993+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2994+
return SYMLINK_TYPE_DIRECTORY;
2995+
2996+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2997+
return SYMLINK_TYPE_UNSPECIFIED;
2998+
}
2999+
3000+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
29213001
{
29223002
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
29233003
int len;
@@ -2937,48 +3017,31 @@ int symlink(const char *target, const char *link)
29373017
if (wtarget[len] == '/')
29383018
wtarget[len] = '\\';
29393019

2940-
/* create file symlink */
2941-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2942-
errno = err_win_to_posix(GetLastError());
2943-
return -1;
2944-
}
2945-
2946-
/* convert to directory symlink if target exists */
2947-
switch (process_phantom_symlink(wtarget, wlink)) {
2948-
case PHANTOM_SYMLINK_RETRY: {
2949-
/* if target doesn't exist, add to phantom symlinks list */
2950-
wchar_t wfullpath[MAX_LONG_PATH];
2951-
struct phantom_symlink_info *psi;
2952-
2953-
/* convert to absolute path to be independent of cwd */
2954-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2955-
if (!len || len >= MAX_LONG_PATH) {
2956-
errno = err_win_to_posix(GetLastError());
2957-
return -1;
2958-
}
2959-
2960-
/* over-allocate and fill phantom_symlink_info structure */
2961-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2962-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2963-
psi->wlink = (wchar_t *)(psi + 1);
2964-
wcscpy(psi->wlink, wfullpath);
2965-
psi->wtarget = psi->wlink + len + 1;
2966-
wcscpy(psi->wtarget, wtarget);
2967-
2968-
EnterCriticalSection(&phantom_symlinks_cs);
2969-
psi->next = phantom_symlinks;
2970-
phantom_symlinks = psi;
2971-
LeaveCriticalSection(&phantom_symlinks_cs);
2972-
break;
2973-
}
2974-
case PHANTOM_SYMLINK_DIRECTORY:
2975-
/* if we created a dir symlink, process other phantom symlinks */
3020+
switch (check_symlink_attr(index, link)) {
3021+
case SYMLINK_TYPE_UNSPECIFIED:
3022+
/* Create a phantom symlink: it is initially created as a file
3023+
* symlink, but may change to a directory symlink later if/when
3024+
* the target exists. */
3025+
return create_phantom_symlink(wtarget, wlink);
3026+
case SYMLINK_TYPE_FILE:
3027+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
3028+
break;
3029+
return 0;
3030+
case SYMLINK_TYPE_DIRECTORY:
3031+
if (!CreateSymbolicLinkW(wlink, wtarget,
3032+
symlink_directory_flags))
3033+
break;
3034+
/* There may be dangling phantom symlinks that point at this
3035+
* one, which should now morph into directory symlinks. */
29763036
process_phantom_symlinks();
2977-
break;
3037+
return 0;
29783038
default:
2979-
break;
3039+
BUG("unhandled symlink type");
29803040
}
2981-
return 0;
3041+
3042+
/* CreateSymbolicLinkW failed. */
3043+
errno = err_win_to_posix(GetLastError());
3044+
return -1;
29823045
}
29833046

29843047
#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
@@ -2028,7 +2028,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
20282028

20292029
char *ref_path = get_locked_file_path(&lock->lk);
20302030
unlink(ref_path);
2031-
ret = symlink(target, ref_path);
2031+
ret = create_symlink(NULL, target, ref_path);
20322032
free(ref_path);
20332033

20342034
if (ret)

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,7 +2127,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
21272127
if (strbuf_readlink(&lnk, template_path->buf,
21282128
st_template.st_size) < 0)
21292129
die_errno(_("cannot readlink '%s'"), template_path->buf);
2130-
if (symlink(lnk.buf, path->buf))
2130+
if (create_symlink(NULL, lnk.buf, path->buf))
21312131
die_errno(_("cannot symlink '%s' '%s'"),
21322132
lnk.buf, path->buf);
21332133
strbuf_release(&lnk);
@@ -2374,7 +2374,7 @@ static int create_default_files(const char *template_path,
23742374
path = git_path_buf(&buf, "tXXXXXX");
23752375
if (!close(xmkstemp(path)) &&
23762376
!unlink(path) &&
2377-
!symlink("testing", path) &&
2377+
!create_symlink(NULL, "testing", path) &&
23782378
!lstat(path, &st1) &&
23792379
S_ISLNK(st1.st_mode))
23802380
unlink(path); /* good */

0 commit comments

Comments
 (0)