Skip to content

Commit 5a4a4b7

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 0eb68ea + ee6a445 commit 5a4a4b7

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
@@ -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
@@ -4413,7 +4413,7 @@ static int try_create_file(struct apply_state *state, const char *path,
44134413
/* Although buf:size is counted string, it also is NUL
44144414
* terminated.
44154415
*/
4416-
return !!symlink(buf, path);
4416+
return !!create_symlink(state && state->repo ? state->repo->index : NULL, buf, path);
44174417

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

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
523523
}
524524
add_path(&wtdir, wtdir_len, dst_path);
525525
if (symlinks) {
526-
if (symlink(wtdir.buf, rdir.buf)) {
526+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
527527
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
528528
goto finish;
529529
}

compat/mingw.c

Lines changed: 103 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "../write-or-die.h"
2323
#include "../repository.h"
2424
#include "win32/fscache.h"
25+
#include "../attr.h"
2526

2627
#define HCAST(type, handle) ((type)(intptr_t)handle)
2728

@@ -458,6 +459,54 @@ static void process_phantom_symlinks(void)
458459
LeaveCriticalSection(&phantom_symlinks_cs);
459460
}
460461

462+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
463+
{
464+
int len;
465+
466+
/* create file symlink */
467+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
468+
errno = err_win_to_posix(GetLastError());
469+
return -1;
470+
}
471+
472+
/* convert to directory symlink if target exists */
473+
switch (process_phantom_symlink(wtarget, wlink)) {
474+
case PHANTOM_SYMLINK_RETRY: {
475+
/* if target doesn't exist, add to phantom symlinks list */
476+
wchar_t wfullpath[MAX_LONG_PATH];
477+
struct phantom_symlink_info *psi;
478+
479+
/* convert to absolute path to be independent of cwd */
480+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
481+
if (!len || len >= MAX_LONG_PATH) {
482+
errno = err_win_to_posix(GetLastError());
483+
return -1;
484+
}
485+
486+
/* over-allocate and fill phantom_symlink_info structure */
487+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
488+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
489+
psi->wlink = (wchar_t *)(psi + 1);
490+
wcscpy(psi->wlink, wfullpath);
491+
psi->wtarget = psi->wlink + len + 1;
492+
wcscpy(psi->wtarget, wtarget);
493+
494+
EnterCriticalSection(&phantom_symlinks_cs);
495+
psi->next = phantom_symlinks;
496+
phantom_symlinks = psi;
497+
LeaveCriticalSection(&phantom_symlinks_cs);
498+
break;
499+
}
500+
case PHANTOM_SYMLINK_DIRECTORY:
501+
/* if we created a dir symlink, process other phantom symlinks */
502+
process_phantom_symlinks();
503+
break;
504+
default:
505+
break;
506+
}
507+
return 0;
508+
}
509+
461510
/* Normalizes NT paths as returned by some low-level APIs. */
462511
static wchar_t *normalize_ntpath(wchar_t *wbuf)
463512
{
@@ -2911,7 +2960,38 @@ int link(const char *oldpath, const char *newpath)
29112960
return 0;
29122961
}
29132962

2914-
int symlink(const char *target, const char *link)
2963+
enum symlink_type {
2964+
SYMLINK_TYPE_UNSPECIFIED = 0,
2965+
SYMLINK_TYPE_FILE,
2966+
SYMLINK_TYPE_DIRECTORY,
2967+
};
2968+
2969+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2970+
{
2971+
static struct attr_check *check;
2972+
const char *value;
2973+
2974+
if (!index)
2975+
return SYMLINK_TYPE_UNSPECIFIED;
2976+
2977+
if (!check)
2978+
check = attr_check_initl("symlink", NULL);
2979+
2980+
git_check_attr(index, link, check);
2981+
2982+
value = check->items[0].value;
2983+
if (ATTR_UNSET(value))
2984+
return SYMLINK_TYPE_UNSPECIFIED;
2985+
if (!strcmp(value, "file"))
2986+
return SYMLINK_TYPE_FILE;
2987+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2988+
return SYMLINK_TYPE_DIRECTORY;
2989+
2990+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2991+
return SYMLINK_TYPE_UNSPECIFIED;
2992+
}
2993+
2994+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
29152995
{
29162996
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
29172997
int len;
@@ -2931,48 +3011,31 @@ int symlink(const char *target, const char *link)
29313011
if (wtarget[len] == '/')
29323012
wtarget[len] = '\\';
29333013

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

29783041
#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
@@ -312,7 +312,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
312312
if (!has_symlinks || to_tempfile)
313313
goto write_file_entry;
314314

315-
ret = symlink(new_blob, path);
315+
ret = create_symlink(state->istate, new_blob, path);
316316
free(new_blob);
317317
if (ret)
318318
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
@@ -613,6 +613,15 @@ static inline int git_has_dir_sep(const char *path)
613613
#define is_mount_point is_mount_point_via_stat
614614
#endif
615615

616+
#ifndef create_symlink
617+
struct index_state;
618+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
619+
{
620+
return symlink(target, link);
621+
}
622+
#define create_symlink git_create_symlink
623+
#endif
624+
616625
#ifndef query_user_email
617626
#define query_user_email() NULL
618627
#endif

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ static int update_file_flags(struct merge_options *opt,
999999
char *lnk = xmemdupz(buf, size);
10001000
safe_create_leading_directories_const(path);
10011001
unlink(path);
1002-
if (symlink(lnk, path))
1002+
if (create_symlink(&opt->priv->orig_index, lnk, path))
10031003
ret = err(opt, _("failed to symlink '%s': %s"),
10041004
path, strerror(errno));
10051005
free(lnk);

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
18941894
#ifndef NO_SYMLINK_HEAD
18951895
char *ref_path = get_locked_file_path(&lock->lk);
18961896
unlink(ref_path);
1897-
ret = symlink(target, ref_path);
1897+
ret = create_symlink(NULL, target, ref_path);
18981898
free(ref_path);
18991899

19001900
if (ret)

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
19231923
if (strbuf_readlink(&lnk, template_path->buf,
19241924
st_template.st_size) < 0)
19251925
die_errno(_("cannot readlink '%s'"), template_path->buf);
1926-
if (symlink(lnk.buf, path->buf))
1926+
if (create_symlink(NULL, lnk.buf, path->buf))
19271927
die_errno(_("cannot symlink '%s' '%s'"),
19281928
lnk.buf, path->buf);
19291929
strbuf_release(&lnk);
@@ -2165,7 +2165,7 @@ static int create_default_files(const char *template_path,
21652165
path = git_path_buf(&buf, "tXXXXXX");
21662166
if (!close(xmkstemp(path)) &&
21672167
!unlink(path) &&
2168-
!symlink("testing", path) &&
2168+
!create_symlink(NULL, "testing", path) &&
21692169
!lstat(path, &st1) &&
21702170
S_ISLNK(st1.st_mode))
21712171
unlink(path); /* good */

0 commit comments

Comments
 (0)