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

[GSOC PATCH v3 2/2] builtin/prune: stop depending on 'the_repository'

From
Ayush Chandekar <ayu.chandekar@gmail.com>
Date
Jul 4, 2025, 14:12 UTC
Message-ID
<22fbbc8cf1b5cd622197e6d9f009acdbbcc0e802.1751630981.git.ayu.chandekar@gmail.com>
In-Reply-To
<cover.1751630981.git.ayu.chandekar@gmail.com>

Refactor builtin/prune.c to remove the dependency on the global 'the_repository'. Replace all the occurrences of 'the_repository' with repo and thus remove the definition '#define USE_THE_REPOSITORY_VARIABLE'. Also, add a test to make sure that 'git prune -h' can be called when the repository is `NULL`.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Ghanshyam Thakkar <shyamthakkar001@gmail.com>
Signed-off-by: Ayush Chandekar <ayu.chandekar@gmail.com>
---
 builtin/prune.c         | 27 ++++++++++++---------------
 t/t1517-outside-repo.sh |  7 +++++++
 2 files changed, 19 insertions(+), 15 deletions(-)
diff --git a/builtin/prune.c b/builtin/prune.c
index dab3c19b6f..320e9c2341 100644
--- a/builtin/prune.c
+++ b/builtin/prune.c
@@ -1,4 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
 #define DISABLE_SIGN_COMPARE_WARNINGS
 
 #include "builtin.h"
@@ -64,7 +63,7 @@ static void perform_reachability_traversal(struct rev_info *revs)
 		return;
 
 	if (show_progress)
