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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Feb 28, 2024 at 09:44 UTC 896a0e11f37687fb3d40c0aa2b28062264232823
1 file changed +45 -21
commit-reach.c
+45 -21
@@ -49,14 +49,14 @@ static int queue_has_nonstale(struct prio_queue *queue)
49 }
50
51 /* 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)
58 {
59 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
59 - struct commit_list *result = NULL;
60 int i;
61 timestamp_t last_gen = GENERATION_NUMBER_INFINITY;
62
@@ -65,8 +65,8 @@ static struct commit_list *paint_down_to_common(struct repository *r,
65
66 one->object.flags |= PARENT1;
67 if (!n) {
68 - commit_list_append(one, &result);
69 - return result;
68 + commit_list_append(one, result);
69 + return 0;
70 }
71 prio_queue_put(&queue, one);
72
@@ -94,7 +94,7 @@ static struct commit_list *paint_down_to_common(struct repository *r,
94 if (flags == (PARENT1 | PARENT2)) {
95 if (!(commit->object.flags & RESULT)) {
96 commit->object.flags |= RESULT;
97 - commit_list_insert_by_date(commit, &result);
97 + commit_list_insert_by_date(commit, result);
98 }
99 /* Mark parents of a found merge stale */
100 flags |= STALE;
@@ -107,7 +107,8 @@ static struct commit_list *paint_down_to_common(struct repository *r,
107 continue;
108 if (repo_parse_commit(r, p)) {
109 clear_prio_queue(&queue);
110 - free_commit_list(result);
110 + free_commit_list(*result);
111 + *result = NULL;
112 /*
113 * At this stage, we know that the commit is
114 * missing: `repo_parse_commit()` uses
@@ -115,7 +116,10 @@ static struct commit_list *paint_down_to_common(struct repository *r,
116 * corrupt commits would already have been
117 * dispatched with a `die()`.
118 */
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));
123 }
124 p->object.flags |= flags;
125 prio_queue_put(&queue, p);
@@ -123,7 +127,7 @@ static struct commit_list *paint_down_to_common(struct repository *r,
127 }
128
129 clear_prio_queue(&queue);
126 - return result;
130 + return 0;
131 }
132
133 static struct commit_list *merge_bases_many(struct repository *r,
@@ -150,7 +154,10 @@ static struct commit_list *merge_bases_many(struct repository *r,
154 return NULL;
155 }
156
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 + }
161
162 while (list) {
163 struct commit *commit = pop_commit(&list);
@@ -204,7 +211,7 @@ static int remove_redundant_no_gen(struct repository *r,
211 for (i = 0; i < cnt; i++)
212 repo_parse_commit(r, array[i]);
213 for (i = 0; i < cnt; i++) {
207 - struct commit_list *common;
214 + struct commit_list *common = NULL;
215 timestamp_t min_generation = commit_graph_generation(array[i]);
216
217 if (redundant[i])
@@ -220,8 +227,16 @@ static int remove_redundant_no_gen(struct repository *r,
227 if (curr_generation < min_generation)
228 min_generation = curr_generation;
229 }
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 + }
240 if (array[i]->object.flags & PARENT2)
241 redundant[i] = 1;
242 for (j = 0; j < filled; j++)
@@ -421,6 +436,10 @@ static struct commit_list *get_merge_bases_many_0(struct repository *r,
436 clear_commit_marks_many(n, twos, all_flags);
437
438 cnt = remove_redundant(r, rslt, cnt);
439 + if (cnt < 0) {
440 + free(rslt);
441 + return NULL;
442 + }
443 result = NULL;
444 for (i = 0; i < cnt; i++)
445 commit_list_insert_by_date(rslt[i], &result);
@@ -490,7 +509,7 @@ int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
509 int nr_reference, struct commit **reference,
510 int ignore_missing_commits)
511 {
493 - struct commit_list *bases;
512 + struct commit_list *bases = NULL;
513 int ret = 0, i;
514 timestamp_t generation, max_generation = GENERATION_NUMBER_ZERO;
515
@@ -509,10 +528,11 @@ int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
528 if (generation > max_generation)
529 return ret;
530
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)
536 ret = 1;
537 clear_commit_marks(commit, all_flags);
538 clear_commit_marks_many(nr_reference, reference, all_flags);
@@ -565,6 +585,10 @@ struct commit_list *reduce_heads(struct commit_list *heads)
585 }
586 }
587 num_head = remove_redundant(the_repository, array, num_head);
588 + if (num_head < 0) {
589 + free(array);
590 + return NULL;
591 + }
592 for (i = 0; i < num_head; i++)
593 tail = &commit_list_insert(array[i], tail)->next;
594 free(array);