Skip to content

Commit 7a9ce02

Browse files
ttaylorrgitster
authored andcommitted
commit-graph.c: introduce '--[no-]check-oids'
When operating on a stream of commit OIDs on stdin, 'git commit-graph write' checks that each OID refers to an object that is indeed a commit. This is convenient to make sure that the given input is well-formed, but can sometimes be undesirable. For example, server operators may wish to feed the refnames that were updated during a push to 'git commit-graph write --input=stdin-commits', and silently discard refs that don't point at commits. This can be done by combing the output of 'git for-each-ref' with '--format %(*objecttype)', but this requires opening up a potentially large number of objects. Instead, it is more convenient to feed the updated refs to the commit-graph machinery, and let it throw out refs that don't point to commits. Introduce '--[no-]check-oids' to make such a behavior possible. With '--check-oids' (the default behavior to retain backwards compatibility), 'git commit-graph write' will barf on a non-commit line in its input. With 'no-check-oids', such lines will be silently ignored, making the above possible by specifying this option. No matter which is supplied, 'git commit-graph write' retains the behavior from the previous commit of rejecting non-OID inputs like "HEAD" and "refs/heads/foo" as before. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6830c36 commit 7a9ce02

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

Documentation/git-commit-graph.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ tip with the previous tip.
8282
Finally, if `--expire-time=<datetime>` is not specified, let `datetime`
8383
be the current time. After writing the split commit-graph, delete all
8484
unused commit-graph whose modified times are older than `datetime`.
85+
+
86+
The `--[no-]check-oids` option decides whether or not OIDs are required
87+
to be commits. By default, `--check-oids` is implied, generating an
88+
error on non-commit objects. If `--no-check-oids` is given, non-commits
89+
are silently discarded.
8590

8691
'verify'::
8792

builtin/commit-graph.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static char const * const builtin_commit_graph_usage[] = {
1111
N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
1212
N_("git commit-graph write [--object-dir <objdir>] [--append] "
1313
"[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
14-
"[--[no-]progress] <split options>"),
14+
"[--[no-]progress] [--[no-]check-oids] <split options>"),
1515
NULL
1616
};
1717

@@ -23,7 +23,7 @@ static const char * const builtin_commit_graph_verify_usage[] = {
2323
static const char * const builtin_commit_graph_write_usage[] = {
2424
N_("git commit-graph write [--object-dir <objdir>] [--append] "
2525
"[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
26-
"[--[no-]progress] <split options>"),
26+
"[--[no-]progress] [--[no-]check-oids] <split options>"),
2727
NULL
2828
};
2929

@@ -36,6 +36,7 @@ static struct opts_commit_graph {
3636
int split;
3737
int shallow;
3838
int progress;
39+
int check_oids;
3940
} opts;
4041

4142
static struct object_directory *find_odb(struct repository *r,
@@ -160,6 +161,8 @@ static int graph_write(int argc, const char **argv)
160161
N_("allow writing an incremental commit-graph file"),
161162
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
162163
write_option_parse_split),
164+
OPT_BOOL(0, "check-oids", &opts.check_oids,
165+
N_("require OIDs to be commits")),
163166
OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
164167
N_("maximum number of commits in a non-base split commit-graph")),
165168
OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
@@ -170,6 +173,7 @@ static int graph_write(int argc, const char **argv)
170173
};
171174

172175
opts.progress = isatty(2);
176+
opts.check_oids = 1;
173177
split_opts.size_multiple = 2;
174178
split_opts.max_commits = 0;
175179
split_opts.expire_time = 0;
@@ -224,7 +228,8 @@ static int graph_write(int argc, const char **argv)
224228

225229
oidset_insert(&commits, &oid);
226230
}
227-
flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
231+
if (opts.check_oids)
232+
flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
228233
}
229234

230235
UNLEAK(buf);

commit-graph.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static int verify_commit_graph_lite(struct commit_graph *g)
145145
*
146146
* There should only be very basic checks here to ensure that
147147
* we don't e.g. segfault in fill_commit_in_graph(), but
148-
* because this is a very hot codepath nothing that e.g. loops
148+
e because this is a very hot codepath nothing that e.g. loops
149149
* over g->num_commits, or runs a checksum on the commit-graph
150150
* itself.
151151
*/

t/t5318-commit-graph.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ test_expect_success 'exit with correct error on bad input to --stdin-commits' '
4949
test_i18ngrep "invalid commit object id" stderr
5050
'
5151

52+
graph_expect_commits() {
53+
test-tool read-graph >got
54+
if ! grep "num_commits: $1" got
55+
then
56+
echo "graph_expect_commits: expected $1 commit(s), got:"
57+
cat got
58+
false
59+
fi
60+
}
61+
62+
test_expect_success 'ignores non-commit OIDs to --input=stdin-commits with --no-check-oids' '
63+
test_when_finished rm -rf "$objdir/info/commit-graph" &&
64+
cd "$TRASH_DIRECTORY/full" &&
65+
# write a graph to ensure layers are/are not added appropriately
66+
git rev-parse HEAD~1 >base &&
67+
git commit-graph write --stdin-commits <base &&
68+
graph_expect_commits 2 &&
69+
# bad input is rejected
70+
echo HEAD >bad &&
71+
test_expect_code 1 git commit-graph write --stdin-commits <bad 2>err &&
72+
test_i18ngrep "unexpected non-hex object ID: HEAD" err &&
73+
graph_expect_commits 2 &&
74+
# update with valid commit OID, ignore tree OID
75+
git rev-parse HEAD HEAD^{tree} >in &&
76+
git commit-graph write --stdin-commits --no-check-oids <in &&
77+
graph_expect_commits 3
78+
'
79+
5280
graph_git_two_modes() {
5381
git -c core.commitGraph=true $1 >output
5482
git -c core.commitGraph=false $1 >expect

0 commit comments

Comments
 (0)