git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 05/21] environment: make `get_graft_file()` accept a repository

From
Patrick Steinhardt <ps@pks.im>
Date
Sep 12, 2024, 11:29 UTC
Message-ID
<e3f3f6391da3c81a0c6333ef450270c359772622.1726139990.git.ps@pks.im>
In-Reply-To
<cover.1726139990.git.ps@pks.im>

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>
---
 builtin/replace.c | 3 +--
 commit.c          | 4 ++--
 environment.c     | 7 -------
 environment.h     | 2 --
 repository.c      | 7 +++++++
 repository.h      | 1 +
 6 files changed, 11 insertions(+), 13 deletions(-)
diff --git a/builtin/replace.c b/builtin/replace.c
index 34cc4672bc1..01161350b1f 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -11,7 +11,6 @@
 #include "builtin.h"
 #include "config.h"
 #include "editor.h"
-#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "refs.h"
@@ -514,7 +513,7 @@ static int create_graft(int argc, const char **argv, int force, int gentle)
 
 static int convert_graft_file(int force)
 {
-	const char *graft_file = get_graft_file(the_repository);
+	const char *graft_file = repo_get_graft_file(the_repository);
 	FILE *fp = fopen_or_warn(graft_file, "r");
 	struct strbuf buf = STRBUF_INIT, err = STRBUF_INIT;
 	struct strvec args = STRVEC_INIT;
diff --git a/commit.c b/commit.c
index 3238772f521..1f710a92a17 100644
--- a/commit.c
+++ b/commit.c
@@ -292,14 +292,14 @@ static int read_graft_file(struct repository *r, const char *graft_file)
 
 void prepare_commit_graft(struct repository *r)
 {
-	char *graft_file;
+	const char *graft_file;
 
 	if (r->parsed_objects->commit_graft_prepared)
 		return;
 	if (!startup_info->have_repository)
 		return;
 
-	graft_file = get_graft_file(r);
+	graft_file = repo_get_graft_file(r);
 	read_graft_file(r, graft_file);
 	/* make sure shallows are read */
 	is_repository_shallow(r);
diff --git a/environment.c b/environment.c
index 10ef77576c3..371f01a705d 100644
--- a/environment.c
+++ b/environment.c
@@ -306,13 +306,6 @@ int odb_pack_keep(const char *name)
 	return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 }
 
-char *get_graft_file(struct repository *r)
-{
-	if (!r->graft_file)
-		BUG("git environment hasn't been setup");
-	return r->graft_file;
-}
-
 static void set_git_dir_1(const char *path)
 {
 	xsetenv(GIT_DIR_ENVIRONMENT, path, 1);
diff --git a/environment.h b/environment.h
index ff590cfff73..d12c48481b6 100644
--- a/environment.h
+++ b/environment.h
@@ -1,7 +1,6 @@
 #ifndef ENVIRONMENT_H
 #define ENVIRONMENT_H
 
-struct repository;
 struct strvec;
 
 /*
@@ -106,7 +105,6 @@ int have_git_dir(void);
 extern int is_bare_repository_cfg;
 int is_bare_repository(void);
 extern char *git_work_tree_cfg;
-char *get_graft_file(struct repository *r);
 void set_git_dir(const char *path, int make_realpath);
 const char *get_git_namespace(void);
 const char *strip_namespace(const char *namespaced_ref);
diff --git a/repository.c b/repository.c
index 849a912b031..c2c231a7fd8 100644
--- a/repository.c
+++ b/repository.c
@@ -119,6 +119,13 @@ const char *repo_get_index_file(struct repository *repo)
 	return repo->index_file;
 }
 
+const char *repo_get_graft_file(struct repository *repo)
+{
+	if (!repo->graft_file)
+		BUG("repository hasn't been set up");
+	return repo->graft_file;
+}
+
 static void repo_set_commondir(struct repository *repo,
 			       const char *commondir)
 {
diff --git a/repository.h b/repository.h
index 15660ac2f19..ad0f984b444 100644
--- a/repository.h
+++ b/repository.h
@@ -210,6 +210,7 @@ const char *repo_get_git_dir(struct repository *repo);
 const char *repo_get_common_dir(struct repository *repo);
 const char *repo_get_object_directory(struct repository *repo);
 const char *repo_get_index_file(struct repository *repo);
+const char *repo_get_graft_file(struct repository *repo);
 
 /*
  * Define a custom repository layout. Any field can be NULL, which
-- 
2.46.0.551.gc5ee8f2d1c.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 75 of 92 in “environment: guard reliance on `the_repository`”
  1. 00/21 environment: guard reliance on `the_repository`Patrick Steinhardt, Aug 29, 2024
  2. 01/21 environment: make `get_git_dir()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  3. Justin ToblerAug 29, 2024
  4. Patrick SteinhardtAug 30, 2024
  5. 02/21 environment: make `get_git_common_dir()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  6. 03/21 environment: make `get_object_directory()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  7. 04/21 environment: make `get_index_file()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  8. 05/21 environment: make `get_graft_file()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  9. 06/21 environment: make `get_git_work_tree()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  10. 07/21 config: document `read_early_config()` and `read_very_early_config()`Patrick Steinhardt, Aug 29, 2024
  11. 08/21 config: make dependency on repo in `read_early_config()` explicitPatrick Steinhardt, Aug 29, 2024
  12. 09/21 environment: move `odb_mkstemp()` into object layerPatrick Steinhardt, Aug 29, 2024
  13. 10/21 environment: make `get_git_namespace()` self-containedPatrick Steinhardt, Aug 29, 2024
  14. 11/21 environment: move `set_git_dir()` and related into setup layerPatrick Steinhardt, Aug 29, 2024
  15. 12/21 environment: reorder header to split out `the_repository`-free sectionPatrick Steinhardt, Aug 29, 2024
  16. 13/21 environment: guard state depending on a repositoryPatrick Steinhardt, Aug 29, 2024
  17. 14/21 repo-settings: split out declarations into a standalone headerPatrick Steinhardt, Aug 29, 2024
  18. 15/21 branch: stop modifying `log_all_ref_updates` variablePatrick Steinhardt, Aug 29, 2024
  19. 16/21 refs: stop modifying global `log_all_ref_updates` variablePatrick Steinhardt, Aug 29, 2024
  20. 17/21 repo-settings: track defaults close to `struct repo_settings`Patrick Steinhardt, Aug 29, 2024
  21. 18/21 environment: stop storing "core.logAllRefUpdates" globallyPatrick Steinhardt, Aug 29, 2024
  22. 19/21 environment: stop storing "core.preferSymlinkRefs" globallyPatrick Steinhardt, Aug 29, 2024
  23. 20/21 environment: stop storing "core.warnAmbiguousRefs" globallyPatrick Steinhardt, Aug 29, 2024
  24. 21/21 environment: stop storing "core.notesRef" globallyPatrick Steinhardt, Aug 29, 2024
  25. Junio C HamanoAug 29, 2024
  26. Patrick SteinhardtAug 30, 2024
  27. Junio C HamanoAug 30, 2024
  28. Patrick SteinhardtSep 2, 2024
  29. 00/21 environment: guard reliance on `the_repository`Patrick Steinhardt, Aug 30, 2024
  30. 01/21 environment: make `get_git_dir()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  31. karthik nayakSep 11, 2024
  32. Patrick SteinhardtSep 12, 2024
  33. 02/21 environment: make `get_git_common_dir()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  34. 03/21 environment: make `get_object_directory()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  35. 04/21 environment: make `get_index_file()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  36. 05/21 environment: make `get_graft_file()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  37. 06/21 environment: make `get_git_work_tree()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  38. Justin ToblerSep 11, 2024
  39. Patrick SteinhardtSep 12, 2024
  40. 07/21 config: document `read_early_config()` and `read_very_early_config()`Patrick Steinhardt, Aug 30, 2024
  41. Justin ToblerSep 11, 2024
  42. Patrick SteinhardtSep 12, 2024
  43. 08/21 config: make dependency on repo in `read_early_config()` explicitPatrick Steinhardt, Aug 30, 2024
  44. James LiuSep 4, 2024
  45. Patrick SteinhardtSep 4, 2024
  46. 09/21 environment: move `odb_mkstemp()` into object layerPatrick Steinhardt, Aug 30, 2024
  47. karthik nayakSep 11, 2024
  48. Patrick SteinhardtSep 12, 2024
  49. 10/21 environment: make `get_git_namespace()` self-containedPatrick Steinhardt, Aug 30, 2024
  50. Justin ToblerSep 11, 2024
  51. 11/21 environment: move `set_git_dir()` and related into setup layerPatrick Steinhardt, Aug 30, 2024
  52. 12/21 environment: reorder header to split out `the_repository`-free sectionPatrick Steinhardt, Aug 30, 2024
  53. 13/21 environment: guard state depending on a repositoryPatrick Steinhardt, Aug 30, 2024
  54. 14/21 repo-settings: split out declarations into a standalone headerPatrick Steinhardt, Aug 30, 2024
  55. 15/21 repo-settings: track defaults close to `struct repo_settings`Patrick Steinhardt, Aug 30, 2024
  56. 16/21 branch: stop modifying `log_all_ref_updates` variablePatrick Steinhardt, Aug 30, 2024
  57. Justin ToblerSep 11, 2024
  58. Patrick SteinhardtSep 12, 2024
  59. 17/21 refs: stop modifying global `log_all_ref_updates` variablePatrick Steinhardt, Aug 30, 2024
  60. 18/21 environment: stop storing "core.logAllRefUpdates" globallyPatrick Steinhardt, Aug 30, 2024
  61. karthik nayakSep 12, 2024
  62. 19/21 environment: stop storing "core.preferSymlinkRefs" globallyPatrick Steinhardt, Aug 30, 2024
  63. 20/21 environment: stop storing "core.warnAmbiguousRefs" globallyPatrick Steinhardt, Aug 30, 2024
  64. James LiuSep 4, 2024
  65. 21/21 environment: stop storing "core.notesRef" globallyPatrick Steinhardt, Aug 30, 2024
  66. James LiuSep 4, 2024
  67. Patrick SteinhardtSep 4, 2024
  68. karthik nayakSep 12, 2024
  69. Patrick SteinhardtSep 12, 2024
  70. 00/21 environment: guard reliance on `the_repository`Patrick Steinhardt, Sep 12, 2024
  71. 01/21 environment: make `get_git_dir()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  72. 02/21 environment: make `get_git_common_dir()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  73. 03/21 environment: make `get_object_directory()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  74. 04/21 environment: make `get_index_file()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  75. 05/21 environment: make `get_graft_file()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  76. 06/21 environment: make `get_git_work_tree()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  77. 07/21 config: document `read_early_config()` and `read_very_early_config()`Patrick Steinhardt, Sep 12, 2024
  78. 08/21 config: make dependency on repo in `read_early_config()` explicitPatrick Steinhardt, Sep 12, 2024
  79. 09/21 environment: move object database functions into object layerPatrick Steinhardt, Sep 12, 2024
  80. 10/21 environment: make `get_git_namespace()` self-containedPatrick Steinhardt, Sep 12, 2024
  81. 11/21 environment: move `set_git_dir()` and related into setup layerPatrick Steinhardt, Sep 12, 2024
  82. 12/21 environment: reorder header to split out `the_repository`-free sectionPatrick Steinhardt, Sep 12, 2024
  83. 13/21 environment: guard state depending on a repositoryPatrick Steinhardt, Sep 12, 2024
  84. 14/21 repo-settings: split out declarations into a standalone headerPatrick Steinhardt, Sep 12, 2024
  85. 15/21 repo-settings: track defaults close to `struct repo_settings`Patrick Steinhardt, Sep 12, 2024
  86. 16/21 branch: stop modifying `log_all_ref_updates` variablePatrick Steinhardt, Sep 12, 2024
  87. 17/21 refs: stop modifying global `log_all_ref_updates` variablePatrick Steinhardt, Sep 12, 2024
  88. 18/21 environment: stop storing "core.logAllRefUpdates" globallyPatrick Steinhardt, Sep 12, 2024
  89. 19/21 environment: stop storing "core.preferSymlinkRefs" globallyPatrick Steinhardt, Sep 12, 2024
  90. 20/21 environment: stop storing "core.warnAmbiguousRefs" globallyPatrick Steinhardt, Sep 12, 2024
  91. 21/21 environment: stop storing "core.notesRef" globallyPatrick Steinhardt, Sep 12, 2024
  92. Junio C HamanoSep 12, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.