Skip to content

Commit d2c1889

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents c9f0a51 + d5f594e commit d2c1889

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

387387

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

apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4366,7 +4366,7 @@ static int try_create_file(struct apply_state *state, const char *path,
43664366
/* Although buf:size is counted string, it also is NUL
43674367
* terminated.
43684368
*/
4369-
return !!symlink(buf, path);
4369+
return !!create_symlink(state && state->repo ? state->repo->index : NULL, buf, path);
43704370

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

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
513513
}
514514
add_path(&wtdir, wtdir_len, dst_path);
515515
if (symlinks) {
516-
if (symlink(wtdir.buf, rdir.buf)) {
516+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
517517
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
518518
goto finish;
519519
}

builtin/init-db.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
7878
if (strbuf_readlink(&lnk, template_path->buf,
7979
st_template.st_size) < 0)
8080
die_errno(_("cannot readlink '%s'"), template_path->buf);
81-
if (symlink(lnk.buf, path->buf))
81+
if (create_symlink(NULL, lnk.buf, path->buf))
8282
die_errno(_("cannot symlink '%s' '%s'"),
8383
lnk.buf, path->buf);
8484
strbuf_release(&lnk);
@@ -300,7 +300,7 @@ static int create_default_files(const char *template_path,
300300
path = git_path_buf(&buf, "tXXXXXX");
301301
if (!close(xmkstemp(path)) &&
302302
!unlink(path) &&
303-
!symlink("testing", path) &&
303+
!create_symlink(NULL, "testing", path) &&
304304
!lstat(path, &st1) &&
305305
S_ISLNK(st1.st_mode))
306306
unlink(path); /* good */

compat/mingw.c

Lines changed: 103 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SECURITY_WIN32
1515
#include <sspi.h>
1616
#include "win32/fscache.h"
17+
#include "../attr.h"
1718

1819
#define HCAST(type, handle) ((type)(intptr_t)handle)
1920

@@ -429,6 +430,54 @@ static void process_phantom_symlinks(void)
429430
LeaveCriticalSection(&phantom_symlinks_cs);
430431
}
431432

433+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
434+
{
435+
int len;
436+
437+
/* create file symlink */
438+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
439+
errno = err_win_to_posix(GetLastError());
440+
return -1;
441+
}
442+
443+
/* convert to directory symlink if target exists */
444+
switch (process_phantom_symlink(wtarget, wlink)) {
445+
case PHANTOM_SYMLINK_RETRY: {
446+
/* if target doesn't exist, add to phantom symlinks list */
447+
wchar_t wfullpath[MAX_LONG_PATH];
448+
struct phantom_symlink_info *psi;
449+
450+
/* convert to absolute path to be independent of cwd */
451+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
452+
if (!len || len >= MAX_LONG_PATH) {
453+
errno = err_win_to_posix(GetLastError());
454+
return -1;
455+
}
456+
457+
/* over-allocate and fill phantom_symlink_info structure */
458+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
459+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
460+
psi->wlink = (wchar_t *)(psi + 1);
461+
wcscpy(psi->wlink, wfullpath);
462+
psi->wtarget = psi->wlink + len + 1;
463+
wcscpy(psi->wtarget, wtarget);
464+
465+
EnterCriticalSection(&phantom_symlinks_cs);
466+
psi->next = phantom_symlinks;
467+
phantom_symlinks = psi;
468+
LeaveCriticalSection(&phantom_symlinks_cs);
469+
break;
470+
}
471+
case PHANTOM_SYMLINK_DIRECTORY:
472+
/* if we created a dir symlink, process other phantom symlinks */
473+
process_phantom_symlinks();
474+
break;
475+
default:
476+
break;
477+
}
478+
return 0;
479+
}
480+
432481
/* Normalizes NT paths as returned by some low-level APIs. */
433482
static wchar_t *normalize_ntpath(wchar_t *wbuf)
434483
{
@@ -2846,7 +2895,38 @@ int link(const char *oldpath, const char *newpath)
28462895
return 0;
28472896
}
28482897

