Skip to content

Commit bfac9b2

Browse files
committed
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 48a32ad + 9deafeb commit bfac9b2

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

385385

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

apply.c

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

43894389
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
43904390
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
{
@@ -2802,7 +2851,38 @@ int link(const char *oldpath, const char *newpath)
28022851
return 0;
28032852
}
28042853

2805-
int symlink(const char *target, const char *link)
2854+
enum symlink_type {
2855+
SYMLINK_TYPE_UNSPECIFIED = 0,
2856+
SYMLINK_TYPE_FILE,
2857+
SYMLINK_TYPE_DIRECTORY,
2858+
};
2859+
2860+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2861+
{
2862+
static struct attr_check *check;
2863+
const char *value;
2864+
2865+
if (!index)
2866+
return SYMLINK_TYPE_UNSPECIFIED;
2867+
2868+
if (!check)
2869+
check = attr_check_initl("symlink", NULL);
2870+
2871+
git_check_attr(index, link, check);
2872+
2873+
value = check->items[0].value;
2874+
if (ATTR_UNSET(value))
2875+
return SYMLINK_TYPE_UNSPECIFIED;
2876+
if (!strcmp(value, "file"))
2877+
return SYMLINK_TYPE_FILE;
2878+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2879+
return SYMLINK_TYPE_DIRECTORY;
2880+
2881+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2882+
return SYMLINK_TYPE_UNSPECIFIED;
2883+
}
2884+
2885+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
28062886
{
28072887
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
28082888
int len;
@@ -2822,48 +2902,31 @@ int symlink(const char *target, const char *link)
28222902
if (wtarget[len] == '/')
28232903
wtarget[len] = '\\';
28242904

2825-
/* create file symlink */
2826-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2827-
errno = err_win_to_posix(GetLastError());
2828-
return -1;
2829-
}
2830-
2831-
/* convert to directory symlink if target exists */
2832-
switch (process_phantom_symlink(wtarget, wlink)) {
2833-
case PHANTOM_SYMLINK_RETRY: {
2834-
/* if target doesn't exist, add to phantom symlinks list */
2835-
wchar_t wfullpath[MAX_LONG_PATH];
2836-
struct phantom_symlink_info *psi;
2837-
2838-
/* convert to absolute path to be independent of cwd */
2839-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2840-
if (!len || len >= MAX_LONG_PATH) {
2841-
errno = err_win_to_posix(GetLastError());
2842-
return -1;
2843-
}
2844-
2845-
/* over-allocate and fill phantom_symlink_info structure */
2846-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2847-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2848-
psi->wlink = (wchar_t *)(psi + 1);
2849-
wcscpy(psi->wlink, wfullpath);
2850-
psi->wtarget = psi->wlink + len + 1;
2851-
wcscpy(psi->wtarget, wtarget);
2852-
2853-
EnterCriticalSection(&phantom_symlinks_cs);
2854-
psi->next = phantom_symlinks;
2855-
phantom_symlinks = psi;
2856-
LeaveCriticalSection(&phantom_symlinks_cs);
2857-
break;
2858-
}
2859-
case PHANTOM_SYMLINK_DIRECTORY:
2860-
/* if we created a dir symlink, process other phantom symlinks */
2905+
switch (check_symlink_attr(index, link)) {
2906+
case SYMLINK_TYPE_UNSPECIFIED:
2907+
/* Create a phantom symlink: it is initially created as a file
2908+
* symlink, but may change to a directory symlink later if/when
2909+
* the target exists. */
2910+
return create_phantom_symlink(wtarget, wlink);
2911+
case SYMLINK_TYPE_FILE:
2912+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2913+
break;
2914+
return 0;
2915+
case SYMLINK_TYPE_DIRECTORY:
2916+
if (!CreateSymbolicLinkW(wlink, wtarget,
2917+
symlink_directory_flags))
2918+
break;
2919+
/* There may be dangling phantom symlinks that point at this
2920+
* one, which should now morph into directory symlinks. */
28612921
process_phantom_symlinks();
2862-
break;
2922+
return 0;
28632923
default:
2864-
break;
2924+
BUG("unhandled symlink type");
28652925
}
2866-
return 0;
2926+
2927+
/* CreateSymbolicLinkW failed. */
2928+
errno = err_win_to_posix(GetLastError());
2929+
return -1;
28672930
}
28682931

28692932
#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
@@ -464,6 +464,15 @@ static inline int git_has_dir_sep(const char *path)
464464
#define is_mount_point is_mount_point_via_stat
465465
#endif
466466

467+
#ifndef create_symlink
468+
struct index_state;
469+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
470+
{
471+
return symlink(target, link);
472+
}
473+
#define create_symlink git_create_symlink
474+
#endif
475+
467476
#ifndef query_user_email
468477
#define query_user_email() NULL
469478
#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
@@ -1862,7 +1862,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
18621862
#ifndef NO_SYMLINK_HEAD
18631863
char *ref_path = get_locked_file_path(&lock->lk);
18641864
unlink(ref_path);
1865-
ret = symlink(target, ref_path);
1865+
ret = create_symlink(NULL, target, ref_path);
18661866
free(ref_path);
18671867

18681868
if (ret)

0 commit comments

Comments
 (0)