Skip to content

Commit 48374c6

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Merge pull request #1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 9b3fdb0 + 996613a commit 48374c6

File tree

11 files changed

+199
-47
lines changed

11 files changed

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

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

builtin/difftool.c

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

compat/mingw-posix.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,10 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
193193
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
194194
int link(const char *oldpath, const char *newpath);
195195
int uname(struct utsname *buf);
196-
int symlink(const char *target, const char *link);
197196
int readlink(const char *path, char *buf, size_t bufsiz);
197+
struct index_state;
198+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link);
199+
#define create_symlink mingw_create_symlink
198200

199201
/*
200202
* replacements of existing functions

compat/mingw.c

Lines changed: 103 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "git-compat-util.h"
55
#include "abspath.h"
66
#include "alloc.h"
7+
#include "attr.h"
78
#include "config.h"
89
#include "dir.h"
910
#include "environment.h"
@@ -454,6 +455,54 @@ static void process_phantom_symlinks(void)
454455
LeaveCriticalSection(&phantom_symlinks_cs);
455456
}
456457

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

3131-
int symlink(const char *target, const char *link)
3180+
enum symlink_type {
3181+
SYMLINK_TYPE_UNSPECIFIED = 0,
3182+
SYMLINK_TYPE_FILE,
3183+
SYMLINK_TYPE_DIRECTORY,
3184+
};
3185+
3186+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
3187+
{
3188+
static struct attr_check *check;
3189+
const char *value;
3190+
3191+
if (!index)
3192+
return SYMLINK_TYPE_UNSPECIFIED;
3193+
3194+
if (!check)
3195+
check = attr_check_initl("symlink", NULL);
3196+
3197+
git_check_attr(index, link, check);
3198+
3199+
value = check->items[0].value;
3200+
if (ATTR_UNSET(value))
3201+
return SYMLINK_TYPE_UNSPECIFIED;
3202+
if (!strcmp(value, "file"))
3203+
return SYMLINK_TYPE_FILE;
3204+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
3205+
return SYMLINK_TYPE_DIRECTORY;
3206+
3207+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
3208+
return SYMLINK_TYPE_UNSPECIFIED;
3209+
}
3210+
3211+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
31323212
{
31333213
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
31343214
int len;
@@ -3148,48 +3228,31 @@ int symlink(const char *target, const char *link)
31483228
if (wtarget[len] == '/')
31493229
wtarget[len] = '\\';
31503230

3151-
/* create file symlink */
3152-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
3153-
errno = err_win_to_posix(GetLastError());
3154-
return -1;
3155-
}
3156-
3157-
/* convert to directory symlink if target exists */
3158-
switch (process_phantom_symlink(wtarget, wlink)) {
3159-
case PHANTOM_SYMLINK_RETRY: {
3160-
/* if target doesn't exist, add to phantom symlinks list */
3161-
wchar_t wfullpath[MAX_LONG_PATH];
3162-
struct phantom_symlink_info *psi;
3163-
3164-
/* convert to absolute path to be independent of cwd */
3165-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
3166-
if (!len || len >= MAX_LONG_PATH) {
3167-
errno = err_win_to_posix(GetLastError());
3168-
return -1;
3169-
}
3170-
3171-
/* over-allocate and fill phantom_symlink_info structure */
3172-
psi = xmalloc(sizeof(struct phantom_symlink_info)
3173-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
3174-
psi->wlink = (wchar_t *)(psi + 1);
3175-
wcscpy(psi->wlink, wfullpath);
3176-
psi->wtarget = psi->wlink + len + 1;
3177-
wcscpy(psi->wtarget, wtarget);
3178-
3179-
EnterCriticalSection(&phantom_symlinks_cs);
3180-
psi->next = phantom_symlinks;
3181-
phantom_symlinks = psi;
3182-
LeaveCriticalSection(&phantom_symlinks_cs);
3183-
break;
3184-
}
3185-
case PHANTOM_SYMLINK_DIRECTORY:
3186-
/* if we created a dir symlink, process other phantom symlinks */
3231+
switch (check_symlink_attr(index, link)) {
3232+
case SYMLINK_TYPE_UNSPECIFIED:
3233+
/* Create a phantom symlink: it is initially created as a file
3234+
* symlink, but may change to a directory symlink later if/when
3235+
* the target exists. */
3236+
return create_phantom_symlink(wtarget, wlink);
3237+
case SYMLINK_TYPE_FILE:
3238+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
3239+
break;
3240+
return 0;
3241+
case SYMLINK_TYPE_DIRECTORY:
3242+
if (!CreateSymbolicLinkW(wlink, wtarget,
3243+
symlink_directory_flags))
3244+
break;
3245+
/* There may be dangling phantom symlinks that point at this
3246+
* one, which should now morph into directory symlinks. */
31873247
process_phantom_symlinks();
3188-
break;
3248+
return 0;
31893249
default:
3190-
break;
3250+
BUG("unhandled symlink type");
31913251
}
3192-
return 0;
3252+
3253+
/* CreateSymbolicLinkW failed. */
3254+
errno = err_win_to_posix(GetLastError());
3255+
return -1;
31933256
}
31943257

31953258
#ifndef _WINNT_H

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
@@ -396,6 +396,16 @@ static inline int git_has_dir_sep(const char *path)
396396
#define is_mount_point is_mount_point_via_stat
397397
#endif
398398

399+
#ifndef create_symlink
400+
struct index_state;
401+
static inline int git_create_symlink(struct index_state *index UNUSED,
402+
const char *target, const char *link)
403+
{
404+
return symlink(target, link);
405+
}
406+
#define create_symlink git_create_symlink
407+
#endif
408+
399409
#ifndef query_user_email
400410
#define query_user_email() NULL
401411
#endif

refs/files-backend.c

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

20602060
char *ref_path = get_locked_file_path(&lock->lk);
20612061
unlink(ref_path);
2062-
ret = symlink(target, ref_path);
2062+
ret = create_symlink(NULL, target, ref_path);
20632063
free(ref_path);
20642064

20652065
if (ret)

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
21422142
if (strbuf_readlink(&lnk, template_path->buf,
21432143
st_template.st_size) < 0)
21442144
die_errno(_("cannot readlink '%s'"), template_path->buf);
2145-
if (symlink(lnk.buf, path->buf))
2145+
if (create_symlink(NULL, lnk.buf, path->buf))
21462146
die_errno(_("cannot symlink '%s' '%s'"),
21472147
lnk.buf, path->buf);
21482148
strbuf_release(&lnk);
@@ -2403,7 +2403,7 @@ static int create_default_files(const char *template_path,
24032403
repo_git_path_replace(the_repository, &path, "tXXXXXX");
24042404
if (!close(xmkstemp(path.buf)) &&
24052405
!unlink(path.buf) &&
2406-
!symlink("testing", path.buf) &&
2406+
!create_symlink(NULL, "testing", path.buf) &&
24072407
!lstat(path.buf, &st1) &&
24082408
S_ISLNK(st1.st_mode))
24092409
unlink(path.buf); /* good */

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ integration_tests = [
263263
't2027-checkout-track.sh',
264264
't2030-unresolve-info.sh',
265265
't2031-checkout-long-paths.sh',
266+
't2040-checkout-symlink-attr.sh',
266267
't2050-git-dir-relative.sh',
267268
't2060-switch.sh',
268269
't2070-restore.sh',

0 commit comments

Comments
 (0)