Skip to content

Commit 5bb6205

Browse files
committed
Merge branch 'jk/robustify-parse-commit'
* jk/robustify-parse-commit: checkout: do not die when leaving broken detached HEAD use parse_commit_or_die instead of custom message use parse_commit_or_die instead of segfaulting assume parse_commit checks for NULL commit assume parse_commit checks commit->object.parsed log_tree_diff: die when we fail to parse a commit
2 parents b2a0afd + 3c62183 commit 5bb6205

15 files changed

+32
-33
lines changed

builtin/blame.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,8 +1551,7 @@ static void assign_blame(struct scoreboard *sb, int opt)
15511551
*/
15521552
origin_incref(suspect);
15531553
commit = suspect->commit;
1554-
if (!commit->object.parsed)
1555-
parse_commit(commit);
1554+
parse_commit(commit);
15561555
if (reverse ||
15571556
(!(commit->object.flags & UNINTERESTING) &&
15581557
!(revs->max_age != -1 && commit->date < revs->max_age)))

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ static void add_verbose_info(struct strbuf *out, struct ref_item *item,
502502
const char *sub = _(" **** invalid ref ****");
503503
struct commit *commit = item->commit;
504504

505-
if (commit && !parse_commit(commit)) {
505+
if (!parse_commit(commit)) {
506506
pp_commit_easy(CMIT_FMT_ONELINE, commit, &subject);
507507
sub = subject.buf;
508508
}

builtin/checkout.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ static void show_local_changes(struct object *head,
380380
static void describe_detached_head(const char *msg, struct commit *commit)
381381
{
382382
struct strbuf sb = STRBUF_INIT;
383-
parse_commit(commit);
384-
pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb);
383+
if (!parse_commit(commit))
384+
pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb);
385385
fprintf(stderr, "%s %s... %s\n", msg,
386386
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
387387
strbuf_release(&sb);
@@ -677,12 +677,12 @@ static int add_pending_uninteresting_ref(const char *refname,
677677

678678
static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
679679
{
680-
parse_commit(commit);
681680
strbuf_addstr(sb, " ");
682681
strbuf_addstr(sb,
683682
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
684683
strbuf_addch(sb, ' ');
685-
pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
684+
if (!parse_commit(commit))
685+
pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
686686
strbuf_addch(sb, '\n');
687687
}
688688

@@ -789,7 +789,7 @@ static int switch_branches(const struct checkout_opts *opts,
789789
new->commit = old.commit;
790790
if (!new->commit)
791791
die(_("You are on a branch yet to be born"));
792-
parse_commit(new->commit);
792+
parse_commit_or_die(new->commit);
793793
}
794794

795795
ret = merge_working_tree(opts, &old, new, &writeout_error);
@@ -995,7 +995,7 @@ static int parse_branchname_arg(int argc, const char **argv,
995995
/* not a commit */
996996
*source_tree = parse_tree_indirect(rev);
997997
} else {
998-
parse_commit(new->commit);
998+
parse_commit_or_die(new->commit);
999999
*source_tree = new->commit->tree;
10001000
}
10011001

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
13381338
commit = lookup_commit(sha1);
13391339
if (!commit)
13401340
die(_("couldn't look up newly created commit"));
1341-
if (!commit || parse_commit(commit))
1341+
if (parse_commit(commit))
13421342
die(_("could not parse newly created commit"));
13431343

13441344
strbuf_addstr(&format, "format:%h] %s");
@@ -1525,7 +1525,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15251525
current_head = NULL;
15261526
else {
15271527
current_head = lookup_commit_or_die(sha1, "HEAD");
1528-
if (!current_head || parse_commit(current_head))
1528+
if (parse_commit(current_head))
15291529
die(_("could not parse HEAD commit"));
15301530
}
15311531
argc = parse_and_validate_options(argc, argv, builtin_commit_options,

builtin/fast-export.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev)
287287

288288
rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
289289

290-
parse_commit(commit);
290+
parse_commit_or_die(commit);
291291
author = strstr(commit->buffer, "\nauthor ");
292292
if (!author)
293293
die ("Could not find author in commit %s",
@@ -308,7 +308,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev)
308308
if (commit->parents &&
309309
get_object_mark(&commit->parents->item->object) != 0 &&
310310
!full_tree) {
311-
parse_commit(commit->parents->item);
311+
parse_commit_or_die(commit->parents->item);
312312
diff_tree_sha1(commit->parents->item->tree->object.sha1,
313313
commit->tree->object.sha1, "", &rev->diffopt);
314314
}

builtin/name-rev.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ static void name_rev(struct commit *commit,
2727
struct commit_list *parents;
2828
int parent_number = 1;
2929

30-
if (!commit->object.parsed)
31-
parse_commit(commit);
30+
parse_commit(commit);
3231

3332
if (commit->date < cutoff)
3433
return;

builtin/show-branch.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ static void join_revs(struct commit_list **list_p,
227227
parents = parents->next;
228228
if ((this_flag & flags) == flags)
229229
continue;
230-
if (!p->object.parsed)
231-
parse_commit(p);
230+
parse_commit(p);
232231
if (mark_seen(p, seen_p) && !still_interesting)
233232
extra--;
234233
p->object.flags |= flags;

commit.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct commit *lookup_commit_reference_by_name(const char *name)
7979
if (get_sha1_committish(name, sha1))
8080
return NULL;
8181
commit = lookup_commit_reference(sha1);
82-
if (!commit || parse_commit(commit))
82+
if (parse_commit(commit))
8383
return NULL;
8484
return commit;
8585
}
@@ -341,6 +341,13 @@ int parse_commit(struct commit *item)
341341
return ret;
342342
}
343343

344+
void parse_commit_or_die(struct commit *item)
345+
{
346+
if (parse_commit(item))
347+
die("unable to parse commit %s",
348+
item ? sha1_to_hex(item->object.sha1) : "(null)");
349+
}
350+
344351
int find_commit_subject(const char *commit_buffer, const char **subject)
345352
{
346353
const char *eol;

commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_n
4949

5050
int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size);
5151
int parse_commit(struct commit *item);
52+
void parse_commit_or_die(struct commit *item);
5253

5354
/* Find beginning and length of commit subject. */
5455
int find_commit_subject(const char *commit_buffer, const char **subject);

fetch-pack.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ static void rev_list_push(struct commit *commit, int mark)
4747
if (!(commit->object.flags & mark)) {
4848
commit->object.flags |= mark;
4949

50-
if (!(commit->object.parsed))
51-
if (parse_commit(commit))
52-
return;
50+
if (parse_commit(commit))
51+
return;
5352

5453
prio_queue_put(&rev_list, commit);
5554

@@ -128,8 +127,7 @@ static const unsigned char *get_rev(void)
128127
return NULL;
129128

130129
commit = prio_queue_get(&rev_list);
131-
if (!commit->object.parsed)
132-
parse_commit(commit);
130+
parse_commit(commit);
133131
parents = commit->parents;
134132

135133
commit->object.flags |= POPPED;

0 commit comments

Comments
 (0)