Skip to content

Commit b753461

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 3107ebf + 7d4a60b commit b753461

File tree

12 files changed

+200
-48
lines changed

12 files changed

+200
-48
lines changed

Documentation/gitattributes.adoc

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
@@ -4398,7 +4398,7 @@ static int try_create_file(struct apply_state *state, const char *path,
43984398
/* Although buf:size is counted string, it also is NUL
43994399
* terminated.
44004400
*/
4401-
return !!symlink(buf, path);
4401+
return !!create_symlink(state && state->repo ? state->repo->index : NULL, buf, path);
44024402

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

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ static int run_dir_diff(struct repository *repo,
541541
}
542542
add_path(&wtdir, wtdir_len, dst_path);
543543
if (dt_options->symlinks) {
544-
if (symlink(wtdir.buf, rdir.buf)) {
544+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
545545
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
546546
goto finish;
547547
}

compat/mingw.c

Lines changed: 103 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "../write-or-die.h"
2626
#include "../repository.h"
2727
#include "win32/fscache.h"
28+
#include "../attr.h"
2829

2930
#define HCAST(type, handle) ((type)(intptr_t)handle)
3031

@@ -461,6 +462,54 @@ static void process_phantom_symlinks(void)
461462
LeaveCriticalSection(&phantom_symlinks_cs);
462463
}
463464

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

3156-
int symlink(const char *target, const char *link)
3205+
enum symlink_type {
3206+
SYMLINK_TYPE_UNSPECIFIED = 0,
3207+
SYMLINK_TYPE_FILE,
3208+
SYMLINK_TYPE_DIRECTORY,
3209+
};
3210+
3211+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
3212+
{
3213+
static struct attr_check *check;
3214+
const char *value;
3215+
3216+
if (!index)
3217+
return SYMLINK_TYPE_UNSPECIFIED;
3218+
3219+
if (!check)
3220+
check = attr_check_initl("symlink", NULL);
3221+
3222+
git_check_attr(index, link, check);
3223+
3224+
value = check->items[0].value;
3225+
if (ATTR_UNSET(value))
3226+
return SYMLINK_TYPE_UNSPECIFIED;
3227+
if (!strcmp(value, "file"))
3228+
return SYMLINK_TYPE_FILE;
3229+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
3230+
return SYMLINK_TYPE_DIRECTORY;
3231+
3232+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
3233+
return SYMLINK_TYPE_UNSPECIFIED;
3234+
}
3235+
3236+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
31573237
{
31583238
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
31593239
int len;
@@ -3173,48 +3253,31 @@ int symlink(const char *target, const char *link)
31733253
if (wtarget[len] == '/')
31743254
wtarget[len] = '\\';
31753255

3176-
/* create file symlink */
3177-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
3178-
errno = err_win_to_posix(GetLastError());
3179-
return -1;
3180-
}
3181-
3182-
/* convert to directory symlink if target exists */
3183-
switch (process_phantom_symlink(wtarget, wlink)) {
3184-
case PHANTOM_SYMLINK_RETRY: {
3185-
/* if target doesn't exist, add to phantom symlinks list */
3186-
wchar_t wfullpath[MAX_LONG_PATH];
3187-
struct phantom_symlink_info *psi;
3188-
3189-
/* convert to absolute path to be independent of cwd */
3190-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
3191-
if (!len || len >= MAX_LONG_PATH) {
3192-
errno = err_win_to_posix(GetLastError());
3193-
return -1;
3194-
}
3195-
3196-
/* over-allocate and fill phantom_symlink_info structure */
3197-
psi = xmalloc(sizeof(struct phantom_symlink_info)
3198-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
3199-
psi->wlink = (wchar_t *)(psi + 1);
3200-
wcscpy(psi->wlink, wfullpath);
3201-
psi->wtarget = psi->wlink + len + 1;
3202-
wcscpy(psi->wtarget, wtarget);
3203-
3204-
EnterCriticalSection(&phantom_symlinks_cs);
3205-
psi->next = phantom_symlinks;
3206-
phantom_symlinks = psi;
3207-
LeaveCriticalSection(&phantom_symlinks_cs);
3208-
break;
3209-
}
3210-
case PHANTOM_SYMLINK_DIRECTORY:
3211-
/* if we created a dir symlink, process other phantom symlinks */
3256+
switch (check_symlink_attr(index, link)) {
3257+
case SYMLINK_TYPE_UNSPECIFIED:
3258+
/* Create a phantom symlink: it is initially created as a file
3259+
* symlink, but may change to a directory symlink later if/when
3260+
* the target exists. */
3261+
return create_phantom_symlink(wtarget, wlink);
3262+
case SYMLINK_TYPE_FILE:
3263+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
3264+
break;
3265+
return 0;
3266+
case SYMLINK_TYPE_DIRECTORY:
3267+
if (!CreateSymbolicLinkW(wlink, wtarget,
3268+
symlink_directory_flags))
3269+
break;
3270+
/* There may be dangling phantom symlinks that point at this
3271+
* one, which should now morph into directory symlinks. */
32123272
process_phantom_symlinks();
3213-
break;
3273+
return 0;
32143274
default:
3215-
break;
3275+
BUG("unhandled symlink type");
32163276
}
3217-
return 0;
3277+
3278+
/* CreateSymbolicLinkW failed. */
3279+
errno = err_win_to_posix(GetLastError());
3280+
return -1;
32183281
}
32193282

32203283
#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
@@ -324,7 +324,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
324324
if (!has_symlinks || to_tempfile)
325325
goto write_file_entry;
326326

327-
ret = symlink(new_blob, path);
327+
ret = create_symlink(state->istate, new_blob, path);
328328
free(new_blob);
329329
if (ret)
330330
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
@@ -651,6 +651,16 @@ static inline int git_has_dir_sep(const char *path)
651651
#define is_mount_point is_mount_point_via_stat
652652
#endif
653653

654+
#ifndef create_symlink
655+
struct index_state;
656+
static inline int git_create_symlink(struct index_state *index UNUSED,
657+
const char *target, const char *link)
658+
{
659+
return symlink(target, link);
660+
}
661+
#define create_symlink git_create_symlink
662+
#endif
663+
654664
#ifndef query_user_email
655665
#define query_user_email() NULL
656666
#endif

merge-recursive.c

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

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
20442044

20452045
char *ref_path = get_locked_file_path(&lock->lk);
20462046
unlink(ref_path);
2047-
ret = symlink(target, ref_path);
2047+
ret = create_symlink(NULL, target, ref_path);
20482048
free(ref_path);
20492049

20502050
if (ret)

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
21352135
if (strbuf_readlink(&lnk, template_path->buf,
21362136
st_template.st_size) < 0)
21372137
die_errno(_("cannot readlink '%s'"), template_path->buf);
2138-
if (symlink(lnk.buf, path->buf))
2138+
if (create_symlink(NULL, lnk.buf, path->buf))
21392139
die_errno(_("cannot symlink '%s' '%s'"),
21402140
lnk.buf, path->buf);
21412141
strbuf_release(&lnk);
@@ -2396,7 +2396,7 @@ static int create_default_files(const char *template_path,
23962396
path = git_path_buf(&buf, "tXXXXXX");
23972397
if (!close(xmkstemp(path)) &&
23982398
!unlink(path) &&
2399-
!symlink("testing", path) &&
2399+
!create_symlink(NULL, "testing", path) &&
24002400
!lstat(path, &st1) &&
24012401
S_ISLNK(st1.st_mode))
24022402
unlink(path); /* good */

0 commit comments

Comments
 (0)