Skip to content

Commit 7cd03a5

Browse files
ayu-chgitster
authored andcommitted
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 <[email protected]> Mentored-by: Ghanshyam Thakkar <[email protected]> Signed-off-by: Ayush Chandekar <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 44e300a commit 7cd03a5

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

builtin/prune.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define USE_THE_REPOSITORY_VARIABLE
21
#define DISABLE_SIGN_COMPARE_WARNINGS
32

43
#include "builtin.h"
@@ -64,7 +63,7 @@ static void perform_reachability_traversal(struct rev_info *revs)
6463
return;
6564

6665
if (show_progress)
67-
progress = start_delayed_progress(the_repository,
66+
progress = start_delayed_progress(revs->repo,
6867
_("Checking connectivity"), 0);
6968
mark_reachable_objects(revs, 1, expire, progress);
7069
stop_progress(&progress);
@@ -78,7 +77,7 @@ static int is_object_reachable(const struct object_id *oid,
7877

7978
perform_reachability_traversal(revs);
8079

81-
obj = lookup_object(the_repository, oid);
80+
obj = lookup_object(revs->repo, oid);
8281
return obj && (obj->flags & SEEN);
8382
}
8483

@@ -99,8 +98,7 @@ static int prune_object(const struct object_id *oid, const char *fullpath,
9998
if (st.st_mtime > expire)
10099
return 0;
101100
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);
104102
printf("%s %s\n", oid_to_hex(oid),
105103
(type > 0) ? type_name(type) : "unknown");
106104
}
@@ -154,7 +152,7 @@ static void remove_temporary_files(const char *path)
154152
int cmd_prune(int argc,
155153
const char **argv,
156154
const char *prefix,
157-
struct repository *repo UNUSED)
155+
struct repository *repo)
158156
{
159157
struct rev_info revs;
160158
int exclude_promisor_objects = 0;
@@ -173,20 +171,19 @@ int cmd_prune(int argc,
173171
expire = TIME_MAX;
174172
save_commit_buffer = 0;
175173
disable_replace_refs();
176-
repo_init_revisions(the_repository, &revs, prefix);
177174

178175
argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
179176

180-
if (the_repository->repository_format_precious_objects)
177+
repo_init_revisions(repo, &revs, prefix);
178+
if (repo->repository_format_precious_objects)
181179
die(_("cannot prune in a precious-objects repo"));
182180

183181
while (argc--) {
184182
struct object_id oid;
185183
const char *name = *argv++;
186184

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);
190187
add_pending_object(&revs, object, "");
191188
}
192189
else
@@ -200,16 +197,16 @@ int cmd_prune(int argc,
200197
revs.exclude_promisor_objects = 1;
201198
}
202199

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),
204201
prune_object, prune_cruft, prune_subdir, &revs);
205202

206203
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));
209206
remove_temporary_files(s);
210207
free(s);
211208

212-
if (is_repository_shallow(the_repository)) {
209+
if (is_repository_shallow(repo)) {
213210
perform_reachability_traversal(&revs);
214211
prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
215212
}

t/t1517-outside-repo.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,11 @@ test_expect_success 'update-server-info does not crash with -h' '
114114
test_grep "[Uu]sage: git update-server-info " usage
115115
'
116116

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+
117124
test_done

0 commit comments

Comments
 (0)