Skip to content

Commit 9830926

Browse files
KarthikNayakgitster
authored andcommitted
rev-list: add commit object support in --missing option
The `--missing` object option in rev-list currently works only with missing blobs/trees. For missing commits the revision walker fails with a fatal error. Let's extend the functionality of `--missing` option to also support commit objects. This is done by adding a `missing_objects` field to `rev_info`. This field is an `oidset` to which we'll add the missing commits as we encounter them. The revision walker will now continue the traversal and call `show_commit()` even for missing commits. In rev-list we can then check if the commit is a missing commit and call the existing code for parsing `--missing` objects. A scenario where this option would be used is to find the boundary objects between different object directories. Consider a repository with a main object directory (GIT_OBJECT_DIRECTORY) and one or more alternate object directories (GIT_ALTERNATE_OBJECT_DIRECTORIES). In such a repository, using the `--missing=print` option while disabling the alternate object directory allows us to find the boundary objects between the main and alternate object directory. Helped-by: Patrick Steinhardt <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b495292 commit 9830926

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

builtin/rev-list.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ static void show_commit(struct commit *commit, void *data)
149149

150150
display_progress(progress, ++progress_counter);
151151

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+
152158
if (show_disk_usage)
153159
total_disk_usage += get_object_disk_usage(&commit->object);
154160

list-objects.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 4 additions & 0 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"
@@ -373,6 +374,9 @@ struct rev_info {
373374

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

378382
/**

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)