]> git.ipfire.org Git - thirdparty/git.git/commitdiff
copy: drop dependency on `the_repository`
authorPatrick Steinhardt <ps@pks.im>
Thu, 16 Jul 2026 15:28:23 +0000 (17:28 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Jul 2026 21:10:08 +0000 (14:10 -0700)
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>
builtin/clone.c
builtin/difftool.c
builtin/worktree.c
bundle-uri.c
copy.c
copy.h
refs/files-backend.c
rerere.c
sequencer.c
setup.c

index d60d1b60bc238cc9f6dd0a8418ba83107b7e944c..18603dd4ce715fd1821a096efa66832bb1c437df 100644 (file)
@@ -335,7 +335,7 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
                                die_errno(_("failed to create link '%s'"), dest->buf);
                        option_no_hardlinks = 1;
                }
-               if (copy_file_with_time(dest->buf, src->buf, 0666))
+               if (copy_file_with_time(the_repository, dest->buf, src->buf, 0666))
                        die_errno(_("failed to copy file to '%s'"), dest->buf);
        }
 
index 26778f8515deefc835c041a69a98655718e87bb1..5e7777fbe48daa4033b5fb97d157b2b3b7efeaa2 100644 (file)
@@ -552,7 +552,7 @@ static int run_dir_diff(struct repository *repo,
                                        struct stat st;
                                        if (stat(wtdir.buf, &st))
                                                st.st_mode = 0644;
-                                       if (copy_file(rdir.buf, wtdir.buf,
+                                       if (copy_file(repo, rdir.buf, wtdir.buf,
                                                      st.st_mode)) {
                                                ret = error("could not copy '%s' to '%s'", wtdir.buf, rdir.buf);
                                                goto finish;
@@ -658,7 +658,7 @@ static int run_dir_diff(struct repository *repo,
                                warning("%s", "");
                                err = 1;
                        } else if (unlink(wtdir.buf) ||
-                                  copy_file(wtdir.buf, rdir.buf, st.st_mode))
+                                  copy_file(repo, wtdir.buf, rdir.buf, st.st_mode))
                                warning_errno(_("could not copy '%s' to '%s'"),
                                              rdir.buf, wtdir.buf);
                }
index d21c43fde38b5e64a7ae1a3bbc4b5d81cb26244a..84b01960fb2fba2e88b510cbcd17db74f3f2c5d4 100644 (file)
@@ -349,7 +349,7 @@ static void copy_sparse_checkout(const char *worktree_git_dir)
 
        if (file_exists(from_file)) {
                if (safe_create_leading_directories(the_repository, to_file) ||
-                       copy_file(to_file, from_file, 0666))
+                       copy_file(the_repository, to_file, from_file, 0666))
                        error(_("failed to copy '%s' to '%s'; sparse-checkout may not work correctly"),
                                from_file, to_file);
        }
@@ -368,7 +368,7 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)
                int bare;
 
                if (safe_create_leading_directories(the_repository, to_file) ||
-                       copy_file(to_file, from_file, 0666)) {
+                       copy_file(the_repository, to_file, from_file, 0666)) {
                        error(_("failed to copy worktree config from '%s' to '%s'"),
                                from_file, to_file);
                        goto worktree_copy_cleanup;
index 3b2e347288c3b73a5e1aad63154ba754e3c0b833..ef37aebf30f6ac8426daca6d8f4857f4e295b09b 100644 (file)
@@ -396,7 +396,7 @@ static int copy_uri_to_file(const char *filename, const char *uri)
                uri = out;
 
        /* Copy as a file */
-       return copy_file(filename, uri, 0);
+       return copy_file(the_repository, filename, uri, 0);
 }
 
 static int unbundle_from_file(struct repository *r, const char *file)
diff --git a/copy.c b/copy.c
index b668209b6c24fd7fb6af7ed68816a2c37e910c6a..6074132050ca6f97eeba1c67e1e481e6b218290b 100644 (file)
--- a/copy.c
+++ b/copy.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "copy.h"
 #include "path.h"
@@ -35,7 +33,8 @@ static int copy_times(const char *dst, const char *src)
        return 0;
 }
 
-int copy_file(const char *dst, const char *src, int mode)
+int copy_file(struct repository *repo,
+             const char *dst, const char *src, int mode)
 {
        int fdi, fdo, status;
 
@@ -59,15 +58,16 @@ int copy_file(const char *dst, const char *src, int mode)
        if (close(fdo) != 0)
                return error_errno("%s: close error", dst);
 
-       if (!status && adjust_shared_perm(the_repository, dst))
+       if (!status && adjust_shared_perm(repo, dst))
                return -1;
 
        return status;
 }
 