2849-
int symlink(const char *target, const char *link)
2898+
enum symlink_type {
2899+
SYMLINK_TYPE_UNSPECIFIED = 0,
2900+
SYMLINK_TYPE_FILE,
2901+
SYMLINK_TYPE_DIRECTORY,
2902+
};
2903+
2904+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2905+
{
2906+
static struct attr_check *check;
2907+
const char *value;
2908+
2909+
if (!index)
2910+
return SYMLINK_TYPE_UNSPECIFIED;
2911+
2912+
if (!check)
2913+
check = attr_check_initl("symlink", NULL);
2914+
2915+
git_check_attr(index, link, check);
2916+
2917+
value = check->items[0].value;
2918+
if (ATTR_UNSET(value))
2919+
return SYMLINK_TYPE_UNSPECIFIED;
2920+
if (!strcmp(value, "file"))
2921+
return SYMLINK_TYPE_FILE;
2922+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2923+
return SYMLINK_TYPE_DIRECTORY;
2924+
2925+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2926+
return SYMLINK_TYPE_UNSPECIFIED;
2927+
}
2928+
2929+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
28502930
{
28512931
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
28522932
int len;
@@ -2866,48 +2946,31 @@ int symlink(const char *target, const char *link)
28662946
if (wtarget[len] == '/')
28672947
wtarget[len] = '\\';
28682948

2869-
/* create file symlink */
2870-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2871-
errno = err_win_to_posix(GetLastError());
2872-
return -1;
2873-
}
2874-
2875-
/* convert to directory symlink if target exists */
2876-
switch (process_phantom_symlink(wtarget, wlink)) {
2877-
case PHANTOM_SYMLINK_RETRY: {
2878-
/* if target doesn't exist, add to phantom symlinks list */
2879-
wchar_t wfullpath[MAX_LONG_PATH];
2880-
struct phantom_symlink_info *psi;
2881-
2882-
/* convert to absolute path to be independent of cwd */
2883-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2884-
if (!len || len >= MAX_LONG_PATH) {
2885-
errno = err_win_to_posix(GetLastError());
2886-
return -1;
2887-
}
2888-
2889-
/* over-allocate and fill phantom_symlink_info structure */
2890-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2891-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2892-
psi->wlink = (wchar_t *)(psi + 1);
2893-
wcscpy(psi->wlink, wfullpath);
2894-
psi->wtarget = psi->wlink + len + 1;
2895-
wcscpy(psi->wtarget, wtarget);
2896-
2897-
EnterCriticalSection(&phantom_symlinks_cs);
2898-
psi->next = phantom_symlinks;
2899-
phantom_symlinks = psi;
2900-
LeaveCriticalSection(&phantom_symlinks_cs);
2901-
break;
2902-
}
2903-
case PHANTOM_SYMLINK_DIRECTORY:
2904-
/* if we created a dir symlink, process other phantom symlinks */
2949+
switch (check_symlink_attr(index, link)) {
2950+
case SYMLINK_TYPE_UNSPECIFIED:
2951+
/* Create a phantom symlink: it is initially created as a file
2952+
* symlink, but may change to a directory symlink later if/when
2953+
* the target exists. */
2954+
return create_phantom_symlink(wtarget, wlink);
2955+
case SYMLINK_TYPE_FILE:
2956+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2957+
break;
2958+
return 0;
2959+
case SYMLINK_TYPE_DIRECTORY:
2960+
if (!CreateSymbolicLinkW(wlink, wtarget,
2961+
symlink_directory_flags))
2962+
break;
2963+
/* There may be dangling phantom symlinks that point at this
2964+
* one, which should now morph into directory symlinks. */
29052965
process_phantom_symlinks();
2906-
break;
2966+
return 0;
29072967
default:
2908-
break;
2968+
BUG("unhandled symlink type");
29092969
}
2910-
return 0;
2970+
2971+
/* CreateSymbolicLinkW failed. */
2972+
errno = err_win_to_posix(GetLastError());
2973+
return -1;
29112974
}
29122975

29132976
#ifndef _WINNT_H

compat/mingw.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,10 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
213213
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
214214
int link(const char *oldpath, const char *newpath);
215215
int uname(struct utsname *buf);
216-
int symlink(const char *target, const char *link);
217216
int readlink(const char *path, char *buf, size_t bufsiz);
217+
struct index_state;
218+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link);
219+
#define create_symlink mingw_create_symlink
218220

219221
/*
220222
* replacements of existing functions

entry.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
305305
if (!has_symlinks || to_tempfile)
306306
goto write_file_entry;
307307

308-
ret = symlink(new_blob, path);
308+
ret = create_symlink(state->istate, new_blob, path);
309309
free(new_blob);
310310
if (ret)
311311
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
@@ -532,6 +532,15 @@ static inline int git_has_dir_sep(const char *path)
532532
#define is_mount_point is_mount_point_via_stat
533533
#endif
534534

535+
#ifndef create_symlink
536+
struct index_state;
537+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
538+
{
539+
return symlink(target, link);
540+
}
541+
#define create_symlink git_create_symlink
542+
#endif
543+
535544
#ifndef query_user_email
536545
#define query_user_email() NULL
537546
#endif

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ static int update_file_flags(struct merge_options *opt,
993993
char *lnk = xmemdupz(buf, size);
994994
safe_create_leading_directories_const(path);
995995
unlink(path);
996-
if (symlink(lnk, path))
996+
if (create_symlink(&opt->priv->orig_index, lnk, path))
997997
ret = err(opt, _("failed to symlink '%s': %s"),
998998
path, strerror(errno));
999999
free(lnk);

refs/files-backend.c

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

18861886
if (ret)

0 commit comments

Comments
 (0)