Skip to content

Commit 0fc73a0

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 d67da81 commit 0fc73a0

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

387387

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

compat/mingw.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SECURITY_WIN32
1515
#include <sspi.h>
1616
#include "win32/fscache.h"
17+
#include "../attr.h"
1718

1819
#define HCAST(type, handle) ((type)(intptr_t)handle)
1920

@@ -2911,6 +2912,37 @@ int link(const char *oldpath, const char *newpath)
29112912
return 0;
29122913
}
29132914

2915+
enum symlink_type {
2916+
SYMLINK_TYPE_UNSPECIFIED = 0,
2917+
SYMLINK_TYPE_FILE,
2918+
SYMLINK_TYPE_DIRECTORY,
2919+
};
2920+
2921+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2922+
{
2923+
static struct attr_check *check;
2924+
const char *value;
2925+
2926+
if (!index)
2927+
return SYMLINK_TYPE_UNSPECIFIED;
2928+
2929+
if (!check)
2930+
check = attr_check_initl("symlink", NULL);
2931+
2932+
git_check_attr(index, NULL, link, check);
2933+
2934+
value = check->items[0].value;
2935+
if (ATTR_UNSET(value))
2936+
return SYMLINK_TYPE_UNSPECIFIED;
2937+
if (!strcmp(value, "file"))
2938+
return SYMLINK_TYPE_FILE;
2939+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2940+
return SYMLINK_TYPE_DIRECTORY;
2941+
2942+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2943+
return SYMLINK_TYPE_UNSPECIFIED;
2944+
}
2945+
29142946
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
29152947
{
29162948
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
@@ -2931,7 +2963,31 @@ int mingw_create_symlink(struct index_state *index, const char *target, const ch
29312963
if (wtarget[len] == '/')
29322964
wtarget[len] = '\\';
29332965

2934-
return create_phantom_symlink(wtarget, wlink);
2966+
switch (check_symlink_attr(index, link)) {
2967+
case SYMLINK_TYPE_UNSPECIFIED:
2968+
/* Create a phantom symlink: it is initially created as a file
2969+
* symlink, but may change to a directory symlink later if/when
2970+
* the target exists. */
2971+
return create_phantom_symlink(wtarget, wlink);
2972+
case SYMLINK_TYPE_FILE:
2973+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2974+
break;
2975+
return 0;
2976+
case SYMLINK_TYPE_DIRECTORY:
2977+
if (!CreateSymbolicLinkW(wlink, wtarget,
2978+
symlink_directory_flags))
2979+
break;
2980+
/* There may be dangling phantom symlinks that point at this
2981+
* one, which should now morph into directory symlinks. */
2982+
process_phantom_symlinks();
2983+
return 0;
2984+
default:
2985+
BUG("unhandled symlink type");
2986+
}
2987+
2988+
/* CreateSymbolicLinkW failed. */
2989+
errno = err_win_to_posix(GetLastError());
2990+
return -1;
29352991
}
29362992

29372993
#ifndef _WINNT_H

0 commit comments

Comments
 (0)