Skip to content

Commit 97f8829

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 49dfba0 commit 97f8829

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

@@ -2397,6 +2398,37 @@ int link(const char *oldpath, const char *newpath)
23972398
return 0;
23982399
}
23992400

2401+
enum symlink_type {
2402+
SYMLINK_TYPE_UNSPECIFIED = 0,
2403+
SYMLINK_TYPE_FILE,
2404+
SYMLINK_TYPE_DIRECTORY,
2405+
};
2406+
2407+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2408+
{
2409+
static struct attr_check *check;
2410+
const char *value;
2411+
2412+
if (!index)
2413+
return SYMLINK_TYPE_UNSPECIFIED;
2414+
2415+
if (!check)
2416+
check = attr_check_initl("symlink", NULL);
2417+
2418+
git_check_attr(index, link, check);
2419+
2420+
value = check->items[0].value;
2421+
if (ATTR_UNSET(value))
2422+
return SYMLINK_TYPE_UNSPECIFIED;
2423+
if (!strcmp(value, "file"))
2424+
return SYMLINK_TYPE_FILE;
2425+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2426+
return SYMLINK_TYPE_DIRECTORY;
2427+
2428+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2429+
return SYMLINK_TYPE_UNSPECIFIED;
2430+
}
2431+
24002432
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
24012433
{
24022434
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
@@ -2417,7 +2449,31 @@ int mingw_create_symlink(struct index_state *index, const char *target, const ch
24172449
if (wtarget[len] == '/')
24182450
wtarget[len] = '\\';
24192451

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

24232479
#ifndef _WINNT_H

0 commit comments

Comments
 (0)