graph: reduce duplication in `graph_insert_into_new_columns()`
I will shortly be making some changes to this function and so am trying to simplify it. It currently contains some duplicated logic; both branches the function can take assign the commit's column index into the `mapping` array and increment `mapping_index`. Here I change the function so that the only conditional behaviour is that it appends the commit to `new_columns` if it's not present. All manipulation of `mapping` now happens on a single code path. Signed-off-by: James Coglan <jcoglan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
James Coglan committed
Oct 15, 2019 at 23:47 UTC
a551fd5efd7b82604c3254e3f7cac08eaaa97ba9
1 file changed
+7
-13
graph.c
+7
-13
@@ -478,23 +478,17 @@ static void graph_insert_into_new_columns(struct git_graph *graph,
478
int i = graph_find_new_column_by_commit(graph, commit);
479
480
/*
481
- * If the commit is already in the new_columns list, we don't need to
482
- * add it. Just update the mapping correctly.
481
+ * If the commit is not already in the new_columns array, then add it
482
+ * and record it as being in the final column.
483
*/
484
- if (i >= 0) {
485
- graph->mapping[*mapping_index] = i;
486
- *mapping_index += 2;
487
- return;
484
+ if (i < 0) {
485
+ i = graph->num_new_columns++;
486
+ graph->new_columns[i].commit = commit;
487
+ graph->new_columns[i].color = graph_find_commit_color(graph, commit);
488
}
489
490
- /*
491
- * This commit isn't already in new_columns. Add it.
492
- */
493
- graph->new_columns[graph->num_new_columns].commit = commit;
494
- graph->new_columns[graph->num_new_columns].color = graph_find_commit_color(graph, commit);
495
- graph->mapping[*mapping_index] = graph->num_new_columns;
490
+ graph->mapping[*mapping_index] = i;
491
*mapping_index += 2;
497
- graph->num_new_columns++;
492
}
493
494
static void graph_update_width(struct git_graph *graph,