Skip to content

Commit c1d47f9

Browse files
piscisaureusdscho
authored andcommitted
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 714ab70 commit c1d47f9

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

Documentation/gitattributes.txt

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "../write-or-die.h"
2626
#include "../repository.h"
2727
#include "win32/fscache.h"
28+
#include "../attr.h"
2829

2930
#define HCAST(type, handle) ((type)(intptr_t)handle)
3031

@@ -3092,6 +3093,37 @@ int link(const char *oldpath, const char *newpath)
30923093
return 0;
30933094
}
30943095

3096+
enum symlink_type {
3097+
SYMLINK_TYPE_UNSPECIFIED = 0,
3098+
SYMLINK_TYPE_FILE,
3099+
SYMLINK_TYPE_DIRECTORY,
3100+
};
3101+
3102+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
3103+
{
3104+
static struct attr_check *check;
3105+
const char *value;
3106+
3107+
if (!index)
3108+
return SYMLINK_TYPE_UNSPECIFIED;
3109+
3110+
if (!check)
3111+
check = attr_check_initl("symlink", NULL);
3112+
3113+
git_check_attr(index, link, check);
3114+
3115+
value = check->items[0].value;
3116+
if (ATTR_UNSET(value))
3117+
return SYMLINK_TYPE_UNSPECIFIED;
3118+
if (!strcmp(value, "file"))
3119+
return SYMLINK_TYPE_FILE;
3120+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
3121+
return SYMLINK_TYPE_DIRECTORY;
3122+
3123+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
3124+
return SYMLINK_TYPE_UNSPECIFIED;
3125+
}
3126+
30953127
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
30963128
{
30973129
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
@@ -3112,7 +3144,31 @@ int mingw_create_symlink(struct index_state *index, const char *target, const ch
31123144
if (wtarget[len] == '/')
31133145
wtarget[len] = '\\';
31143146

3115-
return create_phantom_symlink(wtarget, wlink);
3147+
switch (check_symlink_attr(index, link)) {
3148+
case SYMLINK_TYPE_UNSPECIFIED:
3149+
/* Create a phantom symlink: it is initially created as a file
3150+
* symlink, but may change to a directory symlink later if/when
3151+
* the target exists. */
3152+
return create_phantom_symlink(wtarget, wlink);
3153+
case SYMLINK_TYPE_FILE:
3154+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
3155+
break;
3156+
return 0;
3157+
case SYMLINK_TYPE_DIRECTORY:
3158+
if (!CreateSymbolicLinkW(wlink, wtarget,
3159+
symlink_directory_flags))
3160+
break;
3161+
/* There may be dangling phantom symlinks that point at this
3162+
* one, which should now morph into directory symlinks. */
3163+
process_phantom_symlinks();
3164+
return 0;
3165+
default:
3166+
BUG("unhandled symlink type");
3167+
}
3168+
3169+
/* CreateSymbolicLinkW failed. */
3170+
errno = err_win_to_posix(GetLastError());
3171+
return -1;
31163172
}
31173173

31183174
#ifndef _WINNT_H

0 commit comments

Comments
 (0)