graph: reuse `find_new_column_by_commit()`
I will shortly be making some changes to `graph_insert_into_new_columns()` and so am trying to simplify it. One possible simplification is that we can extract the loop for finding the element in `new_columns` containing the given commit. `find_new_column_by_commit()` contains a very similar loop but it returns a `struct column *` rather than an `int` offset into the array. Here I'm introducing a version that returns `int` and using that in `graph_insert_into_new_columns()` and `graph_output_post_merge_line()`. 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
9157a2a032c4c5a154782537b6f1e2f8b7bd7435
1 file changed
+23
-25
graph.c
+23
-25
@@ -460,22 +460,31 @@ static unsigned short graph_find_commit_color(const struct git_graph *graph,
460
return graph_get_current_column_color(graph);
461
}
462
463
+static int graph_find_new_column_by_commit(struct git_graph *graph,
464
+ struct commit *commit)
465
+{
466
+ int i;
467
+ for (i = 0; i < graph->num_new_columns; i++) {
468
+ if (graph->new_columns[i].commit == commit)
469
+ return i;
470
+ }
471
+ return -1;
472
+}
473
+
474
static void graph_insert_into_new_columns(struct git_graph *graph,
475
struct commit *commit,
476
int *mapping_index)
477
{
467
- int i;
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.
483
*/
473
- for (i = 0; i < graph->num_new_columns; i++) {
474
- if (graph->new_columns[i].commit == commit) {
475
- graph->mapping[*mapping_index] = i;
476
- *mapping_index += 2;
477
- return;
478
- }
484
+ if (i >= 0) {
485
+ graph->mapping[*mapping_index] = i;
486
+ *mapping_index += 2;
487
+ return;
488
}
489
490
/*
@@ -963,17 +972,6 @@ static void graph_output_commit_line(struct git_graph *graph, struct graph_line
972
graph_update_state(graph, GRAPH_COLLAPSING);
973
}
974
966
-static struct column *find_new_column_by_commit(struct git_graph *graph,
967
- struct commit *commit)
968
-{
969
- int i;
970
- for (i = 0; i < graph->num_new_columns; i++) {
971
- if (graph->new_columns[i].commit == commit)
972
- return &graph->new_columns[i];
973
- }
974
- return NULL;
975
-}
976
-
975
static void graph_output_post_merge_line(struct git_graph *graph, struct graph_line *line)
976
{
977
int seen_this = 0;
@@ -1001,20 +999,20 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct graph_l
999
* edges.
1000
*/
1001
struct commit_list *parents = NULL;
1004
- struct column *par_column;
1002
+ int par_column;
1003
seen_this = 1;
1004
parents = first_interesting_parent(graph);
1005
assert(parents);
1008
- par_column = find_new_column_by_commit(graph, parents->item);
1009
- assert(par_column);
1006
+ par_column = graph_find_new_column_by_commit(graph, parents->item);
1007
+ assert(par_column >= 0);
1008
1011
- graph_line_write_column(line, par_column, '|');
1009
+ graph_line_write_column(line, &graph->new_columns[par_column], '|');
1010
for (j = 0; j < graph->num_parents - 1; j++) {
1011
parents = next_interesting_parent(graph, parents);
1012
assert(parents);
1015
- par_column = find_new_column_by_commit(graph, parents->item);
1016
- assert(par_column);
1017
- graph_line_write_column(line, par_column, '\\');
1013
+ par_column = graph_find_new_column_by_commit(graph, parents->item);
1014
+ assert(par_column >= 0);
1015
+ graph_line_write_column(line, &graph->new_columns[par_column], '\\');
1016
graph_line_addch(line, ' ');
1017
}
1018
} else if (seen_this) {