Skip to content

Commit cb86163

Browse files
piscisaureusmjcheetham
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 a5102ee commit cb86163

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

390390

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

compat/mingw.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "../write-or-die.h"
2424
#include "../repository.h"
2525
#include "win32/fscache.h"
26+
#include "../attr.h"
2627

2728
#define HCAST(type, handle) ((type)(intptr_t)handle)
2829

@@ -2920,6 +2921,37 @@ int link(const char *oldpath, const char *newpath)
29202921
return 0;
29212922
}
29222923

2924+
enum symlink_type {
2925+
SYMLINK_TYPE_UNSPECIFIED = 0,
2926+
SYMLINK_TYPE_FILE,
2927+
SYMLINK_TYPE_DIRECTORY,
2928+
};
2929+
2930+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2931+
{
2932+
static struct attr_check *check;
2933+
const char *value;
2934+
2935+
if (!index)
2936+
return SYMLINK_TYPE_UNSPECIFIED;
2937+
2938+
if (!check)
2939+
check = attr_check_initl("symlink", NULL);
2940+
2941+
git_check_attr(index, link, check);
2942+
2943+
value = check->items[0].value;
2944+
if (ATTR_UNSET(value))
2945+
return SYMLINK_TYPE_UNSPECIFIED;
2946+
if (!strcmp(value, "file"))
2947+
return SYMLINK_TYPE_FILE;
2948+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2949+
return SYMLINK_TYPE_DIRECTORY;
2950+
2951+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2952+
return SYMLINK_TYPE_UNSPECIFIED;
2953+
}
2954+
29232955
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
29242956
{
29252957
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
@@ -2940,7 +2972,31 @@ int mingw_create_symlink(struct index_state *index, const char *target, const ch
29402972
if (wtarget[len] == '/')
29412973
wtarget[len] = '\\';
29422974

2943-
return create_phantom_symlink(wtarget, wlink);
2975+
switch (check_symlink_attr(index, link)) {
2976+
case SYMLINK_TYPE_UNSPECIFIED:
2977+
/* Create a phantom symlink: it is initially created as a file
2978+
* symlink, but may change to a directory symlink later if/when
2979+
* the target exists. */
2980+
return create_phantom_symlink(wtarget, wlink);
2981+
case SYMLINK_TYPE_FILE:
2982+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2983+
break;
2984+
return 0;
2985+
case SYMLINK_TYPE_DIRECTORY:
2986+
if (!CreateSymbolicLinkW(wlink, wtarget,
2987+
symlink_directory_flags))
2988+
break;
2989+
/* There may be dangling phantom symlinks that point at this
2990+
* one, which should now morph into directory symlinks. */
2991+
process_phantom_symlinks();
2992+
return 0;
2993+
default:
2994+
BUG("unhandled symlink type");
2995+
}
2996+
2997+
/* CreateSymbolicLinkW failed. */
2998+
errno = err_win_to_posix(GetLastError());
2999+
return -1;
29443000
}
29453001

29463002
#ifndef _WINNT_H

0 commit comments

Comments
 (0)