Skip to content

Commit 2309610

Browse files
committed
Merge pull request git-for-windows#1897 from piscisaureus/symlink-attr
Specify symlink type in .gitattributes
2 parents 458d567 + cb0e177 commit 2309610

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"
@@ -424,6 +425,54 @@ static void process_phantom_symlinks(void)
424425
LeaveCriticalSection(&phantom_symlinks_cs);
425426
}
426427

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

2766-
int symlink(const char *target, const char *link)
2815+
enum symlink_type {
2816+
SYMLINK_TYPE_UNSPECIFIED = 0,
2817+
SYMLINK_TYPE_FILE,
2818+
SYMLINK_TYPE_DIRECTORY,
2819+
};
2820+
2821+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2822+
{
2823+
static struct attr_check *check;
2824+
const char *value;
2825+
2826+
if (!index)
2827+
return SYMLINK_TYPE_UNSPECIFIED;
2828+
2829+
if (!check)
2830+
check = attr_check_initl("symlink", NULL);
2831+
2832+
git_check_attr(index, link, check);
2833+
2834+
value = check->items[0].value;
2835+
if (ATTR_UNSET(value))
2836+
return SYMLINK_TYPE_UNSPECIFIED;
2837+
if (!strcmp(value, "file"))
2838+
return SYMLINK_TYPE_FILE;
2839+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2840+
return SYMLINK_TYPE_DIRECTORY;
2841+
2842+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2843+
return SYMLINK_TYPE_UNSPECIFIED;
2844+
}
2845+
2846+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
27672847
{
27682848
wchar_t wtarget[MAX_PATH], wlink[MAX_PATH];
27692849
int len;
@@ -2783,48 +2863,31 @@ int symlink(const char *target, const char *link)
27832863
if (wtarget[len] == '/')
27842864
wtarget[len] = '\\';
27852865

2786-
/* create file symlink */
2787-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2788-
errno = err_win_to_posix(GetLastError());
2789-
return -1;
2790-
}
2791-
2792-
/* convert to directory symlink if target exists */
2793-
switch (process_phantom_symlink(wtarget, wlink)) {
2794-
case PHANTOM_SYMLINK_RETRY: {
2795-
/* if target doesn't exist, add to phantom symlinks list */
2796-
wchar_t wfullpath[MAX_PATH];
2797-
struct phantom_symlink_info *psi;
2798-
2799-
/* convert to absolute path to be independent of cwd */
2800-
len = GetFullPathNameW(wlink, MAX_PATH, wfullpath, NULL);
2801-
if (!len || len >= MAX_PATH) {
2802-
errno = err_win_to_posix(GetLastError());
2803-
return -1;
2804-
}
2805-
2806-
/* over-allocate and fill phantom_symlink_info structure */
2807-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2808-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2809-
psi->wlink = (wchar_t *)(psi + 1);
2810-
wcscpy(psi->wlink, wfullpath);
2811-
psi->wtarget = psi->wlink + len + 1;
2812-
wcscpy(psi->wtarget, wtarget);
2813-
2814-
EnterCriticalSection(&phantom_symlinks_cs);
2815-
psi->next = phantom_symlinks;
2816-
phantom_symlinks = psi;
2817-
LeaveCriticalSection(&phantom_symlinks_cs);
2818-
break;
2819-
}
2820-
case PHANTOM_SYMLINK_DIRECTORY:
2821-
/* if we created a dir symlink, process other phantom symlinks */
2866+
switch (check_symlink_attr(index, link)) {
2867+
case SYMLINK_TYPE_UNSPECIFIED:
2868+
/* Create a phantom symlink: it is initially created as a file
2869+
* symlink, but may change to a directory symlink later if/when
2870+
* the target exists. */
2871+
return create_phantom_symlink(wtarget, wlink);
2872+
case SYMLINK_TYPE_FILE:
2873+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2874+
break;
2875+
return 0;
2876+
case SYMLINK_TYPE_DIRECTORY:
2877+
if (!CreateSymbolicLinkW(wlink, wtarget,
2878+
symlink_directory_flags))
2879+
break;
2880+
/* There may be dangling phantom symlinks that point at this
2881+
* one, which should now morph into directory symlinks. */
28222882
process_phantom_symlinks();
2823-
break;
2883+
return 0;
28242884
default:
2825-
break;
2885+
BUG("unhandled symlink type");
28262886
}
2827-
return 0;
2887+
2888+
/* CreateSymbolicLinkW failed. */
2889+
errno = err_win_to_posix(GetLastError());
2890+
return -1;
28282891
}
28292892

28302893
#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
@@ -383,6 +383,16 @@ static inline int git_has_dir_sep(const char *path)
383383
#define has_dir_sep(path) git_has_dir_sep(path)
384384
#endif
385385

386+
#ifndef create_symlink
387+
struct index_state;
388+
static inline int git_create_symlink(struct index_state *index UNUSED,
389+
const char *target, const char *link)
390+
{
391+
return symlink(target, link);
392+
}
393+
#define create_symlink git_create_symlink
394+
#endif
395+
386396
#ifndef query_user_email
387397
#define query_user_email() NULL
388398
#endif

refs/files-backend.c

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

21252125
ref_path = get_locked_file_path(&lock->lk);
21262126
unlink(ref_path);
2127-
ret = symlink(target, ref_path);
2127+
ret = create_symlink(NULL, target, ref_path);
21282128
free(ref_path);
21292129

21302130
if (ret)

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,7 +2133,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
21332133
if (strbuf_readlink(&lnk, template_path->buf,
21342134
st_template.st_size) < 0)
21352135
die_errno(_("cannot readlink '%s'"), template_path->buf);
2136-
if (symlink(lnk.buf, path->buf))
2136+
if (create_symlink(NULL, lnk.buf, path->buf))
21372137
die_errno(_("cannot symlink '%s' '%s'"),
21382138
lnk.buf, path->buf);
21392139
strbuf_release(&lnk);
@@ -2394,7 +2394,7 @@ static int create_default_files(const char *template_path,
23942394
repo_git_path_replace(the_repository, &path, "tXXXXXX");
23952395
if (!close(xmkstemp(path.buf)) &&
23962396
!unlink(path.buf) &&
2397-
!symlink("testing", path.buf) &&
2397+
!create_symlink(NULL, "testing", path.buf) &&
23982398
!lstat(path.buf, &st1) &&
23992399
S_ISLNK(st1.st_mode))
24002400
unlink(path.buf); /* good */

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ integration_tests = [
267267
't2026-checkout-pathspec-file.sh',
268268
't2027-checkout-track.sh',
269269
't2030-unresolve-info.sh',
270+
't2040-checkout-symlink-attr.sh',
270271
't2050-git-dir-relative.sh',
271272
't2060-switch.sh',
272273
't2070-restore.sh',

0 commit comments

Comments
 (0)