Skip to content

Commit 7c5c9b9

Browse files
szedergitster
authored andcommitted
commit-graph: error out on invalid commit oids in 'write --stdin-commits'
While 'git commit-graph write --stdin-commits' expects commit object ids as input, it accepts and silently skips over any invalid commit object ids, and still exits with success: # nonsense $ echo not-a-commit-oid | git commit-graph write --stdin-commits $ echo $? 0 # sometimes I forgot that refs are not good... $ echo HEAD | git commit-graph write --stdin-commits $ echo $? 0 # valid tree OID, but not a commit OID $ git rev-parse HEAD^{tree} | git commit-graph write --stdin-commits $ echo $? 0 $ ls -l .git/objects/info/commit-graph ls: cannot access '.git/objects/info/commit-graph': No such file or directory Check that all input records are indeed valid commit object ids and return with error otherwise, the same way '--stdin-packs' handles invalid input; see e103f72 (commit-graph: return with errors during write, 2019-06-12). Note that it should only return with error when encountering an invalid commit object id coming from standard input. However, '--reachable' uses the same code path to process object ids pointed to by all refs, and that includes tag object ids as well, which should still be skipped over. Therefore add a new flag to 'enum commit_graph_write_flags' and a corresponding field to 'struct write_commit_graph_context', so we can differentiate between those two cases. Signed-off-by: SZEDER Gábor <[email protected]> Acked-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 39d8831 commit 7c5c9b9

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

builtin/commit-graph.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,10 @@ static int graph_write(int argc, const char **argv)
213213

214214
if (opts.stdin_packs)
215215
pack_indexes = &lines;
216-
if (opts.stdin_commits)
216+
if (opts.stdin_commits) {
217217
commit_hex = &lines;
218+
flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
219+
}
218220

219221
UNLEAK(buf);
220222
}

commit-graph.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,8 @@ struct write_commit_graph_context {
782782

783783
unsigned append:1,
784784
report_progress:1,
785-
split:1;
785+
split:1,
786+
check_oids:1;
786787

787788
const struct split_commit_graph_opts *split_opts;
788789
};
@@ -1193,8 +1194,8 @@ static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
11931194
return 0;
11941195
}
11951196

1196-
static void fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1197-
struct string_list *commit_hex)
1197+
static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1198+
struct string_list *commit_hex)
11981199
{
11991200
uint32_t i;
12001201
struct strbuf progress_title = STRBUF_INIT;
@@ -1215,20 +1216,21 @@ static void fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
12151216
struct commit *result;
12161217

12171218
display_progress(ctx->progress, i + 1);
1218-
if (commit_hex->items[i].string &&
1219-
parse_oid_hex(commit_hex->items[i].string, &oid, &end))
1220-
continue;
1221-
1222-
result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1223-
1224-
if (result) {
1219+
if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
1220+
(result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
12251221
ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
12261222
oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
12271223
ctx->oids.nr++;
1224+
} else if (ctx->check_oids) {
1225+
error(_("invalid commit object id: %s"),
1226+
commit_hex->items[i].string);
1227+
return -1;
12281228
}
12291229
}
12301230
stop_progress(&ctx->progress);
12311231
strbuf_release(&progress_title);
1232+
1233+
return 0;
12321234
}
12331235

12341236
static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
@@ -1775,6 +1777,7 @@ int write_commit_graph(const char *obj_dir,
17751777
ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
17761778
ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
17771779
ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
1780+
ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
17781781
ctx->split_opts = split_opts;
17791782

17801783
if (ctx->split) {
@@ -1829,8 +1832,10 @@ int write_commit_graph(const char *obj_dir,
18291832
goto cleanup;
18301833
}
18311834

1832-
if (commit_hex)
1833-
fill_oids_from_commit_hex(ctx, commit_hex);
1835+
if (commit_hex) {
1836+
if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
1837+
goto cleanup;
1838+
}
18341839

18351840
if (!pack_indexes && !commit_hex)
18361841
fill_oids_from_all_packs(ctx);

commit-graph.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ int generation_numbers_enabled(struct repository *r);
7474
enum commit_graph_write_flags {
7575
COMMIT_GRAPH_WRITE_APPEND = (1 << 0),
7676
COMMIT_GRAPH_WRITE_PROGRESS = (1 << 1),
77-
COMMIT_GRAPH_WRITE_SPLIT = (1 << 2)
77+
COMMIT_GRAPH_WRITE_SPLIT = (1 << 2),
78+
/* Make sure that each OID in the input is a valid commit OID. */
79+
COMMIT_GRAPH_WRITE_CHECK_OIDS = (1 << 3)
7880
};
7981

8082
struct split_commit_graph_opts {

t/t5318-commit-graph.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test_expect_success 'write graph with no packs' '
2323
test_path_is_missing info/commit-graph
2424
'
2525

26-
test_expect_success 'close with correct error on bad input' '
26+
test_expect_success 'exit with correct error on bad input to --stdin-packs' '
2727
cd "$TRASH_DIRECTORY/full" &&
2828
echo doesnotexist >in &&
2929
test_expect_code 1 git commit-graph write --stdin-packs <in 2>stderr &&
@@ -40,6 +40,15 @@ test_expect_success 'create commits and repack' '
4040
git repack
4141
'
4242

43+
test_expect_success 'exit with correct error on bad input to --stdin-commits' '
44+
cd "$TRASH_DIRECTORY/full" &&
45+
echo HEAD | test_expect_code 1 git commit-graph write --stdin-commits 2>stderr &&
46+
test_i18ngrep "invalid commit object id" stderr &&
47+
# valid tree OID, but not a commit OID
48+
git rev-parse HEAD^{tree} | test_expect_code 1 git commit-graph write --stdin-commits 2>stderr &&
49+
test_i18ngrep "invalid commit object id" stderr
50+
'
51+
4352
graph_git_two_modes() {
4453
git -c core.commitGraph=true $1 >output
4554
git -c core.commitGraph=false $1 >expect

0 commit comments

Comments
 (0)