Skip to content

Commit 9d3bc8d

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 8dfed81 + 1cf49e5 commit 9d3bc8d

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
@@ -4440,7 +4440,7 @@ static int try_create_file(struct apply_state *state, const char *path,
44404440
/* Although buf:size is counted string, it also is NUL
44414441
* terminated.
44424442
*/
4443-
return !!symlink(buf, path);
4443+
return !!create_symlink(state && state->repo ? state->repo->index : NULL, buf, path);
44444444

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

@@ -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
{
@@ -2915,7 +2964,38 @@ int link(const char *oldpath, const char *newpath)
29152964
return 0;
29162965
}
29172966

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

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

29823045
#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
@@ -320,7 +320,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
320320
if (!has_symlinks || to_tempfile)
321321
goto write_file_entry;
322322

323-
ret = symlink(new_blob, path);
323+
ret = create_symlink(state->istate, new_blob, path);
324324
free(new_blob);
325325
if (ret)
326326
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
@@ -627,6 +627,15 @@ static inline int git_has_dir_sep(const char *path)
627627
#define is_mount_point is_mount_point_via_stat
628628
#endif
629629

630+
#ifndef create_symlink
631+
struct index_state;
632+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
633+
{
634+
return symlink(target, link);
635+
}
636+
#define create_symlink git_create_symlink
637+
#endif
638+
630639
#ifndef query_user_email
631640
#define query_user_email() NULL
632641
#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
@@ -1942,7 +1942,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
19421942
#ifndef NO_SYMLINK_HEAD
19431943
char *ref_path = get_locked_file_path(&lock->lk);
19441944
unlink(ref_path);
1945-
ret = symlink(target, ref_path);
1945+
ret = create_symlink(NULL, target, ref_path);
19461946
free(ref_path);
19471947

19481948
if (ret)

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
20212021
if (strbuf_readlink(&lnk, template_path->buf,
20222022
st_template.st_size) < 0)
20232023
die_errno(_("cannot readlink '%s'"), template_path->buf);
2024-
if (symlink(lnk.buf, path->buf))
2024+
if (create_symlink(NULL, lnk.buf, path->buf))
20252025
die_errno(_("cannot symlink '%s' '%s'"),
20262026
lnk.buf, path->buf);
20272027
strbuf_release(&lnk);
@@ -2268,7 +2268,7 @@ static int create_default_files(const char *template_path,
22682268
path = git_path_buf(&buf, "tXXXXXX");
22692269
if (!close(xmkstemp(path)) &&
22702270
!unlink(path) &&
2271-
!symlink("testing", path) &&
2271+
!create_symlink(NULL, "testing", path) &&
22722272
!lstat(path, &st1) &&
22732273
S_ISLNK(st1.st_mode))
22742274
unlink(path); /* good */

0 commit comments

Comments
 (0)