builtin/prune: stop depending on 'the_repository'

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ayush Chandekar committed Jul 4, 2025 at 19:42 UTC 7cd03a555a0d951759a5bce201ad0686c0fc8b12
2 files changed +19 -15
builtin/prune.c
+12 -15
@@ -1,4 +1,3 @@
1 -#define USE_THE_REPOSITORY_VARIABLE
1 #define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "builtin.h"
@@ -64,7 +63,7 @@ static void perform_reachability_traversal(struct rev_info *revs)
63 return;
64
65 if (show_progress)
67 - progress = start_delayed_progress(the_repository,
66 + progress = start_delayed_progress(revs->repo,
67 _("Checking connectivity"), 0);
68 mark_reachable_objects(revs, 1, expire, progress);
69 stop_progress(&progress);
@@ -78,7 +77,7 @@ static int is_object_reachable(const struct object_id *oid,
77
78 perform_reachability_traversal(revs);
79
81 - obj = lookup_object(the_repository, oid);
80 + obj = lookup_object(revs->repo, oid);
81 return obj && (obj->flags & SEEN);
82 }
83
@@ -99,8 +98,7 @@ static int prune_object(const struct object_id *oid, const char *fullpath,
98 if (st.st_mtime > expire)
99 return 0;
100 if (show_only || verbose) {
102 - enum object_type type = oid_object_info(the_repository, oid,
103 - NULL);
101 + enum object_type type = oid_object_info(revs->repo, oid, NULL);
102 printf("%s %s\n", oid_to_hex(oid),
103 (type > 0) ? type_name(type) : "unknown");
104 }
@@ -154,7 +152,7 @@ static void remove_temporary_files(const char *path)
152 int cmd_prune(int argc,
153 const char **argv,
154 const char *prefix,
157 - struct repository *repo UNUSED)
155 + struct repository *repo)
156 {
157 struct rev_info revs;
158 int exclude_promisor_objects = 0;
@@ -173,20 +171,19 @@ int cmd_prune(int argc,
171 expire = TIME_MAX;
172 save_commit_buffer = 0;
173 disable_replace_refs();
176 - repo_init_revisions(the_repository, &revs, prefix);
174
175 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
176
180 - if (the_repository->repository_format_precious_objects)
177 + repo_init_revisions(repo, &revs, prefix);
178 + if (repo->repository_format_precious_objects)
179 die(_("cannot prune in a precious-objects repo"));
180
181 while (argc--) {
182 struct object_id oid;
183 const char *name = *argv++;
184
187 - if (!repo_get_oid(the_repository, name, &oid)) {
188 - struct object *object = parse_object_or_die(the_repository, &oid,
189 - name);
185 + if (!repo_get_oid(repo, name, &oid)) {
186 + struct object *object = parse_object_or_die(repo, &oid, name);
187 add_pending_object(&revs, object, "");
188 }
189 else
@@ -200,16 +197,16 @@ int cmd_prune(int argc,
197 revs.exclude_promisor_objects = 1;
198 }
199
203 - for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
200 + for_each_loose_file_in_objdir(repo_get_object_directory(repo),
201 prune_object, prune_cruft, prune_subdir, &revs);
202
203 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
207 - remove_temporary_files(repo_get_object_directory(the_repository));
208 - s = mkpathdup("%s/pack", repo_get_object_directory(the_repository));
204 + remove_temporary_files(repo_get_object_directory(repo));
205 + s = mkpathdup("%s/pack", repo_get_object_directory(repo));
206 remove_temporary_files(s);
207 free(s);
208
212 - if (is_repository_shallow(the_repository)) {
209 + if (is_repository_shallow(repo)) {
210 perform_reachability_traversal(&revs);
211 prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
212 }
t/t1517-outside-repo.sh
+7
@@ -114,4 +114,11 @@ test_expect_success 'update-server-info does not crash with -h' '
114 test_grep "[Uu]sage: git update-server-info " usage
115 '
116
117 +test_expect_success 'prune does not crash with -h' '
118 + test_expect_code 129 git prune -h >usage &&
119 + test_grep "[Uu]sage: git prune " usage &&
120 + test_expect_code 129 nongit git prune -h >usage &&
121 + test_grep "[Uu]sage: git prune " usage
122 +'
123 +
124 test_done