Skip to content

Commit 2747eb5

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 27c9ddb commit 2747eb5

File tree

9 files changed

+21
-9
lines changed

9 files changed

+21
-9
lines changed

apply.c

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

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

builtin/difftool.c

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

compat/mingw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3091,7 +3091,7 @@ int link(const char *oldpath, const char *newpath)
30913091
return 0;
30923092
}
30933093

3094-
int symlink(const char *target, const char *link)
3094+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link)
30953095
{
30963096
wchar_t wtarget[MAX_LONG_PATH], wlink[MAX_LONG_PATH];
30973097
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
@@ -322,7 +322,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
322322
if (!has_symlinks || to_tempfile)
323323
goto write_file_entry;
324324

325-
ret = symlink(new_blob, path);
325+
ret = create_symlink(state->istate, new_blob, path);
326326
free(new_blob);
327327
if (ret)
328328
return error_errno("unable to create symlink %s", path);

git-compat-util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,16 @@ static inline int git_has_dir_sep(const char *path)
641641
#define is_mount_point is_mount_point_via_stat
642642
#endif
643643

644+
#ifndef create_symlink
645+
struct index_state;
646+
static inline int git_create_symlink(struct index_state *index UNUSED,
647+
const char *target, const char *link)
648+
{
649+
return symlink(target, link);
650+
}
651+
#define create_symlink git_create_symlink
652+
#endif
653+
644654
#ifndef query_user_email
645655
#define query_user_email() NULL
646656
#endif

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ static int update_file_flags(struct merge_options *opt,
10041004
char *lnk = xmemdupz(buf, size);
10051005
safe_create_leading_directories_const(path);
10061006
unlink(path);
1007-
if (symlink(lnk, path))
1007+
if (create_symlink(&opt->priv->orig_index, lnk, path))
10081008
ret = err(opt, _("failed to symlink '%s': %s"),
10091009
path, strerror(errno));
10101010
free(lnk);

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
20352035

20362036
char *ref_path = get_locked_file_path(&lock->lk);
20372037
unlink(ref_path);
2038-
ret = symlink(target, ref_path);
2038+
ret = create_symlink(NULL, target, ref_path);
20392039
free(ref_path);
20402040

20412041
if (ret)

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
21342134
if (strbuf_readlink(&lnk, template_path->buf,
21352135
st_template.st_size) < 0)
21362136
die_errno(_("cannot readlink '%s'"), template_path->buf);
2137-
if (symlink(lnk.buf, path->buf))
2137+
if (create_symlink(NULL, lnk.buf, path->buf))
21382138
die_errno(_("cannot symlink '%s' '%s'"),
21392139
lnk.buf, path->buf);
21402140
strbuf_release(&lnk);
@@ -2395,7 +2395,7 @@ static int create_default_files(const char *template_path,
23952395
path = git_path_buf(&buf, "tXXXXXX");
23962396
if (!close(xmkstemp(path)) &&
23972397
!unlink(path) &&
2398-
!symlink("testing", path) &&
2398+
!create_symlink(NULL, "testing", path) &&
23992399
!lstat(path, &st1) &&
24002400
S_ISLNK(st1.st_mode))
24012401
unlink(path); /* good */

0 commit comments

Comments
 (0)