-int copy_file_with_time(const char *dst, const char *src, int mode)
+int copy_file_with_time(struct repository *repo,
+                       const char *dst, const char *src, int mode)
 {
-       int status = copy_file(dst, src, mode);
+       int status = copy_file(repo, dst, src, mode);
        if (!status)
                return copy_times(dst, src);
        return status;
diff --git a/copy.h b/copy.h
index 2af77cba8649b322151fa1b410e6a9522690cde9..1059b118d615ec2a67b1503eb239cc8f22edd6d7 100644 (file)
--- a/copy.h
+++ b/copy.h
@@ -1,10 +1,14 @@
 #ifndef COPY_H
 #define COPY_H
 
+struct repository;
+
 #define COPY_READ_ERROR (-2)
 #define COPY_WRITE_ERROR (-3)
 int copy_fd(int ifd, int ofd);
-int copy_file(const char *dst, const char *src, int mode);
-int copy_file_with_time(const char *dst, const char *src, int mode);
+int copy_file(struct repository *repo,
+             const char *dst, const char *src, int mode);
+int copy_file_with_time(struct repository *repo,
+                       const char *dst, const char *src, int mode);
 
 #endif /* COPY_H */
index 3df56c25c8c585cd919aad9cefad41bd24914f0b..442c98414ecc08ef5cfd1287894c43f495455a10 100644 (file)
@@ -1736,7 +1736,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
                goto out;
        }
 
-       if (copy && log && copy_file(tmp_renamed_log.buf, sb_oldref.buf, 0644)) {
+       if (copy && log && copy_file(refs->base.repo, tmp_renamed_log.buf, sb_oldref.buf, 0644)) {
                ret = error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
                            oldrefname, strerror(errno));
                goto out;
index 8232542585cad411e4937c6c1ec3d1196879d199..bf5cfc6e513103334feb3831a13a83c197971755 100644 (file)
--- a/rerere.c
+++ b/rerere.c
@@ -756,7 +756,7 @@ static void do_rerere_one_path(struct index_state *istate,
        /* Has the user resolved it already? */
        if (variant >= 0) {
                if (!handle_file(istate, path, NULL, NULL)) {
-                       copy_file(rerere_path(&buf, id, "postimage"), path, 0666);
+                       copy_file(the_repository, rerere_path(&buf, id, "postimage"), path, 0666);
                        id->collection->status[variant] |= RR_HAS_POSTIMAGE;
                        fprintf_ln(stderr, _("Recorded resolution for '%s'."), path);
                        free_rerere_id(rr_item);
index 1355a99a0922681a4443b03df7387974009d1fee..63bc1ef215b3fc9acd0943c938d1ac38d5591840 100644 (file)
@@ -2419,7 +2419,7 @@ static int do_pick_commit(struct repository *r,
                } else {
                        const char *dest = git_path_squash_msg(r);
                        unlink(dest);
-                       if (copy_file(dest, rebase_path_squash_msg(), 0666)) {
+                       if (copy_file(r, dest, rebase_path_squash_msg(), 0666)) {
                                res = error(_("could not copy '%s' to '%s'"),
                                            rebase_path_squash_msg(), dest);
                                goto leave;
@@ -3864,11 +3864,11 @@ static int error_failed_squash(struct repository *r,
                               int subject_len,
                               const char *subject)
 {
-       if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666))
+       if (copy_file(r, rebase_path_message(), rebase_path_squash_msg(), 0666))
                return error(_("could not copy '%s' to '%s'"),
                        rebase_path_squash_msg(), rebase_path_message());
        unlink(git_path_merge_msg(r));
-       if (copy_file(git_path_merge_msg(r), rebase_path_message(), 0666))
+       if (copy_file(r, git_path_merge_msg(r), rebase_path_message(), 0666))
                return error(_("could not copy '%s' to '%s'"),
                             rebase_path_message(),
                             git_path_merge_msg(r));
diff --git a/setup.c b/setup.c
index 0de56a074f7c1581d705675bb02bec56a7752fab..91d61a5939067e3412671ff38e36a398be65c387 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -2331,7 +2331,7 @@ static void copy_templates_1(struct repository *repo,
                        strbuf_release(&lnk);
                }
                else if (S_ISREG(st_template.st_mode)) {
-                       if (copy_file(path->buf, template_path->buf, st_template.st_mode))
+                       if (copy_file(repo, path->buf, template_path->buf, st_template.st_mode))
                                die_errno(_("cannot copy '%s' to '%s'"),
                                          template_path->buf, path->buf);
                }