Skip to content

Commit 896a0e1

Browse files
dschogitster
authored andcommitted
commit-reach(paint_down_to_common): start reporting errors
If a commit cannot be parsed, it is currently ignored when looking for merge bases. That's undesirable as the operation can pretend success in a corrupt repository, even though the command should fail with an error message. Let's start at the bottom of the stack by teaching the `paint_down_to_common()` function to return an `int`: if negative, it indicates fatal error, if 0 success. This requires a couple of callers to be adjusted accordingly. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2d2da17 commit 896a0e1

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

commit-reach.c

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ static int queue_has_nonstale(struct prio_queue *queue)
4949
}
5050

5151
/* all input commits in one and twos[] must have been parsed! */
52-
static struct commit_list *paint_down_to_common(struct repository *r,
53-
struct commit *one, int n,
54-
struct commit **twos,
55-
timestamp_t min_generation,
56-
int ignore_missing_commits)
52+
static int paint_down_to_common(struct repository *r,
53+
struct commit *one, int n,
54+
struct commit **twos,
55+
timestamp_t min_generation,
56+
int ignore_missing_commits,
57+
struct commit_list **result)
5758
{
5859
struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
59-
struct commit_list *result = NULL;
6060
int i;
6161
timestamp_t last_gen = GENERATION_NUMBER_INFINITY;
6262

@@ -65,8 +65,8 @@ static struct commit_list *paint_down_to_common(struct repository *r,
6565

6666
one->object.flags |= PARENT1;
6767
if (!n) {
68-
commit_list_append(one, &result);
69-
return result;
68+
commit_list_append(one, result);
69+
return 0;
7070
}
7171
prio_queue_put(&queue, one);
7272

@@ -94,7 +94,7 @@ static struct commit_list *paint_down_to_common(struct repository *r,
9494
if (flags == (PARENT1 | PARENT2)) {
9595
if (!(commit->object.flags & RESULT)) {
9696
commit->object.flags |= RESULT;
97-
commit_list_insert_by_date(commit, &result);
97+
commit_list_insert_by_date(commit, result);
9898
}
9999
/* Mark parents of a found merge stale */
100100
flags |= STALE;
@@ -107,23 +107,27 @@ static struct commit_list *paint_down_to_common(struct repository *r,
107107
continue;
108108
if (repo_parse_commit(r, p)) {
109109
clear_prio_queue(&queue);
110-
free_commit_list(result);
110+
free_commit_list(*result);
111+
*result = NULL;
111112
/*
112113
* At this stage, we know that the commit is
113114
* missing: `repo_parse_commit()` uses
114115
* `OBJECT_INFO_DIE_IF_CORRUPT` and therefore
115116
* corrupt commits would already have been
116117
* dispatched with a `die()`.
117118
*/
118-
return NULL;
119+
if (ignore_missing_commits)
120+
return 0;
121+
return error(_("could not parse commit %s"),
122+
oid_to_hex(&p->object.oid));
119123
}
120124
p->object.flags |= flags;
121125
prio_queue_put(&queue, p);
122126
}
123127
}
124128

125129
clear_prio_queue(&queue);
126-
return result;
130+
return 0;
127131
}
128132

129133
static struct commit_list *merge_bases_many(struct repository *r,
@@ -150,7 +154,10 @@ static struct commit_list *merge_bases_many(struct repository *r,
150154
return NULL;
151155
}
152156

153-
list = paint_down_to_common(r, one, n, twos, 0, 0);
157+
if (paint_down_to_common(r, one, n, twos, 0, 0, &list)) {
158+
free_commit_list(list);
159+
return NULL;
160+
}
154161

155162
while (list) {
156163
struct commit *commit = pop_commit(&list);
@@ -204,7 +211,7 @@ static int remove_redundant_no_gen(struct repository *r,
204211
for (i = 0; i < cnt; i++)
205212
repo_parse_commit(r, array[i]);
206213
for (i = 0; i < cnt; i++) {
207-
struct commit_list *common;
214+
struct commit_list *common = NULL;
208215
timestamp_t min_generation = commit_graph_generation(array[i]);
209216

210217
if (redundant[i])
@@ -220,8 +227,16 @@ static int remove_redundant_no_gen(struct repository *r,
220227
if (curr_generation < min_generation)
221228
min_generation = curr_generation;
222229
}
223-
common = paint_down_to_common(r, array[i], filled,
224-
work, min_generation, 0);
230+
if (paint_down_to_common(r, array[i], filled,
231+
work, min_generation, 0, &common)) {
232+
clear_commit_marks(array[i], all_flags);
233+
clear_commit_marks_many(filled, work, all_flags);
234+
free_commit_list(common);
235+
free(work);
236+
free(redundant);
237+
free(filled_index);
238+
return -1;
239+
}
225240
if (array[i]->object.flags & PARENT2)
226241
redundant[i] = 1;
227242
for (j = 0; j < filled; j++)
@@ -421,6 +436,10 @@ static struct commit_list *get_merge_bases_many_0(struct repository *r,
421436
clear_commit_marks_many(n, twos, all_flags);
422437

423438
cnt = remove_redundant(r, rslt, cnt);
439+
if (cnt < 0) {
440+
free(rslt);
441+
return NULL;
442+
}
424443
result = NULL;
425444
for (i = 0; i < cnt; i++)
426445
commit_list_insert_by_date(rslt[i], &result);
@@ -490,7 +509,7 @@ int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
490509
int nr_reference, struct commit **reference,
491510
int ignore_missing_commits)
492511
{
493-
struct commit_list *bases;
512+
struct commit_list *bases = NULL;
494513
int ret = 0, i;
495514
timestamp_t generation, max_generation = GENERATION_NUMBER_ZERO;
496515

@@ -509,10 +528,11 @@ int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
509528
if (generation > max_generation)
510529
return ret;
511530

512-
bases = paint_down_to_common(r, commit,
513-
nr_reference, reference,
514-
generation, ignore_missing_commits);
515-
if (commit->object.flags & PARENT2)
531+
if (paint_down_to_common(r, commit,
532+
nr_reference, reference,
533+
generation, ignore_missing_commits, &bases))
534+
ret = -1;
535+
else if (commit->object.flags & PARENT2)
516536
ret = 1;
517537
clear_commit_marks(commit, all_flags);
518538
clear_commit_marks_many(nr_reference, reference, all_flags);
@@ -565,6 +585,10 @@ struct commit_list *reduce_heads(struct commit_list *heads)
565585
}
566586
}
567587
num_head = remove_redundant(the_repository, array, num_head);
588+
if (num_head < 0) {
589+
free(array);
590+
return NULL;
591+
}
568592
for (i = 0; i < num_head; i++)
569593
tail = &commit_list_insert(array[i], tail)->next;
570594
free(array);

0 commit comments

Comments
 (0)