Skip to content

Commit 10ca079

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 2a6b10e commit 10ca079

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
@@ -12,6 +12,7 @@
1212
#define SECURITY_WIN32
1313
#include <sspi.h>
1414
#include "win32/fscache.h"
15+
#include "../attr.h"
1516

1617
#define HCAST(type, handle) ((type)(intptr_t)handle)
1718

@@ -2842,6 +2843,37 @@ int link(const char *oldpath, const char *newpath)
28422843
return 0;
28432844
}
28442845

2846+
enum symlink_type {
2847+
SYMLINK_TYPE_UNSPECIFIED = 0,
2848+
SYMLINK_TYPE_FILE,
2849+
SYMLINK_TYPE_DIRECTORY,
2850+
};
2851+
2852+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2853+
{
2854+
static struct attr_check *check;
2855+
const char *value;
2856+
2857+
if (!index)
2858+
return SYMLINK_TYPE_UNSPECIFIED;
2859+
2860+
if (!check)
2861+
check = attr_check_initl("symlink", NULL);
2862+
2863+
git_check_attr(index, link, check);
2864+
2865+
value = check->items[0].value;
2866+
if (ATTR_UNSET(value))
2867+
return SYMLINK_TYPE_UNSPECIFIED;
2868+
if (!strcmp(value, "file"))
2869+
return SYMLINK_TYPE_FILE;
2870+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2871+
return SYMLINK_TYPE_DIRECTORY;
2872+
2873+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2874+
return SYMLINK_TYPE_UNSPECIFIED;
2875+
}
2876+
28452877
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
28462878
{
28472879
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
@@ -2862,7 +2894,31 @@ int mingw_create_symlink(struct index_state *index, const char *target, const ch
28622894
if (wtarget[len] == '/')
28632895
wtarget[len] = '\\';
28642896

2865-
return create_phantom_symlink(wtarget, wlink);
2897+
switch (check_symlink_attr(index, link)) {
2898+
case SYMLINK_TYPE_UNSPECIFIED:
2899+
/* Create a phantom symlink: it is initially created as a file
2900+
* symlink, but may change to a directory symlink later if/when
2901+
* the target exists. */
2902+
return create_phantom_symlink(wtarget, wlink);
2903+
case SYMLINK_TYPE_FILE:
2904+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2905+
break;
2906+
return 0;
2907+
case SYMLINK_TYPE_DIRECTORY:
2908+
if (!CreateSymbolicLinkW(wlink, wtarget,
2909+
symlink_directory_flags))
2910+
break;
2911+
/* There may be dangling phantom symlinks that point at this
2912+
* one, which should now morph into directory symlinks. */
2913+
process_phantom_symlinks();
2914+
return 0;
2915+
default:
2916+
BUG("unhandled symlink type");
2917+
}
2918+
2919+
/* CreateSymbolicLinkW failed. */
2920+
errno = err_win_to_posix(GetLastError());
2921+
return -1;
28662922
}
28672923

28682924
#ifndef _WINNT_H

0 commit comments

Comments
 (0)