-		progress = start_delayed_progress(the_repository,
+		progress = start_delayed_progress(revs->repo,
 						  _("Checking connectivity"), 0);
 	mark_reachable_objects(revs, 1, expire, progress);
 	stop_progress(&progress);
@@ -78,7 +77,7 @@ static int is_object_reachable(const struct object_id *oid,
 
 	perform_reachability_traversal(revs);
 
-	obj = lookup_object(the_repository, oid);
+	obj = lookup_object(revs->repo, oid);
 	return obj && (obj->flags & SEEN);
 }
 
@@ -99,8 +98,7 @@ static int prune_object(const struct object_id *oid, const char *fullpath,
 	if (st.st_mtime > expire)
 		return 0;
 	if (show_only || verbose) {
-		enum object_type type = oid_object_info(the_repository, oid,
-							NULL);
+		enum object_type type = oid_object_info(revs->repo, oid, NULL);
 		printf("%s %s\n", oid_to_hex(oid),
 		       (type > 0) ? type_name(type) : "unknown");
 	}
@@ -154,7 +152,7 @@ static void remove_temporary_files(const char *path)
 int cmd_prune(int argc,
 	      const char **argv,
 	      const char *prefix,
-	      struct repository *repo UNUSED)
+	      struct repository *repo)
 {
 	struct rev_info revs;
 	int exclude_promisor_objects = 0;
@@ -173,20 +171,19 @@ int cmd_prune(int argc,
 	expire = TIME_MAX;
 	save_commit_buffer = 0;
 	disable_replace_refs();
-	repo_init_revisions(the_repository, &revs, prefix);
 
 	argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
 
-	if (the_repository->repository_format_precious_objects)
+	repo_init_revisions(repo, &revs, prefix);
+	if (repo->repository_format_precious_objects)
 		die(_("cannot prune in a precious-objects repo"));
 
 	while (argc--) {
 		struct object_id oid;
 		const char *name = *argv++;
 
-		if (!repo_get_oid(the_repository, name, &oid)) {
-			struct object *object = parse_object_or_die(the_repository, &oid,
-								    name);
+		if (!repo_get_oid(repo, name, &oid)) {
+			struct object *object = parse_object_or_die(repo, &oid, name);
 			add_pending_object(&revs, object, "");
 		}
 		else
@@ -200,16 +197,16 @@ int cmd_prune(int argc,
 		revs.exclude_promisor_objects = 1;
 	}
 
-	for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
+	for_each_loose_file_in_objdir(repo_get_object_directory(repo),
 				      prune_object, prune_cruft, prune_subdir, &revs);
 
 	prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
-	remove_temporary_files(repo_get_object_directory(the_repository));
-	s = mkpathdup("%s/pack", repo_get_object_directory(the_repository));
+	remove_temporary_files(repo_get_object_directory(repo));
+	s = mkpathdup("%s/pack", repo_get_object_directory(repo));
 	remove_temporary_files(s);
 	free(s);
 
-	if (is_repository_shallow(the_repository)) {
+	if (is_repository_shallow(repo)) {
 		perform_reachability_traversal(&revs);
 		prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
 	}
diff --git a/t/t1517-outside-repo.sh b/t/t1517-outside-repo.sh
index 6824581317..8f59b867f2 100755
--- a/t/t1517-outside-repo.sh
+++ b/t/t1517-outside-repo.sh
@@ -114,4 +114,11 @@ test_expect_success 'update-server-info does not crash with -h' '
 	test_grep "[Uu]sage: git update-server-info " usage
 '
 
+test_expect_success 'prune does not crash with -h' '
+	test_expect_code 129 git prune -h >usage &&
+	test_grep "[Uu]sage: git prune " usage &&
+	test_expect_code 129 nongit git prune -h >usage &&
+	test_grep "[Uu]sage: git prune " usage
+'
+
 test_done
-- 
2.49.0
Previous: Ayush ChandekarNext: Patrick Steinhardt
Message 27 of 29 in “builtin/prune: remove dependency on global variables and 'the_repository'”
  1. 0/2 builtin/prune: remove dependency on global variables and 'the_repository'Ayush Chandekar, Jun 8, 2025
  2. 1/2 repository: move 'repository_format_precious_objects' to repo scopeAyush Chandekar, Jun 8, 2025
  3. shejialuoJun 28, 2025
  4. Ayush ChandekarJun 28, 2025
  5. 2/2 builtin/prune: stop depending on 'the_repository'Ayush Chandekar, Jun 8, 2025
  6. shejialuoJun 28, 2025
  7. Ayush ChandekarJun 28, 2025
  8. Ayush ChandekarJun 25, 2025
  9. 0/2 builtin/prune: remove dependency on global variables and 'the_repository'Ayush Chandekar, Jun 30, 2025
  10. 1/2 repository: move 'repository_format_precious_objects' to repo scopeAyush Chandekar, Jun 30, 2025
  11. Patrick SteinhardtJul 1, 2025
  12. Ayush ChandekarJul 1, 2025
  13. Patrick SteinhardtJul 2, 2025
  14. 2/2 builtin/prune: stop depending on 'the_repository'Ayush Chandekar, Jun 30, 2025
  15. Patrick SteinhardtJul 1, 2025
  16. Junio C HamanoJul 1, 2025
  17. Ayush ChandekarJul 1, 2025
  18. Usman AkinyemiJul 1, 2025
  19. Ayush ChandekarJul 1, 2025
  20. Patrick SteinhardtJul 2, 2025
  21. Usman AkinyemiJul 2, 2025
  22. Ben KnobleJul 2, 2025
  23. Junio C HamanoJul 2, 2025
  24. Ayush ChandekarJul 2, 2025
  25. 0/2 builtin/prune: remove dependency on global variables and 'the_repository'Ayush Chandekar, Jul 4, 2025
  26. 1/2 repository: move 'repository_format_precious_objects' to repo scopeAyush Chandekar, Jul 4, 2025
  27. 2/2 builtin/prune: stop depending on 'the_repository'Ayush Chandekar, Jul 4, 2025
  28. Patrick SteinhardtJul 7, 2025
  29. Ayush ChandekarJul 8, 2025

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.