Skip to content

Commit 52633bf

Browse files
dschomjcheetham
authored andcommitted
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 3b7241b + 3ebb19b commit 52633bf

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
@@ -4391,7 +4391,7 @@ static int try_create_file(struct apply_state *state, const char *path,
43914391
/* Although buf:size is counted string, it also is NUL
43924392
* terminated.
43934393
*/
4394-
return !!symlink(buf, path);
4394+
return !!create_symlink(state && state->repo ? state->repo->index : NULL, buf, path);
43954395

43964396
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
43974397
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
}

builtin/init-db.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
8686
if (strbuf_readlink(&lnk, template_path->buf,
8787
st_template.st_size) < 0)
8888
die_errno(_("cannot readlink '%s'"), template_path->buf);
89-
if (symlink(lnk.buf, path->buf))
89+
if (create_symlink(NULL, lnk.buf, path->buf))
9090
die_errno(_("cannot symlink '%s' '%s'"),
9191
lnk.buf, path->buf);
9292
strbuf_release(&lnk);
@@ -308,7 +308,7 @@ static int create_default_files(const char *template_path,
308308
path = git_path_buf(&buf, "tXXXXXX");
309309
if (!close(xmkstemp(path)) &&
310310
!unlink(path) &&
311-
!symlink("testing", path) &&
311+
!create_symlink(NULL, "testing", path) &&
312312
!lstat(path, &st1) &&
313313
S_ISLNK(st1.st_mode))
314314
unlink(path); /* good */

compat/mingw.c

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

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

@@ -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
{
@@ -2879,7 +2928,38 @@ int link(const char *oldpath, const char *newpath)
28792928
return 0;
28802929
}
28812930

2882-
int symlink(const char *target, const char *link)
2931+
enum symlink_type {
2932+
SYMLINK_TYPE_UNSPECIFIED = 0,
2933+
SYMLINK_TYPE_FILE,
2934+
SYMLINK_TYPE_DIRECTORY,
2935+
};
2936+
2937+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2938+
{
2939+
static struct attr_check *check;
2940+
const char *value;
2941+
2942+
if (!index)
2943+
return SYMLINK_TYPE_UNSPECIFIED;
2944+
2945+
if (!check)
2946+
check = attr_check_initl("symlink", NULL);
2947+
2948+
git_check_attr(index, link, check);
2949+
2950+
value = check->items[0].value;
2951+
if (ATTR_UNSET(value))
2952+
return SYMLINK_TYPE_UNSPECIFIED;
2953+
if (!strcmp(value, "file"))
2954+
return SYMLINK_TYPE_FILE;
2955+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2956+
return SYMLINK_TYPE_DIRECTORY;
2957+
2958+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2959+
return SYMLINK_TYPE_UNSPECIFIED;
2960+
}
2961+
2962+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
28832963
{
28842964
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
28852965
int len;
@@ -2899,48 +2979,31 @@ int symlink(const char *target, const char *link)
28992979
if (wtarget[len] == '/')
29002980
wtarget[len] = '\\';
29012981

2902-
/* create file symlink */
2903-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2904-
errno = err_win_to_posix(GetLastError());
2905-
return -1;
2906-
}
2907-
2908-
/* convert to directory symlink if target exists */
2909-
switch (process_phantom_symlink(wtarget, wlink)) {
2910-
case PHANTOM_SYMLINK_RETRY: {
2911-
/* if target doesn't exist, add to phantom symlinks list */
2912-
wchar_t wfullpath[MAX_LONG_PATH];
2913-
struct phantom_symlink_info *psi;
2914-
2915-
/* convert to absolute path to be independent of cwd */
2916-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2917-
if (!len || len >= MAX_LONG_PATH) {
2918-
errno = err_win_to_posix(GetLastError());
2919-
return -1;
2920-
}
2921-
2922-
/* over-allocate and fill phantom_symlink_info structure */
2923-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2924-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2925-
psi->wlink = (wchar_t *)(psi + 1);
2926-
wcscpy(psi->wlink, wfullpath);
2927-
psi->wtarget = psi->wlink + len + 1;
2928-
wcscpy(psi->wtarget, wtarget);
2929-
2930-
EnterCriticalSection(&phantom_symlinks_cs);
2931-
psi->next = phantom_symlinks;
2932-
phantom_symlinks = psi;
2933-
LeaveCriticalSection(&phantom_symlinks_cs);
2934-
break;
2935-
}
2936-
case PHANTOM_SYMLINK_DIRECTORY:
2937-
/* if we created a dir symlink, process other phantom symlinks */
2982+
switch (check_symlink_attr(index, link)) {
2983+
case SYMLINK_TYPE_UNSPECIFIED:
2984+
/* Create a phantom symlink: it is initially created as a file
2985+
* symlink, but may change to a directory symlink later if/when
2986+
* the target exists. */
2987+
return create_phantom_symlink(wtarget, wlink);
2988+
case SYMLINK_TYPE_FILE:
2989+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2990+
break;
2991+
return 0;
2992+
case SYMLINK_TYPE_DIRECTORY:
2993+
if (!CreateSymbolicLinkW(wlink, wtarget,
2994+
symlink_directory_flags))
2995+
break;
2996+
/* There may be dangling phantom symlinks that point at this
2997+
* one, which should now morph into directory symlinks. */
29382998
process_phantom_symlinks();
2939-
break;
2999+
return 0;
29403000
default:
2941-
break;
3001+
BUG("unhandled symlink type");
29423002
}
2943-
return 0;
3003+
3004+
/* CreateSymbolicLinkW failed. */
3005+
errno = err_win_to_posix(GetLastError());
3006+
return -1;
29443007
}
29453008

29463009
#ifndef _WINNT_H

compat/mingw.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
215215
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
216216
int link(const char *oldpath, const char *newpath);
217217
int uname(struct utsname *buf);
218-
int symlink(const char *target, const char *link);
219218
int readlink(const char *path, char *buf, size_t bufsiz);
219+
struct index_state;
220+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link);
221+
#define create_symlink mingw_create_symlink
220222

221223
/*
222224
* 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
@@ -593,6 +593,15 @@ static inline int git_has_dir_sep(const char *path)
593593
#define is_mount_point is_mount_point_via_stat
594594
#endif
595595

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

merge-recursive.c

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

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
18811881
#ifndef NO_SYMLINK_HEAD
18821882
char *ref_path = get_locked_file_path(&lock->lk);
18831883
unlink(ref_path);
1884-
ret = symlink(target, ref_path);
1884+
ret = create_symlink(NULL, target, ref_path);
18851885
free(ref_path);
18861886

18871887
if (ret)

0 commit comments

Comments
 (0)