environment: make `get_graft_file()` accept a repository

The `get_graft_file()` function retrieves the path to the graft file of `the_repository`. Make it accept a `struct repository` such that it can work on arbitrary repositories and make it part of the repository subsystem. This reduces our reliance on `the_repository` and clarifies scope. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 12, 2024 at 13:29 UTC 14c90ac0885c2fd1668d64084975a9ab9ef86318
6 files changed +11 -13
builtin/replace.c
+1 -2
@@ -11,7 +11,6 @@
11 #include "builtin.h"
12 #include "config.h"
13 #include "editor.h"
14 -#include "environment.h"
14 #include "gettext.h"
15 #include "hex.h"
16 #include "refs.h"
@@ -514,7 +513,7 @@ static int create_graft(int argc, const char **argv, int force, int gentle)
513
514 static int convert_graft_file(int force)
515 {
517 - const char *graft_file = get_graft_file(the_repository);
516 + const char *graft_file = repo_get_graft_file(the_repository);
517 FILE *fp = fopen_or_warn(graft_file, "r");
518 struct strbuf buf = STRBUF_INIT, err = STRBUF_INIT;
519 struct strvec args = STRVEC_INIT;
commit.c
+2 -2
@@ -292,14 +292,14 @@ static int read_graft_file(struct repository *r, const char *graft_file)
292
293 void prepare_commit_graft(struct repository *r)
294 {
295 - char *graft_file;
295 + const char *graft_file;
296
297 if (r->parsed_objects->commit_graft_prepared)
298 return;
299 if (!startup_info->have_repository)
300 return;
301
302 - graft_file = get_graft_file(r);
302 + graft_file = repo_get_graft_file(r);
303 read_graft_file(r, graft_file);
304 /* make sure shallows are read */
305 is_repository_shallow(r);
environment.c
-7
@@ -306,13 +306,6 @@ int odb_pack_keep(const char *name)
306 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
307 }
308
309 -char *get_graft_file(struct repository *r)
310 -{
311 - if (!r->graft_file)
312 - BUG("git environment hasn't been setup");
313 - return r->graft_file;
314 -}
315 -
309 static void set_git_dir_1(const char *path)
310 {
311 xsetenv(GIT_DIR_ENVIRONMENT, path, 1);
environment.h
-2
@@ -1,7 +1,6 @@
1 #ifndef ENVIRONMENT_H
2 #define ENVIRONMENT_H
3
4 -struct repository;
4 struct strvec;
5
6 /*
@@ -106,7 +105,6 @@ int have_git_dir(void);
105 extern int is_bare_repository_cfg;
106 int is_bare_repository(void);
107 extern char *git_work_tree_cfg;
109 -char *get_graft_file(struct repository *r);
108 void set_git_dir(const char *path, int make_realpath);
109 const char *get_git_namespace(void);
110 const char *strip_namespace(const char *namespaced_ref);
repository.c
+7
@@ -119,6 +119,13 @@ const char *repo_get_index_file(struct repository *repo)
119 return repo->index_file;
120 }
121
122 +const char *repo_get_graft_file(struct repository *repo)
123 +{
124 + if (!repo->graft_file)
125 + BUG("repository hasn't been set up");
126 + return repo->graft_file;
127 +}
128 +
129 static void repo_set_commondir(struct repository *repo,
130 const char *commondir)
131 {
repository.h
+1
@@ -210,6 +210,7 @@ const char *repo_get_git_dir(struct repository *repo);
210 const char *repo_get_common_dir(struct repository *repo);
211 const char *repo_get_object_directory(struct repository *repo);
212 const char *repo_get_index_file(struct repository *repo);
213 +const char *repo_get_graft_file(struct repository *repo);
214
215 /*
216 * Define a custom repository layout. Any field can be NULL, which