Skip to content

Commit 57e216d

Browse files
committed
Merge branch 'kn/rev-list-missing-fix'
"git rev-list --missing" did not work for missing commit objects, which has been corrected. * kn/rev-list-missing-fix: rev-list: add commit object support in `--missing` option rev-list: move `show_commit()` to the bottom revision: rename bit to `do_not_die_on_missing_objects`
2 parents fe84aa5 + 9830926 commit 57e216d

File tree

6 files changed

+155
-56
lines changed

6 files changed

+155
-56
lines changed

builtin/reflog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
298298
struct rev_info revs;
299299

300300
repo_init_revisions(the_repository, &revs, prefix);
301-
revs.do_not_die_on_missing_tree = 1;
301+
revs.do_not_die_on_missing_objects = 1;
302302
revs.ignore_missing = 1;
303303
revs.ignore_missing_links = 1;
304304
if (verbose)

builtin/rev-list.c

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,61 @@ static off_t get_object_disk_usage(struct object *obj)
100100
return size;
101101
}
102102

103-
static void finish_commit(struct commit *commit);
103+
static inline void finish_object__ma(struct object *obj)
104+
{
105+
/*
106+
* Whether or not we try to dynamically fetch missing objects
107+
* from the server, we currently DO NOT have the object. We
108+
* can either print, allow (ignore), or conditionally allow
109+
* (ignore) them.
110+
*/
111+
switch (arg_missing_action) {
112+
case MA_ERROR:
113+
die("missing %s object '%s'",
114+
type_name(obj->type), oid_to_hex(&obj->oid));
115+
return;
116+
117+
case MA_ALLOW_ANY:
118+
return;
119+
120+
case MA_PRINT:
121+
oidset_insert(&missing_objects, &obj->oid);
122+
return;
123+
124+
case MA_ALLOW_PROMISOR:
125+
if (is_promisor_object(&obj->oid))
126+
return;
127+
die("unexpected missing %s object '%s'",
128+
type_name(obj->type), oid_to_hex(&obj->oid));
129+
return;
130+
131+
default:
132+
BUG("unhandled missing_action");
133+
return;
134+
}
135+
}
136+
137+
static void finish_commit(struct commit *commit)
138+
{
139+
free_commit_list(commit->parents);
140+
commit->parents = NULL;
141+
free_commit_buffer(the_repository->parsed_objects,
142+
commit);
143+
}
144+
104145
static void show_commit(struct commit *commit, void *data)
105146
{
106147
struct rev_list_info *info = data;
107148
struct rev_info *revs = info->revs;
108149

109150
display_progress(progress, ++progress_counter);
110151

152+
if (revs->do_not_die_on_missing_objects &&
153+
oidset_contains(&revs->missing_commits, &commit->object.oid)) {
154+
finish_object__ma(&commit->object);
155+
return;
156+
}
157+
111158
if (show_disk_usage)
112159
total_disk_usage += get_object_disk_usage(&commit->object);
113160

@@ -219,48 +266,6 @@ static void show_commit(struct commit *commit, void *data)
219266
finish_commit(commit);
220267
}
221268

222-
static void finish_commit(struct commit *commit)
223-
{
224-
free_commit_list(commit->parents);
225-
commit->parents = NULL;
226-
free_commit_buffer(the_repository->parsed_objects,
227-
commit);
228-
}
229-
230-
static inline void finish_object__ma(struct object *obj)
231-
{
232-
/*
233-
* Whether or not we try to dynamically fetch missing objects
234-
* from the server, we currently DO NOT have the object. We
235-
* can either print, allow (ignore), or conditionally allow
236-
* (ignore) them.
237-
*/
238-
switch (arg_missing_action) {
239-
case MA_ERROR:
240-
die("missing %s object '%s'",
241-
type_name(obj->type), oid_to_hex(&obj->oid));
242-
return;
243-
244-
case MA_ALLOW_ANY:
245-
return;
246-
247-
case MA_PRINT:
248-
oidset_insert(&missing_objects, &obj->oid);
249-
return;
250-
251-
case MA_ALLOW_PROMISOR:
252-
if (is_promisor_object(&obj->oid))
253-
return;
254-
die("unexpected missing %s object '%s'",
255-
type_name(obj->type), oid_to_hex(&obj->oid));
256-
return;
257-
258-
default:
259-
BUG("unhandled missing_action");
260-
return;
261-
}
262-
}
263-
264269
static int finish_object(struct object *obj, const char *name UNUSED,
265270
void *cb_data)
266271
{
@@ -561,7 +566,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
561566
}
562567

