Skip to content

Commit b5131be

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
Merge pull request #2091 from dscho/symlink-attr-extra
Touch up symlink .gitattributes support
2 parents 69ff638 + bd66c56 commit b5131be

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

apply.c

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

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

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
497497
}
498498
add_path(&wtdir, wtdir_len, dst_path);
499499
if (symlinks) {
500-
if (symlink(wtdir.buf, rdir.buf)) {
500+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
501501
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
502502
goto finish;
503503
}

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);
@@ -280,7 +280,7 @@ static int create_default_files(const char *template_path,
280280
path = git_path_buf(&buf, "tXXXXXX");
281281
if (!close(xmkstemp(path)) &&
282282
!unlink(path) &&
283-
!symlink("testing", path) &&
283+
!create_symlink(NULL, "testing", path) &&
284284
!lstat(path, &st1) &&
285285
S_ISLNK(st1.st_mode))
286286
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

@@ -2410,7 +2411,38 @@ int link(const char *oldpath, const char *newpath)
24102411
return 0;
24112412
}
24122413

2413-
int symlink(const char *target, const char *link)
2414+
enum symlink_type {
2415+
SYMLINK_TYPE_UNSPECIFIED = 0,
2416+
SYMLINK_TYPE_FILE,
2417+
SYMLINK_TYPE_DIRECTORY,
2418+
};
2419+
2420+
static enum symlink_type check_symlink_attr(struct index_state *index, const char *link)
2421+
{
2422+
static struct attr_check *check;
2423+
const char *value;
2424+
2425+
if (!index)
2426+
return SYMLINK_TYPE_UNSPECIFIED;
2427+
2428+
if (!check)
2429+
check = attr_check_initl("symlink", NULL);
2430+
2431+
git_check_attr(index, link, check);
2432+
2433+
value = check->items[0].value;
2434+
if (ATTR_UNSET(value))
2435+
return SYMLINK_TYPE_UNSPECIFIED;
2436+
if (!strcmp(value, "file"))
2437+
return SYMLINK_TYPE_FILE;
2438+
if (!strcmp(value, "dir") || !strcmp(value, "directory"))
2439+
return SYMLINK_TYPE_DIRECTORY;
2440+
2441+
warning(_("ignoring invalid symlink type '%s' for '%s'"), value, link);
2442+
return SYMLINK_TYPE_UNSPECIFIED;
2443+
}
2444+
2445+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
24142446
{
24152447
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
24162448
int len;
@@ -2430,7 +2462,31 @@ int symlink(const char *target, const char *link)
24302462
if (wtarget[len] == '/')
24312463
wtarget[len] = '\\';
24322464

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

24362492
#ifndef _WINNT_H

compat/mingw.h

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

219221
/*
220222
* 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
@@ -970,7 +970,7 @@ static int update_file_flags(struct merge_options *opt,
970970
char *lnk = xmemdupz(buf, size);
971971
safe_create_leading_directories_const(path);
972972
unlink(path);
973-
if (symlink(lnk, path))
973+
if (create_symlink(&opt->orig_index, lnk, path))
974974
ret = err(opt, _("failed to symlink '%s': %s"),
975975
path, strerror(errno));
976976
free(lnk);

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
17921792
#ifndef NO_SYMLINK_HEAD
17931793
char *ref_path = get_locked_file_path(&lock->lk);
17941794
unlink(ref_path);
1795-
ret = symlink(target, ref_path);
1795+
ret = create_symlink(NULL, target, ref_path);
17961796
free(ref_path);
17971797

17981798
if (ret)

0 commit comments

Comments
 (0)