Skip to content

Commit 7dabef3

Browse files
dschovdye
authored andcommitted
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 8cfd8f0 + 07167b3 commit 7dabef3

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
@@ -13,6 +13,7 @@
1313
#define SECURITY_WIN32
1414
#include <sspi.h>
1515
#include "win32/fscache.h"
16+
#include "../attr.h"
1617

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

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

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

2820-
int symlink(const char *target, const char *link)
2869+
enum symlink_type {
2870+
SYMLINK_TYPE_UNSPECIFIED = 0,
2871+
SYMLINK_TYPE_FILE,
2872+
SYMLINK_TYPE_DIRECTORY,
2873+
};
2874+
2875+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2876+
{
2877+
static struct attr_check *check;
2878+
const char *value;
2879+
2880+
if (!index)
2881+
return SYMLINK_TYPE_UNSPECIFIED;
2882+
2883+
if (!check)
2884+
check = attr_check_initl("symlink", NULL);
2885+
2886+
git_check_attr(index, link, check);
2887+
2888+
value = check->items[0].value;
2889+
if (ATTR_UNSET(value))
2890+
return SYMLINK_TYPE_UNSPECIFIED;
2891+
if (!strcmp(value, "file"))
2892+
return SYMLINK_TYPE_FILE;
2893+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2894+
return SYMLINK_TYPE_DIRECTORY;
2895+
2896+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2897+
return SYMLINK_TYPE_UNSPECIFIED;
2898+
}
2899+
2900+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
28212901
{
28222902
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
28232903
int len;
@@ -2837,48 +2917,31 @@ int symlink(const char *target, const char *link)
28372917
if (wtarget[len] == '/')
28382918
wtarget[len] = '\\';
28392919

2840-
/* create file symlink */
2841-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2842-
errno = err_win_to_posix(GetLastError());
2843-
return -1;
2844-
}
2845-
2846-
/* convert to directory symlink if target exists */
2847-
switch (process_phantom_symlink(wtarget, wlink)) {
2848-
case PHANTOM_SYMLINK_RETRY: {
2849-
/* if target doesn't exist, add to phantom symlinks list */
2850-
wchar_t wfullpath[MAX_LONG_PATH];
2851-
struct phantom_symlink_info *psi;
2852-
2853-
/* convert to absolute path to be independent of cwd */
2854-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2855-
if (!len || len >= MAX_LONG_PATH) {
2856-
errno = err_win_to_posix(GetLastError());
2857-
return -1;
2858-
}
2859-
2860-
/* over-allocate and fill phantom_symlink_info structure */
2861-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2862-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2863-
psi->wlink = (wchar_t *)(psi + 1);
2864-
wcscpy(psi->wlink, wfullpath);
2865-
psi->wtarget = psi->wlink + len + 1;
2866-
wcscpy(psi->wtarget, wtarget);
2867-
2868-
EnterCriticalSection(&phantom_symlinks_cs);
2869-
psi->next = phantom_symlinks;
2870-
phantom_symlinks = psi;
2871-
LeaveCriticalSection(&phantom_symlinks_cs);
2872-
break;
2873-
}
2874-
case PHANTOM_SYMLINK_DIRECTORY:
2875-
/* if we created a dir symlink, process other phantom symlinks */
2920+
switch (check_symlink_attr(index, link)) {
2921+
case SYMLINK_TYPE_UNSPECIFIED:
2922+
/* Create a phantom symlink: it is initially created as a file
2923+
* symlink, but may change to a directory symlink later if/when
2924+
* the target exists. */
2925+
return create_phantom_symlink(wtarget, wlink);
2926+
case SYMLINK_TYPE_FILE:
2927+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2928+
break;
2929+
return 0;
2930+
case SYMLINK_TYPE_DIRECTORY:
2931+
if (!CreateSymbolicLinkW(wlink, wtarget,
2932+
symlink_directory_flags))
2933+
break;
2934+
/* There may be dangling phantom symlinks that point at this
2935+
* one, which should now morph into directory symlinks. */
28762936
process_phantom_symlinks();
2877-
break;
2937+
return 0;
28782938
default:
2879-
break;
2939+
BUG("unhandled symlink type");
28802940
}
2881-
return 0;
2941+
2942+
/* CreateSymbolicLinkW failed. */
2943+
errno = err_win_to_posix(GetLastError());
2944+
return -1;
28822945
}
28832946

28842947
#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
@@ -470,6 +470,15 @@ static inline int git_has_dir_sep(const char *path)
470470
#define is_mount_point is_mount_point_via_stat
471471
#endif
472472

473+
#ifndef create_symlink
474+
struct index_state;
475+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
476+
{
477+
return symlink(target, link);
478+
}
479+
#define create_symlink git_create_symlink
480+
#endif
481+
473482
#ifndef query_user_email
474483
#define query_user_email() NULL
475484
#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)