Skip to content

Commit 63cd5f1

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 b158c5a commit 63cd5f1

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

384384

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

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

@@ -2388,6 +2389,37 @@ int link(const char *oldpath, const char *newpath)
23882389
return 0;
23892390
}
23902391

2392+
enum symlink_type {
2393+
SYMLINK_TYPE_UNSPECIFIED = 0,
2394+
SYMLINK_TYPE_FILE,
2395+
SYMLINK_TYPE_DIRECTORY,
2396+
};
2397+
2398+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2399+
{
2400+
static struct attr_check *check;
2401+
const char *value;
2402+
2403+
if (!index)
2404+
return SYMLINK_TYPE_UNSPECIFIED;
2405+
2406+
if (!check)
2407+
check = attr_check_initl("symlink", NULL);
2408+
2409+
git_check_attr(index, link, check);
2410+
2411+
value = check->items[0].value;
2412+
if (ATTR_UNSET(value))
2413+
return SYMLINK_TYPE_UNSPECIFIED;
2414+
if (!strcmp(value, "file"))
2415+
return SYMLINK_TYPE_FILE;
2416+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2417+
return SYMLINK_TYPE_DIRECTORY;
2418+
2419+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2420+
return SYMLINK_TYPE_UNSPECIFIED;
2421+
}
2422+
23912423
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
23922424
{
23932425
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
@@ -2408,7 +2440,31 @@ int mingw_create_symlink(struct index_state *index, const char *target, const ch
24082440
if (wtarget[len] == '/')
24092441
wtarget[len] = '\\';
24102442

2411-
return create_phantom_symlink(wtarget, wlink);
2443+
switch (check_symlink_attr(index, link)) {
2444+
case SYMLINK_TYPE_UNSPECIFIED:
2445+
/* Create a phantom symlink: it is initially created as a file
2446+
* symlink, but may change to a directory symlink later if/when
2447+
* the target exists. */
2448+
return create_phantom_symlink(wtarget, wlink);
2449+
case SYMLINK_TYPE_FILE:
2450+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2451+
break;
2452+
return 0;
2453+
case SYMLINK_TYPE_DIRECTORY:
2454+
if (!CreateSymbolicLinkW(wlink, wtarget,
2455+
symlink_directory_flags))
2456+
break;
2457+
/* There may be dangling phantom symlinks that point at this
2458+
* one, which should now morph into directory symlinks. */
2459+
process_phantom_symlinks();
2460+
return 0;
2461+
default:
2462+
BUG("unhandled symlink type");
2463+
}
2464+
2465+
/* CreateSymbolicLinkW failed. */
2466+
errno = err_win_to_posix(GetLastError());
2467+
return -1;
24122468
}
24132469

24142470
#ifndef _WINNT_H

0 commit comments

Comments
 (0)