Skip to content

Commit 612492a

Browse files
piscisaureusGit for Windows Build Agent
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 44ee4cd commit 612492a

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
@@ -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

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

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

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

24302486
#ifndef _WINNT_H

0 commit comments

Comments
 (0)