@matheusbsbs / matheus-bagliano / commits / 7a9ce0269b
Fork 0

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

Taylor Blau committed Apr 14, 2020 at 22:31 UTC 7a9ce0269bc0f4ef230f930b3910b70ac3142552
4 files changed +42 -4
Documentation/git-commit-graph.txt
+5
@@ -82,6 +82,11 @@ tip with the previous tip.
82 Finally, if `--expire-time=<datetime>` is not specified, let `datetime`
83 be the current time. After writing the split commit-graph, delete all
84 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.
90
91 'verify'::
92
builtin/commit-graph.c
+8 -3
@@ -11,7 +11,7 @@ static char const * const builtin_commit_graph_usage[] = {
11 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
12 N_("git commit-graph write [--object-dir <objdir>] [--append] "
13 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
14 - "[--[no-]progress] <split options>"),
14 + "[--[no-]progress] [--[no-]check-oids] <split options>"),
15 NULL
16 };
17
@@ -23,7 +23,7 @@ static const char * const builtin_commit_graph_verify_usage[] = {
23 static const char * const builtin_commit_graph_write_usage[] = {
24 N_("git commit-graph write [--object-dir <objdir>] [--append] "
25 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
26 - "[--[no-]progress] <split options>"),
26 + "[--[no-]progress] [--[no-]check-oids] <split options>"),
27 NULL
28 };
29
@@ -36,6 +36,7 @@ static struct opts_commit_graph {
36 int split;
37 int shallow;
38 int progress;
39 + int check_oids;
40 } opts;
41
42 static struct object_directory *find_odb(struct repository *r,
@@ -160,6 +161,8 @@ static int graph_write(int argc, const char **argv)
161 N_("allow writing an incremental commit-graph file"),
162 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
163 write_option_parse_split),
164 + OPT_BOOL(0, "check-oids", &opts.check_oids,
165 + N_("require OIDs to be commits")),
166 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
167 N_("maximum number of commits in a non-base split commit-graph")),
168 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
@@ -170,6 +173,7 @@ static int graph_write(int argc, const char **argv)
173 };
174
175 opts.progress = isatty(2);
176 + opts.check_oids = 1;
177 split_opts.size_multiple = 2;
178 split_opts.max_commits = 0;
179 split_opts.expire_time = 0;
@@ -224,7 +228,8 @@ static int graph_write(int argc, const char **argv)
228
229 oidset_insert(&commits, &oid);
230 }
227 - flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
231 + if (opts.check_oids)
232 + flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
233 }
234
235 UNLEAK(buf);
commit-graph.c
+1 -1
@@ -145,7 +145,7 @@ static int verify_commit_graph_lite(struct commit_graph *g)
145 *
146 * There should only be very basic checks here to ensure that
147 * 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
149 * over g->num_commits, or runs a checksum on the commit-graph
150 * itself.
151 */
t/t5318-commit-graph.sh
+28
@@ -49,6 +49,34 @@ test_expect_success 'exit with correct error on bad input to --stdin-commits' '
49 test_i18ngrep "invalid commit object id" stderr
50 '
51
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 +
80 graph_git_two_modes() {
81 git -c core.commitGraph=true $1 >output
82 git -c core.commitGraph=false $1 >expect