Skip to content

Commit 02b5eb8

Browse files
piscisaureusdscho
authored andcommitted
Win32: symlink: specify 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 fdd6b16 commit 02b5eb8

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

Documentation/gitattributes.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,36 @@ sign `$` upon checkout. Any byte sequence that begins with
378378
with `$Id$` upon check-in.
379379

380380

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

compat/mingw.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../cache.h"
99
#include "win32/lazyload.h"
1010
#include "../config.h"
11+
#include "../attr.h"
1112

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

@@ -2526,6 +2527,33 @@ int link(const char *oldpath, const char *newpath)
25262527
return 0;
25272528
}
25282529

2530+
enum symlink_type {
2531+
SYMLINK_TYPE_UNSPECIFIED = 0,
2532+
SYMLINK_TYPE_FILE,
2533+
SYMLINK_TYPE_DIRECTORY,
2534+
};
2535+
2536+
static enum symlink_type check_symlink_attr(const char *link)
2537+
{
2538+
static struct attr_check *check;
2539+
const char *value;
2540+
2541+
if (!check)
2542+
check = attr_check_initl("symlink", NULL);
2543+
2544+
git_check_attr(&the_index, link, check);
2545+
2546+
value = check->items[0].value;
2547+
if (value == NULL)
2548+
;
2549+
else if (!strcmp(value, "file"))
2550+
return SYMLINK_TYPE_FILE;
2551+
else if (!strcmp(value, "dir"))
2552+
return SYMLINK_TYPE_DIRECTORY;
2553+
2554+
return SYMLINK_TYPE_UNSPECIFIED;
2555+
}
2556+
25292557
int symlink(const char *target, const char *link)
25302558
{
25312559
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
@@ -2546,7 +2574,31 @@ int symlink(const char *target, const char *link)
25462574
if (wtarget[len] == '/')
25472575
wtarget[len] = '\\';
25482576

2549-
return create_phantom_symlink(wtarget, wlink);
2577+
switch (check_symlink_attr(link)) {
2578+
case SYMLINK_TYPE_UNSPECIFIED:
2579+
/* Create a phantom symlink: it is initially created as a file
2580+
* symlink, but may change to a directory symlink later if/when
2581+
* the target exists. */
2582+
return create_phantom_symlink(wtarget, wlink);
2583+
case SYMLINK_TYPE_FILE:
2584+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags))
2585+
break;
2586+
return 0;
2587+
case SYMLINK_TYPE_DIRECTORY:
2588+
if (!CreateSymbolicLinkW(wlink, wtarget,
2589+
symlink_directory_flags))
2590+
break;
2591+
/* There may be dangling phantom symlinks that point at this
2592+
* one, which should now morph into directory symlinks. */
2593+
process_phantom_symlinks();
2594+
return 0;
2595+
default:
2596+
BUG("unhandled symlink type");
2597+
}
2598+
2599+
/* CreateSymbolicLinkW failed. */
2600+
errno = err_win_to_posix(GetLastError());
2601+
return -1;
25502602
}
25512603

25522604
#ifndef _WINNT_H

0 commit comments

Comments
 (0)