Skip to content

Commit d452649

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 2655a7e commit d452649

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

385385

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

compat/mingw.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "../config.h"
1111
#include "dir.h"
1212
#include "win32/fscache.h"
13+
#include "../attr.h"
1314

1415
#define HCAST(type, handle) ((type)(intptr_t)handle)
1516

@@ -2809,6 +2810,37 @@ int link(const char *oldpath, const char *newpath)
28092810
return 0;
28102811
}
28112812

2813+
enum symlink_type {
2814+
SYMLINK_TYPE_UNSPECIFIED = 0,
2815+
SYMLINK_TYPE_FILE,
2816+
SYMLINK_TYPE_DIRECTORY,
2817+
};
2818+
2819+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2820+
{
2821+
static struct attr_check *check;
2822+
const char *value;
2823+
2824+
if (!index)
2825+
return SYMLINK_TYPE_UNSPECIFIED;
2826+
2827+
if (!check)
2828+
check = attr_check_initl("symlink", NULL);
2829+
2830+
git_check_attr(index, link, check);
2831+
2832+
value = check->items[0].value;
2833+
if (ATTR_UNSET(value))
2834+
return SYMLINK_TYPE_UNSPECIFIED;
2835+
if (!strcmp(value, "file"))
2836+
return SYMLINK_TYPE_FILE;
2837+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2838+
return SYMLINK_TYPE_DIRECTORY;
2839+
2840+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2841+
return SYMLINK_TYPE_UNSPECIFIED;
2842+
}
2843+
28122844
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
28132845
{
28142846
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
@@ -2829,7 +2861,31 @@ int mingw_create_symlink(struct index_state *index, const char *target, const ch
28292861
if (wtarget[len] == '/')
28302862
wtarget[len] = '\\';
28312863

2832-
return create_phantom_symlink(wtarget, wlink);
2864+
switch (check_symlink_attr(index, link)) {
2865+
case SYMLINK_TYPE_UNSPECIFIED:
2866+
/* Create a phantom symlink: it is initially created as a file
2867+
* symlink, but may change to a directory symlink later if/when
2868+
* the target exists. */
2869+
return create_phantom_symlink(wtarget, wlink);
2870+
case SYMLINK_TYPE_FILE:
2871+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2872+
break;
2873+
return 0;
2874+
case SYMLINK_TYPE_DIRECTORY:
2875+
if (!CreateSymbolicLinkW(wlink, wtarget,
2876+
symlink_directory_flags))
2877+
break;
2878+
/* There may be dangling phantom symlinks that point at this
2879+
* one, which should now morph into directory symlinks. */
2880+
process_phantom_symlinks();
2881+
return 0;
2882+
default:
2883+
BUG("unhandled symlink type");
2884+
}
2885+
2886+
/* CreateSymbolicLinkW failed. */
2887+
errno = err_win_to_posix(GetLastError());
2888+
return -1;
28332889
}
28342890

28352891
#ifndef _WINNT_H

0 commit comments

Comments
 (0)