Skip to content

Commit 2249637

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 <johannes.schindelin@gmx.de>
1 parent f2ff2a5 commit 2249637

8 files changed

Lines changed: 20 additions & 8 deletions

File tree

apply.c

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

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

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ static int run_dir_diff(struct repository *repo,
544544
}
545545
add_path(&wtdir, wtdir_len, dst_path);
546546
if (dt_options->symlinks) {
547-
if (symlink(wtdir.buf, rdir.buf)) {
547+
if (create_symlink(lstate.istate, wtdir.buf, rdir.buf)) {
548548
ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
549549
goto finish;
550550
}

compat/mingw-posix.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,10 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
193193
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
194194
int link(const char *oldpath, const char *newpath);
195195
int uname(struct utsname *buf);
196-
int symlink(const char *target, const char *link);
197196
int readlink(const char *path, char *buf, size_t bufsiz);
197+
struct index_state;
198+
int mingw_create_symlink(struct index_state *index, const char *target, const char *link);
199+
#define create_symlink mingw_create_symlink
198200

199201
/*
200202
* replacements of existing functions

compat/mingw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2981,7 +2981,7 @@ int link(const char *oldpath, const char *newpath)
29812981
return 0;
29822982
}
29832983

2984-
int symlink(const char *target, const char *link)
2984+
int mingw_create_symlink(struct index_state *index UNUSED, const char *target, const char *link)
29852985
{
29862986
wchar_t wtarget[MAX_PATH], wlink[MAX_PATH];
29872987
int len;

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
@@ -346,6 +346,16 @@ static inline int git_has_dir_sep(const char *path)
346346
#define has_dir_sep(path) git_has_dir_sep(path)
347347
#endif
348348

349+
#ifndef create_symlink
350+
struct index_state;
351+
static inline int git_create_symlink(struct index_state *index UNUSED,
352+
const char *target, const char *link)
353+
{
354+
return symlink(target, link);
355+
}
356+
#define create_symlink git_create_symlink
357+
#endif
358+
349359
#ifndef query_user_email
350360
#define query_user_email() NULL
351361
#endif

refs/files-backend.c

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

21512151
ref_path = get_locked_file_path(&lock->lk);
21522152
unlink(ref_path);
2153-
ret = symlink(target, ref_path);
2153+
ret = create_symlink(NULL, target, ref_path);
21542154
free(ref_path);
21552155

21562156
if (ret)

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,7 +2325,7 @@ static void copy_templates_1(struct repository *repo,
23252325
if (strbuf_readlink(&lnk, template_path->buf,
23262326
st_template.st_size) < 0)
23272327
die_errno(_("cannot readlink '%s'"), template_path->buf);
2328-
if (symlink(lnk.buf, path->buf))
2328+
if (create_symlink(NULL, lnk.buf, path->buf))
23292329
die_errno(_("cannot symlink '%s' '%s'"),
23302330
lnk.buf, path->buf);
23312331
strbuf_release(&lnk);
@@ -2611,7 +2611,7 @@ static int create_default_files(struct repository *repo,
26112611
repo_git_path_replace(repo, &path, "tXXXXXX");
26122612
if (!close(xmkstemp(path.buf)) &&
26132613
!unlink(path.buf) &&
2614-
!symlink("testing", path.buf) &&
2614+
!create_symlink(NULL, "testing", path.buf) &&
26152615
!lstat(path.buf, &st1) &&
26162616
S_ISLNK(st1.st_mode))
26172617
unlink(path.buf); /* good */

0 commit comments

Comments
 (0)