Skip to content

Commit 0d5affc

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 34caab4 + 1aaa51e commit 0d5affc

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

390390

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

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

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

@@ -456,6 +457,54 @@ static void process_phantom_symlinks(void)
456457
LeaveCriticalSection(&phantom_symlinks_cs);
457458
}
458459

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

2900-
int symlink(const char *target, const char *link)
2949+
enum symlink_type {
2950+
SYMLINK_TYPE_UNSPECIFIED = 0,
2951+
SYMLINK_TYPE_FILE,
2952+
SYMLINK_TYPE_DIRECTORY,
2953+
};
2954+
2955+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2956+
{
2957+
static struct attr_check *check;
2958+
const char *value;
2959+
2960+
if (!index)
2961+
return SYMLINK_TYPE_UNSPECIFIED;
2962+
2963+
if (!check)
2964+
check = attr_check_initl("symlink", NULL);
2965+
2966+
git_check_attr(index, link, check);
2967+
2968+
value = check->items[0].value;
2969+
if (ATTR_UNSET(value))
2970+
return SYMLINK_TYPE_UNSPECIFIED;
2971+
if (!strcmp(value, "file"))
2972+
return SYMLINK_TYPE_FILE;
2973+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2974+
return SYMLINK_TYPE_DIRECTORY;
2975+
2976+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2977+
return SYMLINK_TYPE_UNSPECIFIED;
2978+
}
2979+
2980+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
29012981
{
29022982
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
29032983
int len;
@@ -2917,48 +2997,31 @@ int symlink(const char *target, const char *link)
29172997
if (wtarget[len] == '/')
29182998
wtarget[len] = '\\';
29192999

2920-
/* create file symlink */
2921-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2922-
errno = err_win_to_posix(GetLastError());
2923-
return -1;
2924-
}
2925-
2926-
/* convert to directory symlink if target exists */
2927-
switch (process_phantom_symlink(wtarget, wlink)) {
2928-
case PHANTOM_SYMLINK_RETRY: {
2929-
/* if target doesn't exist, add to phantom symlinks list */
2930-
wchar_t wfullpath[MAX_LONG_PATH];
2931-
struct phantom_symlink_info *psi;
2932-
2933-
/* convert to absolute path to be independent of cwd */
2934-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2935-
if (!len || len >= MAX_LONG_PATH) {
2936-
errno = err_win_to_posix(GetLastError());
2937-
return -1;
2938-
}
2939-
2940-
/* over-allocate and fill phantom_symlink_info structure */
2941-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2942-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2943-
psi->wlink = (wchar_t *)(psi + 1);
2944-
wcscpy(psi->wlink, wfullpath);
2945-
psi->wtarget = psi->wlink + len + 1;
2946-
wcscpy(psi->wtarget, wtarget);
2947-
2948-
EnterCriticalSection(&phantom_symlinks_cs);
2949-
psi->next = phantom_symlinks;
2950-
phantom_symlinks = psi;
2951-
LeaveCriticalSection(&phantom_symlinks_cs);
2952-
break;
2953-
}
2954-
case PHANTOM_SYMLINK_DIRECTORY:
2955-
/* if we created a dir symlink, process other phantom symlinks */
3000+
switch (check_symlink_attr(index, link)) {
3001+
case SYMLINK_TYPE_UNSPECIFIED:
3002+
/* Create a phantom symlink: it is initially created as a file
3003+
* symlink, but may change to a directory symlink later if/when
3004+
* the target exists. */
3005+
return create_phantom_symlink(wtarget, wlink);
3006+
case SYMLINK_TYPE_FILE:
3007+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
3008+
break;
3009+
return 0;
3010+
case SYMLINK_TYPE_DIRECTORY:
3011+
if (!CreateSymbolicLinkW(wlink, wtarget,
3012+
symlink_directory_flags))
3013+
break;
3014+
/* There may be dangling phantom symlinks that point at this
3015+
* one, which should now morph into directory symlinks. */
29563016
process_phantom_symlinks();
2957-
break;
3017+
return 0;
29583018
default:
2959-
break;
3019+
BUG("unhandled symlink type");
29603020
}
2961-
return 0;
3021+
3022+
/* CreateSymbolicLinkW failed. */
3023+
errno = err_win_to_posix(GetLastError());
3024+
return -1;
29623025
}
29633026

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

316-
ret = symlink(new_blob, path);
316+
ret = create_symlink(state->istate, new_blob, path);
317317
free(new_blob);
318318
if (ret)
319319
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
@@ -595,6 +595,15 @@ static inline int git_has_dir_sep(const char *path)
595595
#define is_mount_point is_mount_point_via_stat
596596
#endif
597597

598+
#ifndef create_symlink
599+
struct index_state;
600+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
601+
{
602+
return symlink(target, link);
603+
}
604+
#define create_symlink git_create_symlink
605+
#endif
606+
598607
#ifndef query_user_email
599608
#define query_user_email() NULL
600609
#endif

merge-recursive.c

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

refs/files-backend.c

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

18961896
if (ret)

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
17851785
if (strbuf_readlink(&lnk, template_path->buf,
17861786
st_template.st_size) < 0)
17871787
die_errno(_("cannot readlink '%s'"), template_path->buf);
1788-
if (symlink(lnk.buf, path->buf))
1788+
if (create_symlink(NULL, lnk.buf, path->buf))
17891789
die_errno(_("cannot symlink '%s' '%s'"),
17901790
lnk.buf, path->buf);
17911791
strbuf_release(&lnk);
@@ -2036,7 +2036,7 @@ static int create_default_files(const char *template_path,
20362036
path = git_path_buf(&buf, "tXXXXXX");
20372037
if (!close(xmkstemp(path)) &&
20382038
!unlink(path) &&
2039-
!symlink("testing", path) &&
2039+
!create_symlink(NULL, "testing", path) &&
20402040
!lstat(path, &st1) &&
20412041
S_ISLNK(st1.st_mode))
20422042
unlink(path); /* good */

0 commit comments

Comments
 (0)