Skip to content

Commit 2655a7e

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 25b9367 commit 2655a7e

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
@@ -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
@@ -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
@@ -79,7 +79,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
7979
if (strbuf_readlink(&lnk, template_path->buf,
8080
st_template.st_size) < 0)
8181
die_errno(_("cannot readlink '%s'"), template_path->buf);
82-
if (symlink(lnk.buf, path->buf))
82+
if (create_symlink(NULL, lnk.buf, path->buf))
8383
die_errno(_("cannot symlink '%s' '%s'"),
8484
lnk.buf, path->buf);
8585
strbuf_release(&lnk);
@@ -312,7 +312,7 @@ static int create_default_files(const char *template_path,
312312
path = git_path_buf(&buf, "tXXXXXX");
313313
if (!close(xmkstemp(path)) &&
314314
!unlink(path) &&
315-
!symlink("testing", path) &&
315+
!create_symlink(NULL, "testing", path) &&
316316
!lstat(path, &st1) &&
317317
S_ISLNK(st1.st_mode))
318318
unlink(path); /* good */

compat/mingw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2809,7 +2809,7 @@ int link(const char *oldpath, const char *newpath)
28092809
return 0;
28102810
}
28112811

2812-
int symlink(const char *target, const char *link)
2812+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
28132813
{
28142814
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
28152815
int len;

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
@@ -291,7 +291,7 @@ static int write_entry(struct cache_entry *ce,
291291
if (!has_symlinks || to_tempfile)
292292
goto write_file_entry;
293293

294-
ret = symlink(new_blob, path);
294+
ret = create_symlink(state->istate, new_blob, path);
295295
free(new_blob);
296296
if (ret)
297297
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
@@ -407,6 +407,15 @@ static inline int git_has_dir_sep(const char *path)
407407
#define is_mount_point is_mount_point_via_stat
408408
#endif
409409

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

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ static int update_file_flags(struct merge_options *opt,
986986
char *lnk = xmemdupz(buf, size);
987987
safe_create_leading_directories_const(path);
988988
unlink(path);
989-
if (symlink(lnk, path))
989+
if (create_symlink(&opt->priv->orig_index, lnk, path))
990990
ret = err(opt, _("failed to symlink '%s': %s"),
991991
path, strerror(errno));
992992
free(lnk);

refs/files-backend.c

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

17941794
if (ret)

0 commit comments

Comments
 (0)