copy: drop dependency on `the_repository`

When copying a file we need to potentially adapt permissions of the new file based on whether or not "core.shared" is enabled. Parsing this configuration makes us implicitly depend on `the_repository`. Refactor the code to instead require the caller to pass in a repository so that we can remove `USE_THE_REPOSITORY_VARIABLE`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 16, 2026 at 17:28 UTC 0717b3595d4ebfca396134e4356e473c794aa1c9
10 files changed +24 -20
builtin/clone.c
+1 -1
@@ -335,7 +335,7 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
335 die_errno(_("failed to create link '%s'"), dest->buf);
336 option_no_hardlinks = 1;
337 }
338 - if (copy_file_with_time(dest->buf, src->buf, 0666))
338 + if (copy_file_with_time(the_repository, dest->buf, src->buf, 0666))
339 die_errno(_("failed to copy file to '%s'"), dest->buf);
340 }
341
builtin/difftool.c
+2 -2
@@ -552,7 +552,7 @@ static int run_dir_diff(struct repository *repo,
552 struct stat st;
553 if (stat(wtdir.buf, &st))
554 st.st_mode = 0644;
555 - if (copy_file(rdir.buf, wtdir.buf,
555 + if (copy_file(repo, rdir.buf, wtdir.buf,
556 st.st_mode)) {
557 ret = error("could not copy '%s' to '%s'", wtdir.buf, rdir.buf);
558 goto finish;
@@ -658,7 +658,7 @@ static int run_dir_diff(struct repository *repo,
658 warning("%s", "");
659 err = 1;
660 } else if (unlink(wtdir.buf) ||
661 - copy_file(wtdir.buf, rdir.buf, st.st_mode))
661 + copy_file(repo, wtdir.buf, rdir.buf, st.st_mode))
662 warning_errno(_("could not copy '%s' to '%s'"),
663 rdir.buf, wtdir.buf);
664 }
builtin/worktree.c
+2 -2
@@ -349,7 +349,7 @@ static void copy_sparse_checkout(const char *worktree_git_dir)
349
350 if (file_exists(from_file)) {
351 if (safe_create_leading_directories(the_repository, to_file) ||
352 - copy_file(to_file, from_file, 0666))
352 + copy_file(the_repository, to_file, from_file, 0666))
353 error(_("failed to copy '%s' to '%s'; sparse-checkout may not work correctly"),
354 from_file, to_file);
355 }
@@ -368,7 +368,7 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)
368 int bare;
369
370 if (safe_create_leading_directories(the_repository, to_file) ||
371 - copy_file(to_file, from_file, 0666)) {
371 + copy_file(the_repository, to_file, from_file, 0666)) {
372 error(_("failed to copy worktree config from '%s' to '%s'"),
373 from_file, to_file);
374 goto worktree_copy_cleanup;
bundle-uri.c
+1 -1
@@ -396,7 +396,7 @@ static int copy_uri_to_file(const char *filename, const char *uri)
396 uri = out;
397
398 /* Copy as a file */
399 - return copy_file(filename, uri, 0);
399 + return copy_file(the_repository, filename, uri, 0);
400 }
401
402 static int unbundle_from_file(struct repository *r, const char *file)
copy.c
+6 -6
@@ -1,5 +1,3 @@
1 -#define USE_THE_REPOSITORY_VARIABLE
2 -
1 #include "git-compat-util.h"
2 #include "copy.h"
3 #include "path.h"
@@ -35,7 +33,8 @@ static int copy_times(const char *dst, const char *src)
33 return 0;
34 }
35
38 -int copy_file(const char *dst, const char *src, int mode)
36 +int copy_file(struct repository *repo,
37 + const char *dst, const char *src, int mode)
38 {
39 int fdi, fdo, status;
40
@@ -59,15 +58,16 @@ int copy_file(const char *dst, const char *src, int mode)
58 if (close(fdo) != 0)
59 return error_errno("%s: close error", dst);
60
62 - if (!status && adjust_shared_perm(the_repository, dst))
61 + if (!status && adjust_shared_perm(repo, dst))
62 return -1;
63
64 return status;
65 }
66
68 -int copy_file_with_time(const char *dst, const char *src, int mode)
67 +int copy_file_with_time(struct repository *repo,
68 + const char *dst, const char *src, int mode)
69 {
70 - int status = copy_file(dst, src, mode);
70 + int status = copy_file(repo, dst, src, mode);
71 if (!status)
72 return copy_times(dst, src);
73 return status;
copy.h
+6 -2
@@ -1,10 +1,14 @@
1 #ifndef COPY_H
2 #define COPY_H
3
4 +struct repository;
5 +
6 #define COPY_READ_ERROR (-2)
7 #define COPY_WRITE_ERROR (-3)
8 int copy_fd(int ifd, int ofd);
7 -int copy_file(const char *dst, const char *src, int mode);
8 -int copy_file_with_time(const char *dst, const char *src, int mode);
9 +int copy_file(struct repository *repo,
10 + const char *dst, const char *src, int mode);
11 +int copy_file_with_time(struct repository *repo,
12 + const char *dst, const char *src, int mode);
13
14 #endif /* COPY_H */
refs/files-backend.c
+1 -1
@@ -1736,7 +1736,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
1736 goto out;
1737 }
1738
1739 - if (copy && log && copy_file(tmp_renamed_log.buf, sb_oldref.buf, 0644)) {
1739 + if (copy && log && copy_file(refs->base.repo, tmp_renamed_log.buf, sb_oldref.buf, 0644)) {
1740 ret = error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
1741 oldrefname, strerror(errno));
1742 goto out;
rerere.c
+1 -1
@@ -756,7 +756,7 @@ static void do_rerere_one_path(struct index_state *istate,
756 /* Has the user resolved it already? */
757 if (variant >= 0) {
758 if (!handle_file(istate, path, NULL, NULL)) {
759 - copy_file(rerere_path(&buf, id, "postimage"), path, 0666);
759 + copy_file(the_repository, rerere_path(&buf, id, "postimage"), path, 0666);
760 id->collection->status[variant] |= RR_HAS_POSTIMAGE;
761 fprintf_ln(stderr, _("Recorded resolution for '%s'."), path);
762 free_rerere_id(rr_item);
sequencer.c
+3 -3
@@ -2419,7 +2419,7 @@ static int do_pick_commit(struct repository *r,
2419 } else {
2420 const char *dest = git_path_squash_msg(r);
2421 unlink(dest);
2422 - if (copy_file(dest, rebase_path_squash_msg(), 0666)) {
2422 + if (copy_file(r, dest, rebase_path_squash_msg(), 0666)) {
2423 res = error(_("could not copy '%s' to '%s'"),
2424 rebase_path_squash_msg(), dest);
2425 goto leave;
@@ -3864,11 +3864,11 @@ static int error_failed_squash(struct repository *r,
3864 int subject_len,
3865 const char *subject)
3866 {
3867 - if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666))
3867 + if (copy_file(r, rebase_path_message(), rebase_path_squash_msg(), 0666))
3868 return error(_("could not copy '%s' to '%s'"),
3869 rebase_path_squash_msg(), rebase_path_message());
3870 unlink(git_path_merge_msg(r));
3871 - if (copy_file(git_path_merge_msg(r), rebase_path_message(), 0666))
3871 + if (copy_file(r, git_path_merge_msg(r), rebase_path_message(), 0666))
3872 return error(_("could not copy '%s' to '%s'"),
3873 rebase_path_message(),
3874 git_path_merge_msg(r));
setup.c
+1 -1
@@ -2331,7 +2331,7 @@ static void copy_templates_1(struct repository *repo,
2331 strbuf_release(&lnk);
2332 }
2333 else if (S_ISREG(st_template.st_mode)) {
2334 - if (copy_file(path->buf, template_path->buf, st_template.st_mode))
2334 + if (copy_file(repo, path->buf, template_path->buf, st_template.st_mode))
2335 die_errno(_("cannot copy '%s' to '%s'"),
2336 template_path->buf, path->buf);
2337 }