Skip to content

Commit 33cc0f0

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents ee130a5 + 23d4205 commit 33cc0f0

File tree

11 files changed

+199
-48
lines changed

11 files changed

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

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

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

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

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

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

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

325-
ret = symlink(new_blob, path);
325+
ret = create_symlink(state->istate, new_blob, path);
326326
free(new_blob);
327327
if (ret)
328328
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
@@ -641,6 +641,16 @@ static inline int git_has_dir_sep(const char *path)
641641
#define is_mount_point is_mount_point_via_stat
642642
#endif
643643

644+
#ifndef create_symlink
645+
struct index_state;
646+
static inline int git_create_symlink(struct index_state *index UNUSED,
647+
const char *target, const char *link)
648+
{
649+
return symlink(target, link);
650+
}
651+
#define create_symlink git_create_symlink
652+
#endif
653+
644654
#ifndef query_user_email
645655
#define query_user_email() NULL
646656
#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
@@ -2020,7 +2020,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
20202020

20212021
char *ref_path = get_locked_file_path(&lock->lk);
20222022
unlink(ref_path);
2023-
ret = symlink(target, ref_path);
2023+
ret = create_symlink(NULL, target, ref_path);
20242024
free(ref_path);
20252025

20262026
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)