Skip to content

Commit 7b24f74

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 8e17c47 commit 7b24f74

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
@@ -9,6 +9,7 @@
99
#include "win32/lazyload.h"
1010
#include "../config.h"
1111
#include "dir.h"
12+
#include "../attr.h"
1213

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

@@ -2403,6 +2404,37 @@ int link(const char *oldpath, const char *newpath)
24032404
return 0;
24042405
}
24052406

2407+
enum symlink_type {
2408+
SYMLINK_TYPE_UNSPECIFIED = 0,
2409+
SYMLINK_TYPE_FILE,
2410+
SYMLINK_TYPE_DIRECTORY,
2411+
};
2412+
2413+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2414+
{
2415+
static struct attr_check *check;
2416+
const char *value;
2417+
2418+
if (!index)
2419+
return SYMLINK_TYPE_UNSPECIFIED;
2420+
2421+
if (!check)
2422+
check = attr_check_initl("symlink", NULL);
2423+
2424+
git_check_attr(index, link, check);
2425+
2426+
value = check->items[0].value;
2427+
if (ATTR_UNSET(value))
2428+
return SYMLINK_TYPE_UNSPECIFIED;
2429+
if (!strcmp(value, "file"))
2430+
return SYMLINK_TYPE_FILE;
2431+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2432+
return SYMLINK_TYPE_DIRECTORY;
2433+
2434+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2435+
return SYMLINK_TYPE_UNSPECIFIED;
2436+
}
2437+
24062438
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
24072439
{
24082440
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
@@ -2423,7 +2455,31 @@ int mingw_create_symlink(struct index_state *index, const char *target, const ch
24232455
if (wtarget[len] == '/')
24242456
wtarget[len] = '\\';
24252457

2426-
return create_phantom_symlink(wtarget, wlink);
2458+
switch (check_symlink_attr(index, link)) {
2459+
case SYMLINK_TYPE_UNSPECIFIED:
2460+
/* Create a phantom symlink: it is initially created as a file
2461+
* symlink, but may change to a directory symlink later if/when
2462+
* the target exists. */
2463+
return create_phantom_symlink(wtarget, wlink);
2464+
case SYMLINK_TYPE_FILE:
2465+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2466+
break;
2467+
return 0;
2468+
case SYMLINK_TYPE_DIRECTORY:
2469+
if (!CreateSymbolicLinkW(wlink, wtarget,
2470+
symlink_directory_flags))
2471+
break;
2472+
/* There may be dangling phantom symlinks that point at this
2473+
* one, which should now morph into directory symlinks. */
2474+
process_phantom_symlinks();
2475+
return 0;
2476+
default:
2477+
BUG("unhandled symlink type");
2478+
}
2479+
2480+
/* CreateSymbolicLinkW failed. */
2481+
errno = err_win_to_posix(GetLastError());
2482+
return -1;
24272483
}
24282484

24292485
#ifndef _WINNT_H

0 commit comments

Comments
 (0)