Skip to content

Commit 4600e54

Browse files
piscisaureusGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
mingw: allow to specify the symlink type in .gitattributes
On Windows, symbolic links have a type: a "file symlink" must point at a file, and a "directory symlink" must point at a directory. If the type of symlink does not match its target, it doesn't work. Git does not record the type of symlink in the index or in a tree. On checkout it'll guess the type, which only works if the target exists at the time the symlink is created. This may often not be the case, for example when the link points at a directory inside a submodule. By specifying `symlink=file` or `symlink=dir` the user can specify what type of symlink Git should create, so Git doesn't have to rely on unreliable heuristics. Signed-off-by: Bert Belder <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 15e60aa commit 4600e54

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
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

compat/mingw.c

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include "../wrapper.h"
2121
#include "../write-or-die.h"
2222
#include "../repository.h"
23-
#include "../repository.h"
2423
#include "win32/fscache.h"
24+
#include "../attr.h"
2525
#include "dir.h"
2626
#include "gettext.h"
2727
#define SECURITY_WIN32
@@ -3145,6 +3145,37 @@ int link(const char *oldpath, const char *newpath)
31453145
return 0;
31463146
}
31473147

3148+
enum symlink_type {
3149+
SYMLINK_TYPE_UNSPECIFIED = 0,
3150+
SYMLINK_TYPE_FILE,
3151+
SYMLINK_TYPE_DIRECTORY,
3152+
};
3153+
3154+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
3155+
{
3156+
static struct attr_check *check;
3157+
const char *value;
3158+
3159+
if (!index)
3160+
return SYMLINK_TYPE_UNSPECIFIED;
3161+
3162+
if (!check)
3163+
check = attr_check_initl("symlink", NULL);
3164+
3165+
git_check_attr(index, link, check);
3166+
3167+
value = check->items[0].value;
3168+
if (ATTR_UNSET(value))
3169+
return SYMLINK_TYPE_UNSPECIFIED;
3170+
if (!strcmp(value, "file"))
3171+
return SYMLINK_TYPE_FILE;
3172+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
3173+
return SYMLINK_TYPE_DIRECTORY;
3174+
3175+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
3176+
return SYMLINK_TYPE_UNSPECIFIED;
3177+
}
3178+
31483179
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
31493180
{
31503181
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
@@ -3165,7 +3196,31 @@ int mingw_create_symlink(struct index_state *index, const char *target, const ch
31653196
if (wtarget[len] == '/')
31663197
wtarget[len] = '\\';
31673198

3168-
return create_phantom_symlink(wtarget, wlink);
3199+
switch (check_symlink_attr(index, link)) {
3200+
case SYMLINK_TYPE_UNSPECIFIED:
3201+
/* Create a phantom symlink: it is initially created as a file
3202+
* symlink, but may change to a directory symlink later if/when
3203+
* the target exists. */
3204+
return create_phantom_symlink(wtarget, wlink);
3205+
case SYMLINK_TYPE_FILE:
3206+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
3207+
break;
3208+
return 0;
3209+
case SYMLINK_TYPE_DIRECTORY:
3210+
if (!CreateSymbolicLinkW(wlink, wtarget,
3211+
symlink_directory_flags))
3212+
break;
3213+
/* There may be dangling phantom symlinks that point at this
3214+
* one, which should now morph into directory symlinks. */
3215+
process_phantom_symlinks();
3216+
return 0;
3217+
default:
3218+
BUG("unhandled symlink type");
3219+
}
3220+
3221+
/* CreateSymbolicLinkW failed. */
3222+
errno = err_win_to_posix(GetLastError());
3223+
return -1;
31693224
}
31703225

31713226
#ifndef _WINNT_H

0 commit comments

Comments
 (0)