563568
if (arg_missing_action)
564-
revs.do_not_die_on_missing_tree = 1;
569+
revs.do_not_die_on_missing_objects = 1;
565570

566571
argc = setup_revisions(argc, argv, &revs, &s_r_opt);
567572

list-objects.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static void process_tree(struct traversal_context *ctx,
177177
is_promisor_object(&obj->oid))
178178
return;
179179

180-
if (!revs->do_not_die_on_missing_tree)
180+
if (!revs->do_not_die_on_missing_objects)
181181
die("bad tree object %s", oid_to_hex(&obj->oid));
182182
}
183183

@@ -389,6 +389,9 @@ static void do_traverse(struct traversal_context *ctx)
389389
*/
390390
if (!ctx->revs->tree_objects)
391391
; /* do not bother loading tree */
392+
else if (ctx->revs->do_not_die_on_missing_objects &&
393+
oidset_contains(&ctx->revs->missing_commits, &commit->object.oid))
394+
;
392395
else if (repo_get_commit_tree(the_repository, commit)) {
393396
struct tree *tree = repo_get_commit_tree(the_repository,
394397
commit);

revision.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "object-name.h"
77
#include "object-file.h"
88
#include "object-store-ll.h"
9+
#include "oidset.h"
910
#include "tag.h"
1011
#include "blob.h"
1112
#include "tree.h"
@@ -1112,6 +1113,9 @@ static int process_parents(struct rev_info *revs, struct commit *commit,
11121113

11131114
if (commit->object.flags & ADDED)
11141115
return 0;
1116+
if (revs->do_not_die_on_missing_objects &&
1117+
oidset_contains(&revs->missing_commits, &commit->object.oid))
1118+
return 0;
11151119
commit->object.flags |= ADDED;
11161120

11171121
if (revs->include_check &&
@@ -1168,15 +1172,20 @@ static int process_parents(struct rev_info *revs, struct commit *commit,
11681172
for (parent = commit->parents; parent; parent = parent->next) {
11691173
struct commit *p = parent->item;
11701174
int gently = revs->ignore_missing_links ||
1171-
revs->exclude_promisor_objects;
1175+
revs->exclude_promisor_objects ||
1176+
revs->do_not_die_on_missing_objects;
11721177
if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
11731178
if (revs->exclude_promisor_objects &&
11741179
is_promisor_object(&p->object.oid)) {
11751180
if (revs->first_parent_only)
11761181
break;
11771182
continue;
11781183
}
1179-
return -1;
1184+
1185+
if (revs->do_not_die_on_missing_objects)
1186+
oidset_insert(&revs->missing_commits, &p->object.oid);
1187+
else
1188+
return -1; /* corrupt repository */
11801189
}
11811190
if (revs->sources) {
11821191
char **slot = revision_sources_at(revs->sources, p);
@@ -3109,6 +3118,7 @@ void release_revisions(struct rev_info *revs)
31093118
clear_decoration(&revs->merge_simplification, free);
31103119
clear_decoration(&revs->treesame, free);
31113120
line_log_free(revs);
3121+
oidset_clear(&revs->missing_commits);
31123122
}
31133123

31143124
static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
@@ -3800,6 +3810,8 @@ int prepare_revision_walk(struct rev_info *revs)
38003810
FOR_EACH_OBJECT_PROMISOR_ONLY);
38013811
}
38023812

3813+
oidset_init(&revs->missing_commits, 0);
3814+
38033815
if (!revs->reflog_info)
38043816
prepare_to_use_bloom_filter(revs);
38053817
if (!revs->unsorted_input)

revision.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "commit.h"
55
#include "grep.h"
66
#include "notes.h"
7+
#include "oidset.h"
78
#include "pretty.h"
89
#include "diff.h"
910
#include "commit-slab-decl.h"
@@ -212,18 +213,19 @@ struct rev_info {
212213

213214
/*
214215
* Blobs are shown without regard for their existence.
215-
* But not so for trees: unless exclude_promisor_objects
216+
* But not so for trees/commits: unless exclude_promisor_objects
216217
* is set and the tree in question is a promisor object;
217218
* OR ignore_missing_links is set, the revision walker
218-
* dies with a "bad tree object HASH" message when
219-
* encountering a missing tree. For callers that can
220-
* handle missing trees and want them to be filterable
219+
* dies with a "bad <type> object HASH" message when
220+
* encountering a missing object. For callers that can
221+
* handle missing trees/commits and want them to be filterable
221222
* and showable, set this to true. The revision walker
222-
* will filter and show such a missing tree as usual,
223-
* but will not attempt to recurse into this tree
224-
* object.
223+
* will filter and show such a missing object as usual,
224+
* but will not attempt to recurse into this tree/commit
225+
* object. The revision walker will also set the MISSING
226+
* flag for such objects.
225227
*/
226-
do_not_die_on_missing_tree:1,
228+
do_not_die_on_missing_objects:1,
227229

228230
/* for internal use only */
229231
exclude_promisor_objects:1;
@@ -372,6 +374,9 @@ struct rev_info {
372374

373375
/* Location where temporary objects for remerge-diff are written. */
374376
struct tmp_objdir *remerge_objdir;
377+
378+
/* Missing commits to be tracked without failing traversal. */
379+
struct oidset missing_commits;
375380
};
376381

377382
/**

t/t6022-rev-list-missing.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/sh
2+
3+
test_description='handling of missing objects in rev-list'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
6+
. ./test-lib.sh
7+
8+
# We setup the repository with two commits, this way HEAD is always
9+
# available and we can hide commit 1.
10+
test_expect_success 'create repository and alternate directory' '
11+
test_commit 1 &&
12+
test_commit 2 &&
13+
test_commit 3
14+
'
15+
16+
for obj in "HEAD~1" "HEAD~1^{tree}" "HEAD:1.t"
17+
do
18+
test_expect_success "rev-list --missing=error fails with missing object $obj" '
19+
oid="$(git rev-parse $obj)" &&
20+
path=".git/objects/$(test_oid_to_path $oid)" &&
21+
22+
mv "$path" "$path.hidden" &&
23+
test_when_finished "mv $path.hidden $path" &&
24+
25+
test_must_fail git rev-list --missing=error --objects \
26+
--no-object-names HEAD
27+
'
28+
done
29+
30+
for obj in "HEAD~1" "HEAD~1^{tree}" "HEAD:1.t"
31+
do
32+
for action in "allow-any" "print"
33+
do
34+
test_expect_success "rev-list --missing=$action with missing $obj" '
35+
oid="$(git rev-parse $obj)" &&
36+
path=".git/objects/$(test_oid_to_path $oid)" &&
37+
38+
# Before the object is made missing, we use rev-list to
39+
# get the expected oids.
40+
git rev-list --objects --no-object-names \
41+
HEAD ^$obj >expect.raw &&
42+
43+
# Blobs are shared by all commits, so evethough a commit/tree
44+
# might be skipped, its blob must be accounted for.
45+
if [ $obj != "HEAD:1.t" ]; then
46+
echo $(git rev-parse HEAD:1.t) >>expect.raw &&
47+
echo $(git rev-parse HEAD:2.t) >>expect.raw
48+
fi &&
49+
50+
mv "$path" "$path.hidden" &&
51+
test_when_finished "mv $path.hidden $path" &&
52+
53+
git rev-list --missing=$action --objects --no-object-names \
54+
HEAD >actual.raw &&
55+
56+
# When the action is to print, we should also add the missing
57+
# oid to the expect list.
58+
case $action in
59+
allow-any)
60+
;;
61+
print)
62+
grep ?$oid actual.raw &&
63+
echo ?$oid >>expect.raw
64+
;;
65+
esac &&
66+
67+
sort actual.raw >actual &&
68+
sort expect.raw >expect &&
69+
test_cmp expect actual
70+
'
71+
done
72+
done
73+
74+
test_done

0 commit comments

Comments
 (0)