Skip to content

Commit 54085a4

Browse files
committed
Introduce helper to create symlinks that knows about index_state
On Windows, symbolic links actually have a type depending on the target: it can be a file or a directory. In certain circumstances, this poses problems, e.g. when a symbolic link is supposed to point into a submodule that is not checked out, so there is no way for Git to auto-detect the type. To help with that, we will add support over the course of the next commits to specify that symlink type via the Git attributes. This requires an index_state, though, something that Git for Windows' `symlink()` replacement cannot know about because the function signature is defined by the POSIX standard and not ours to change. So let's introduce a helper function to create symbolic links that *does* know about the index_state. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 158ce8b commit 54085a4

File tree

9 files changed

+20
-9
lines changed

9 files changed

+20
-9
lines changed

apply.c

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

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

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
523523
}
524524
add_path(&wtdir, wtdir_len, dst_path);
525525
if (symlinks) {
526-
if (symlink(wtdir.buf, rdir.buf)) {
526+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
527527
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
528528
goto finish;
529529
}

compat/mingw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2939,7 +2939,7 @@ int link(const char *oldpath, const char *newpath)
29392939
return 0;
29402940
}
29412941

2942-
int symlink(const char *target, const char *link)
2942+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
29432943
{
29442944
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
29452945
int len;

compat/mingw.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,10 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
218218
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
219219
int link(const char *oldpath, const char *newpath);
220220
int uname(struct utsname *buf);
221-
int symlink(const char *target, const char *link);
222221
int readlink(const char *path, char *buf, size_t bufsiz);
222+
struct index_state;
223+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link);
224+
#define create_symlink mingw_create_symlink
223225

224226
/*
225227
* replacements of existing functions

entry.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
312312
if (!has_symlinks || to_tempfile)
313313
goto write_file_entry;
314314

315-
ret = symlink(new_blob, path);
315+
ret = create_symlink(state->istate, new_blob, path);
316316
free(new_blob);
317317
if (ret)
318318
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
@@ -600,6 +600,15 @@ static inline int git_has_dir_sep(const char *path)
600600
#define is_mount_point is_mount_point_via_stat
601601
#endif
602602

603+
#ifndef create_symlink
604+
struct index_state;
605+
static inline int git_create_symlink(struct index_state *index, const char *target, const char *link)
606+
{
607+
return symlink(target, link);
608+
}
609+
#define create_symlink git_create_symlink
610+
#endif
611+
603612
#ifndef query_user_email
604613
#define query_user_email() NULL
605614
#endif

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ static int update_file_flags(struct merge_options *opt,
998998
char *lnk = xmemdupz(buf, size);
999999
safe_create_leading_directories_const(path);
10001000
unlink(path);
1001-
if (symlink(lnk, path))
1001+
if (create_symlink(&opt->priv->orig_index, lnk, path))
10021002
ret = err(opt, _("failed to symlink '%s': %s"),
10031003
path, strerror(errno));
10041004
free(lnk);

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
18361836
#ifndef NO_SYMLINK_HEAD
18371837
char *ref_path = get_locked_file_path(&lock->lk);
18381838
unlink(ref_path);
1839-
ret = symlink(target, ref_path);
1839+
ret = create_symlink(NULL, target, ref_path);
18401840
free(ref_path);
18411841

18421842
if (ret)

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
18021802
if (strbuf_readlink(&lnk, template_path->buf,
18031803
st_template.st_size) < 0)
18041804
die_errno(_("cannot readlink '%s'"), template_path->buf);
1805-
if (symlink(lnk.buf, path->buf))
1805+
if (create_symlink(NULL, lnk.buf, path->buf))
18061806
die_errno(_("cannot symlink '%s' '%s'"),
18071807
lnk.buf, path->buf);
18081808
strbuf_release(&lnk);
@@ -2074,7 +2074,7 @@ static int create_default_files(const char *template_path,
20742074
path = git_path_buf(&buf, "tXXXXXX");
20752075
if (!close(xmkstemp(path)) &&
20762076
!unlink(path) &&
2077-
!symlink("testing", path) &&
2077+
!create_symlink(NULL, "testing", path) &&
20782078
!lstat(path, &st1) &&
20792079
S_ISLNK(st1.st_mode))
20802080
unlink(path); /* good */

0 commit comments

Comments
 (0)