Skip to content

Commit e851f6d

Browse files
committed
Merge pull request #2091 from dscho/symlink-attr-extra
Touch up symlink .gitattributes support
2 parents f19e6b6 + 63cd5f1 commit e851f6d

File tree

10 files changed

+107
-10
lines changed

10 files changed

+107
-10
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

apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4356,7 +4356,7 @@ static int try_create_file(struct apply_state *state, const char *path,
43564356
/* Although buf:size is counted string, it also is NUL
43574357
* terminated.
43584358
*/
4359-
return !!symlink(buf, path);
4359+
return !!create_symlink(state && state->repo ? state->repo->index : NULL, buf, path);
43604360

43614361
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
43624362
if (fd < 0)

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
505505
}
506506
add_path(&wtdir, wtdir_len, dst_path);
507507
if (symlinks) {
508-
if (symlink(wtdir.buf, rdir.buf)) {
508+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
509509
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
510510
goto finish;
511511
}

builtin/init-db.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
7676
if (strbuf_readlink(&lnk, template_path->buf,
7777
st_template.st_size) < 0)
7878
die_errno(_("cannot readlink '%s'"), template_path->buf);
79-
if (symlink(lnk.buf, path->buf))
79+
if (create_symlink(NULL, lnk.buf, path->buf))
8080
die_errno(_("cannot symlink '%s' '%s'"),
8181
lnk.buf, path->buf);
8282
strbuf_release(&lnk);
@@ -278,7 +278,7 @@ static int create_default_files(const char *template_path,
278278
path = git_path_buf(&buf, "tXXXXXX");
279279
if (!close(xmkstemp(path)) &&
280280
!unlink(path) &&
281-
!symlink("testing", path) &&
281+
!create_symlink(NULL, "testing", path) &&
282282
!lstat(path, &st1) &&
283283
S_ISLNK(st1.st_mode))
284284
unlink(path); /* good */

compat/mingw.c

Lines changed: 58 additions & 2 deletions
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

@@ -2395,7 +2396,38 @@ int link(const char *oldpath, const char *newpath)
23952396
return 0;
23962397
}
23972398

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

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

24212477
#ifndef _WINNT_H

compat/mingw.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,10 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
214214
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
215215
int link(const char *oldpath, const char *newpath);
216216
int uname(struct utsname *buf);
217-
int symlink(const char *target, const char *link);
218217
int readlink(const char *path, char *buf, size_t bufsiz);
218+
struct index_state;
219+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link);
220+
#define create_symlink mingw_create_symlink
219221

220222
/*
221223
* replacements of existing functions

entry.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ static int write_entry(struct cache_entry *ce,
289289
if (!has_symlinks || to_tempfile)
290290
goto write_file_entry;
291291

292-
ret = symlink(new_blob, path);
292+
ret = create_symlink(state->istate, new_blob, path);
293293
free(new_blob);
294294
if (ret)
295295
return error_errno("unable to create symlink %s", path);

git-compat-util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,15 @@ static inline char *git_find_last_dir_sep(const char *path)
406406
#define find_last_dir_sep git_find_last_dir_sep
407407
#endif
408408

409+
#ifndef create_symlink
410+
struct index_state;
411+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
412+
{
413+
return symlink(target, link);
414+
}
415+
#define create_symlink git_create_symlink
416+
#endif
417+
409418
#ifndef query_user_email
410419
#define query_user_email() NULL
411420
#endif

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ static int update_file_flags(struct merge_options *o,
10111011
char *lnk = xmemdupz(buf, size);
10121012
safe_create_leading_directories_const(path);
10131013
unlink(path);
1014-
if (symlink(lnk, path))
1014+
if (create_symlink(&o->orig_index, lnk, path))
10151015
ret = err(o, _("failed to symlink '%s': %s"),
10161016
path, strerror(errno));
10171017
free(lnk);

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1786,7 +1786,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
17861786
#ifndef NO_SYMLINK_HEAD
17871787
char *ref_path = get_locked_file_path(&lock->lk);
17881788
unlink(ref_path);
1789-
ret = symlink(target, ref_path);
1789+
ret = create_symlink(NULL, target, ref_path);
17901790
free(ref_path);
17911791

17921792
if (ret)

0 commit comments

Comments
 (0)