]> git.ipfire.org Git - thirdparty/git.git/commitdiff
path: adjust last remaining users of `the_repository`
authorPatrick Steinhardt <ps@pks.im>
Fri, 7 Feb 2025 11:03:41 +0000 (12:03 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 28 Feb 2025 21:54:11 +0000 (13:54 -0800)
With the preceding refactorings we now only have a couple of implicit
users of `the_repository` left in the "path" subsystem, all of which
depend on global state via `calc_shared_perm()`. Make the dependency on
`the_repository` explicit by passing the repo as a parameter instead and
adjust callers accordingly.

Note that this change bubbles up into a couple of subsystems that were
previously declared as free from `the_repository`. Instead of marking
all of them as `the_repository`-dependent again, we instead use the
repository that is available in the calling context. There are three
exceptions though with "copy.c", "pack-write.c" and "tempfile.c".
Adjusting these would require us to adapt callsites all over the place,
so this is left for a future iteration.

Mark "path.c" as free from `the_repository`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
17 files changed:
builtin/clone.c
commit-graph.c
copy.c
loose.c
midx-write.c
object-file.c
pack-bitmap-write.c
pack-write.c
path.c
path.h
read-cache.c
refs/files-backend.c
refs/reftable-backend.c
server-info.c
setup.c
tempfile.c
tmp-objdir.c

index 5ae6ee9db9b4fbcd1b2ad79d6929b870999d2c6c..23eeb782aa4fd2891cf9f779c8d660af3d6b26cf 100644 (file)
@@ -1220,7 +1220,7 @@ int cmd_clone(int argc,
 
        strbuf_reset(&buf);
        strbuf_addf(&buf, "%s/refs", git_dir);
-       safe_create_dir(buf.buf, 1);
+       safe_create_dir(the_repository, buf.buf, 1);
 
        /*
         * additional config can be injected with -c, make sure it's included
index 2a2999a6b886905276a0c39dda6135f0c92aa361..1021ccb983d4ee02655df851f792c3ca24edc99c 100644 (file)
@@ -2084,7 +2084,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
                        return -1;
                }
 
-               if (adjust_shared_perm(get_tempfile_path(graph_layer))) {
+               if (adjust_shared_perm(the_repository, get_tempfile_path(graph_layer))) {
                        error(_("unable to adjust shared permissions for '%s'"),
                              get_tempfile_path(graph_layer));
                        return -1;
diff --git a/copy.c b/copy.c
index d9d209201269055b1d6e8c2fcccf436fdc8d3071..b668209b6c24fd7fb6af7ed68816a2c37e910c6a 100644 (file)
--- a/copy.c
+++ b/copy.c
@@ -1,3 +1,5 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "git-compat-util.h"
 #include "copy.h"
 #include "path.h"
@@ -57,7 +59,7 @@ 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(dst))
+       if (!status && adjust_shared_perm(the_repository, dst))
                return -1;
 
        return status;
diff --git a/loose.c b/loose.c
index 51ef490f935adc8f664bdee047cf15e5ca86082b..bb602aaa366bf7a36789d28c7bc7566f54c8e437 100644 (file)
--- a/loose.c
+++ b/loose.c
@@ -190,7 +190,7 @@ static int write_one_object(struct repository *repo, const struct object_id *oid
                goto errout;
        if (close(fd))
                goto errout;
-       adjust_shared_perm(path.buf);
+       adjust_shared_perm(repo, path.buf);
        rollback_lock_file(&lock);
        strbuf_release(&buf);
        strbuf_release(&path);
index 61b59d557d3ba46a39db74c62393545cabf50f2c..48d6558253ec9357a1edf709af7c5e90df8e22c1 100644 (file)
@@ -1336,7 +1336,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
                        return -1;
                }
 
-               if (adjust_shared_perm(get_tempfile_path(incr))) {
+               if (adjust_shared_perm(r, get_tempfile_path(incr))) {
                        error(_("unable to adjust shared permissions for '%s'"),
                              get_tempfile_path(incr));
                        return -1;
index dc9fcaf3e973808282338dfb042ccc5eb683b3d0..5d782417e390a5d005056ca2df1e45ea10f88d03 100644 (file)
@@ -388,7 +388,7 @@ int mkdir_in_gitdir(const char *path)
                }
                strbuf_release(&sb);
        }
-       return adjust_shared_perm(path);
+       return adjust_shared_perm(the_repository, path);
 }
 
 static enum scld_error safe_create_leading_directories_1(char *path, int share)
@@ -437,7 +437,7 @@ static enum scld_error safe_create_leading_directories_1(char *path, int share)
                                ret = SCLD_VANISHED;
                        else
                                ret = SCLD_FAILED;
-               } else if (share && adjust_shared_perm(path)) {
+               } else if (share && adjust_shared_perm(the_repository, path)) {
                        ret = SCLD_PERMS;
                }
                *slash = slash_character;
@@ -2105,7 +2105,7 @@ retry:
        }
 
 out:
-       if (adjust_shared_perm(filename))
+       if (adjust_shared_perm(the_repository, filename))
                return error(_("unable to set permission to '%s'"), filename);
        return 0;
 }
@@ -2181,7 +2181,7 @@ static int create_tmpfile(struct strbuf *tmp, const char *filename)
                strbuf_add(tmp, filename, dirlen - 1);
                if (mkdir(tmp->buf, 0777) && errno != EEXIST)
                        return -1;
-               if (adjust_shared_perm(tmp->buf))
+               if (adjust_shared_perm(the_repository, tmp->buf))
                        return -1;
 
                /* Try again */
index a06a1f35c619b3b01e63a506a6d4312e14cf181c..34e86d49947d231abb6c9958e4e02810f5a723db 100644 (file)
@@ -1072,7 +1072,7 @@ void bitmap_writer_finish(struct bitmap_writer *writer,
        finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
                          CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
 
-       if (adjust_shared_perm(tmp_file.buf))
+       if (adjust_shared_perm(the_repository, tmp_file.buf))
                die_errno("unable to make temporary bitmap file readable");
 
        if (rename(tmp_file.buf, filename))
index a2faeb1895e41f4c17281380478f1f2cabcc6f24..c3f4e66f02e28bbfc5305ce413058212d846b28d 100644 (file)
@@ -1,3 +1,5 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "git-compat-util.h"
 #include "environment.h"
 #include "gettext.h"
@@ -287,7 +289,7 @@ char *write_rev_file_order(const struct git_hash_algo *hash_algo,
        write_rev_index_positions(f, pack_order, nr_objects);
        write_rev_trailer(hash_algo, f, hash);
 
-       if (adjust_shared_perm(path) < 0)
+       if (adjust_shared_perm(the_repository, path) < 0)
                die(_("failed to make %s readable"), path);
 
        finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
@@ -350,7 +352,7 @@ static char *write_mtimes_file(const struct git_hash_algo *hash_algo,
        write_mtimes_objects(f, to_pack, objects, nr_objects);
        write_mtimes_trailer(hash_algo, f, hash);
 
-       if (adjust_shared_perm(mtimes_name) < 0)
+       if (adjust_shared_perm(the_repository, mtimes_name) < 0)
                die(_("failed to make %s readable"), mtimes_name);
 
        finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
@@ -565,12 +567,12 @@ void stage_tmp_packfiles(const struct git_hash_algo *hash_algo,
        char *rev_tmp_name = NULL;
        char *mtimes_tmp_name = NULL;
 
-       if (adjust_shared_perm(pack_tmp_name))
+       if (adjust_shared_perm(the_repository, pack_tmp_name))
                die_errno("unable to make temporary pack file readable");
 
        *idx_tmp_name = (char *)write_idx_file(hash_algo, NULL, written_list,
                                               nr_written, pack_idx_opts, hash);
-       if (adjust_shared_perm(*idx_tmp_name))
+       if (adjust_shared_perm(the_repository, *idx_tmp_name))
                die_errno("unable to make temporary index file readable");
 
        rev_tmp_name = write_rev_file(hash_algo, NULL, written_list, nr_written,
diff --git a/path.c b/path.c
index a2f402baecf21a27895bf72968fc04d08c644e01..910756c8b3249c5306c0bd4c45d5172ba04b9da0 100644 (file)
--- a/path.c
+++ b/path.c
@@ -2,8 +2,6 @@
  * Utilities for paths and pathnames
  */
 
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "environment.h"
@@ -840,21 +838,22 @@ const char *enter_repo(const char *path, unsigned flags)
        return NULL;
 }
 
-int calc_shared_perm(int mode)
+int calc_shared_perm(struct repository *repo,
+                    int mode)
 {
        int tweak;
 
-       if (repo_settings_get_shared_repository(the_repository) < 0)
-               tweak = -repo_settings_get_shared_repository(the_repository);
+       if (repo_settings_get_shared_repository(repo) < 0)
+               tweak = -repo_settings_get_shared_repository(repo);
        else
-               tweak = repo_settings_get_shared_repository(the_repository);
+               tweak = repo_settings_get_shared_repository(repo);
 
        if (!(mode & S_IWUSR))
                tweak &= ~0222;
        if (mode & S_IXUSR)
                /* Copy read bits to execute bits */
                tweak |= (tweak & 0444) >> 2;
-       if (repo_settings_get_shared_repository(the_repository) < 0)
+       if (repo_settings_get_shared_repository(repo) < 0)
                mode = (mode & ~0777) | tweak;
        else
                mode |= tweak;
@@ -862,17 +861,17 @@ int calc_shared_perm(int mode)
        return mode;
 }
 
-
-int adjust_shared_perm(const char *path)
+int adjust_shared_perm(struct repository *repo,
+                      const char *path)
 {
        int old_mode, new_mode;
 
-       if (!repo_settings_get_shared_repository(the_repository))
+       if (!repo_settings_get_shared_repository(repo))
                return 0;
        if (get_st_mode_bits(path, &old_mode) < 0)
                return -1;
 
-       new_mode = calc_shared_perm(old_mode);
+       new_mode = calc_shared_perm(repo, old_mode);
        if (S_ISDIR(old_mode)) {
                /* Copy read bits to execute bits */
                new_mode |= (new_mode & 0444) >> 2;
@@ -891,7 +890,7 @@ int adjust_shared_perm(const char *path)
        return 0;
 }
 
-void safe_create_dir(const char *dir, int share)
+void safe_create_dir(struct repository *repo, const char *dir, int share)
 {
        if (mkdir(dir, 0777) < 0) {
                if (errno != EEXIST) {
@@ -899,7 +898,7 @@ void safe_create_dir(const char *dir, int share)
                        exit(1);
                }
        }
-       else if (share && adjust_shared_perm(dir))
+       else if (share && adjust_shared_perm(repo, dir))
                die(_("Could not make %s writable by group"), dir);
 }
 
diff --git a/path.h b/path.h
index 373404dd9d2a5f50370441ebf7e29a1fa7ffc34a..65fe968a13a191fea16dfab879faa9be18653cf5 100644 (file)
--- a/path.h
+++ b/path.h
@@ -141,8 +141,8 @@ const char *git_path_shallow(struct repository *r);
 
 int ends_with_path_components(const char *path, const char *components);
 
-int calc_shared_perm(int mode);
-int adjust_shared_perm(const char *path);
+int calc_shared_perm(struct repository *repo, int mode);
+int adjust_shared_perm(struct repository *repo, const char *path);
 
 char *interpolate_path(const char *path, int real_home);
 
@@ -219,7 +219,7 @@ char *xdg_cache_home(const char *filename);
  * directories under $GIT_DIR.  Don't use it for working tree
  * directories.
  */
-void safe_create_dir(const char *dir, int share);
+void safe_create_dir(struct repository *repo, const char *dir, int share);
 
 # ifdef USE_THE_REPOSITORY_VARIABLE
 #  include "strbuf.h"
index 66ad0015a7f75a44d444fe9411810a59e46ea941..900738e7a8d6aa5df1b1cfcafa68d24de78c541c 100644 (file)
@@ -3290,7 +3290,7 @@ static int write_shared_index(struct index_state *istate,
 
        if (ret)
                return ret;
-       ret = adjust_shared_perm(get_tempfile_path(*temp));
+       ret = adjust_shared_perm(the_repository, get_tempfile_path(*temp));
        if (ret) {
                error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp));
                return ret;
index 29f08dced40418eb815072c6335e0c3d1a45c7d8..6c6e67dc1c48ed0936fdc8395faf663f2c8c7c26 100644 (file)
@@ -1831,7 +1831,7 @@ static int log_ref_setup(struct files_ref_store *refs,
        }
 
        if (*logfd >= 0)
-               adjust_shared_perm(logfile);
+               adjust_shared_perm(the_repository, logfile);
 
        free(logfile);
        return 0;
@@ -3488,8 +3488,8 @@ static int files_ref_store_create_on_disk(struct ref_store *ref_store,
         *   they do not understand the reference format extension.
         */
        strbuf_addf(&sb, "%s/refs", ref_store->gitdir);
-       safe_create_dir(sb.buf, 1);
-       adjust_shared_perm(sb.buf);
+       safe_create_dir(the_repository, sb.buf, 1);
+       adjust_shared_perm(the_repository, sb.buf);
 
        /*
         * There is no need to create directories for common refs when creating
@@ -3501,11 +3501,11 @@ static int files_ref_store_create_on_disk(struct ref_store *ref_store,
                 */
                strbuf_reset(&sb);
                files_ref_path(refs, &sb, "refs/heads");
-               safe_create_dir(sb.buf, 1);
+               safe_create_dir(the_repository, sb.buf, 1);
 
                strbuf_reset(&sb);
                files_ref_path(refs, &sb, "refs/tags");
-               safe_create_dir(sb.buf, 1);
+               safe_create_dir(the_repository, sb.buf, 1);
        }
 
        strbuf_release(&sb);
index d39a14c5a469d7d219362e9eae4f578784d65a5b..9c54e2c1737164f57877046748c91f7d932aa9f3 100644 (file)
@@ -380,7 +380,7 @@ static struct ref_store *reftable_be_init(struct repository *repo,
        default:
                BUG("unknown hash algorithm %d", repo->hash_algo->format_id);
        }
-       refs->write_options.default_permissions = calc_shared_perm(0666 & ~mask);
+       refs->write_options.default_permissions = calc_shared_perm(the_repository, 0666 & ~mask);
        refs->write_options.disable_auto_compact =
                !git_env_bool("GIT_TEST_REFTABLE_AUTOCOMPACTION", 1);
        refs->write_options.lock_timeout_ms = 100;
@@ -470,21 +470,21 @@ static int reftable_be_create_on_disk(struct ref_store *ref_store,
        struct strbuf sb = STRBUF_INIT;
 
        strbuf_addf(&sb, "%s/reftable", refs->base.gitdir);
-       safe_create_dir(sb.buf, 1);
+       safe_create_dir(the_repository, sb.buf, 1);
        strbuf_reset(&sb);
 
        strbuf_addf(&sb, "%s/HEAD", refs->base.gitdir);
        write_file(sb.buf, "ref: refs/heads/.invalid");
-       adjust_shared_perm(sb.buf);
+       adjust_shared_perm(the_repository, sb.buf);
        strbuf_reset(&sb);
 
        strbuf_addf(&sb, "%s/refs", refs->base.gitdir);
-       safe_create_dir(sb.buf, 1);
+       safe_create_dir(the_repository, sb.buf, 1);
        strbuf_reset(&sb);
 
        strbuf_addf(&sb, "%s/refs/heads", refs->base.gitdir);
        write_file(sb.buf, "this repository uses the reftable format");
-       adjust_shared_perm(sb.buf);
+       adjust_shared_perm(the_repository, sb.buf);
 
        strbuf_release(&sb);
        return 0;
index 31c3fdc118447d42745362935e3483c59b7e0bc2..1ca0e00d51e6c06f898db19339b02814017dfe9f 100644 (file)
@@ -125,7 +125,7 @@ static int update_info_file(struct repository *r, char *path,
        uic.cur_fp = NULL;
 
        if (uic_is_stale(&uic)) {
-               if (adjust_shared_perm(get_tempfile_path(f)) < 0)
+               if (adjust_shared_perm(r, get_tempfile_path(f)) < 0)
                        goto out;
                if (rename_tempfile(&f, path) < 0)
                        goto out;
diff --git a/setup.c b/setup.c
index aa65b93f530aa1a4d13807983946c1bfbb0f2533..71a3d66f05da3057f6b9beb2bb79b96b53c50e00 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -2088,7 +2088,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
         * with the way the namespace under .git/ is organized, should
         * be really carefully chosen.
         */
-       safe_create_dir(path->buf, 1);
+       safe_create_dir(the_repository, path->buf, 1);
        while ((de = readdir(dir)) != NULL) {
                struct stat st_git, st_template;
                int exists = 0;
@@ -2352,7 +2352,7 @@ static int create_default_files(const char *template_path,
         * shared-repository settings, we would need to fix them up.
         */
        if (repo_settings_get_shared_repository(the_repository)) {
-               adjust_shared_perm(repo_get_git_dir(the_repository));
+               adjust_shared_perm(the_repository, repo_get_git_dir(the_repository));
        }
 
        initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit);
@@ -2413,15 +2413,15 @@ static void create_object_directory(void)
        strbuf_addstr(&path, repo_get_object_directory(the_repository));
        baselen = path.len;
 
-       safe_create_dir(path.buf, 1);
+       safe_create_dir(the_repository, path.buf, 1);
 
        strbuf_setlen(&path, baselen);
        strbuf_addstr(&path, "/pack");
-       safe_create_dir(path.buf, 1);
+       safe_create_dir(the_repository, path.buf, 1);
 
        strbuf_setlen(&path, baselen);
        strbuf_addstr(&path, "/info");
-       safe_create_dir(path.buf, 1);
+       safe_create_dir(the_repository, path.buf, 1);
 
        strbuf_release(&path);
 }
@@ -2588,7 +2588,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
         */
        git_config(platform_core_config, NULL);
 
-       safe_create_dir(git_dir, 0);
+       safe_create_dir(the_repository, git_dir, 0);
 
        reinit = create_default_files(template_dir, original_git_dir,
                                      &repo_fmt, init_shared_repository);
index ed88cf84314753443c8c42f0043320957d65d165..82dfa3d82f2ac9c842088d6833181cd6214b56a9 100644 (file)
@@ -42,6 +42,8 @@
  * file created by its parent.
  */
 
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "path.h"
@@ -148,7 +150,7 @@ struct tempfile *create_tempfile_mode(const char *path, int mode)
                return NULL;
        }
        activate_tempfile(tempfile);
-       if (adjust_shared_perm(tempfile->filename.buf)) {
+       if (adjust_shared_perm(the_repository, tempfile->filename.buf)) {
                int save_errno = errno;
                error("cannot fix permission bits on %s", tempfile->filename.buf);
                delete_tempfile(&tempfile);
index 0ea078a5c5f4eb1ff465aa64ae962a0ccd44fb8b..31d16a4c2c576e26a93a1fb245d3dffbe69d650f 100644 (file)
@@ -207,10 +207,12 @@ static int read_dir_paths(struct string_list *out, const char *path)
        return 0;
 }
 
-static int migrate_paths(struct strbuf *src, struct strbuf *dst,
+static int migrate_paths(struct tmp_objdir *t,
+                        struct strbuf *src, struct strbuf *dst,
                         enum finalize_object_file_flags flags);
 
-static int migrate_one(struct strbuf *src, struct strbuf *dst,
+static int migrate_one(struct tmp_objdir *t,
+                      struct strbuf *src, struct strbuf *dst,
                       enum finalize_object_file_flags flags)
 {
        struct stat st;
@@ -219,11 +221,11 @@ static int migrate_one(struct strbuf *src, struct strbuf *dst,
                return -1;
        if (S_ISDIR(st.st_mode)) {
                if (!mkdir(dst->buf, 0777)) {
-                       if (adjust_shared_perm(dst->buf))
+                       if (adjust_shared_perm(t->repo, dst->buf))
                                return -1;
                } else if (errno != EEXIST)
                        return -1;
-               return migrate_paths(src, dst, flags);
+               return migrate_paths(t, src, dst, flags);
        }
        return finalize_object_file_flags(src->buf, dst->buf, flags);
 }
@@ -233,7 +235,8 @@ static int is_loose_object_shard(const char *name)
        return strlen(name) == 2 && isxdigit(name[0]) && isxdigit(name[1]);
 }
 
-static int migrate_paths(struct strbuf *src, struct strbuf *dst,
+static int migrate_paths(struct tmp_objdir *t,
+                        struct strbuf *src, struct strbuf *dst,
                         enum finalize_object_file_flags flags)
 {
        size_t src_len = src->len, dst_len = dst->len;
@@ -255,7 +258,7 @@ static int migrate_paths(struct strbuf *src, struct strbuf *dst,
                if (is_loose_object_shard(name))
                        flags_copy |= FOF_SKIP_COLLISION_CHECK;
 
-               ret |= migrate_one(src, dst, flags_copy);
+               ret |= migrate_one(t, src, dst, flags_copy);
 
                strbuf_setlen(src, src_len);
                strbuf_setlen(dst, dst_len);
@@ -283,7 +286,7 @@ int tmp_objdir_migrate(struct tmp_objdir *t)
        strbuf_addbuf(&src, &t->path);
        strbuf_addstr(&dst, repo_get_object_directory(t->repo));
 
-       ret = migrate_paths(&src, &dst, 0);
+       ret = migrate_paths(t, &src, &dst, 0);
 
        strbuf_release(&src);
        strbuf_release(&dst);