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>
This commit is contained in:
James Coglan 2019-10-15 23:47:49 +00:00 committed by Junio C Hamano
parent 210179a20d
commit 9157a2a032

48
graph.c
View File

@ -460,22 +460,31 @@ static unsigned short graph_find_commit_color(const struct git_graph *graph,
return graph_get_current_column_color(graph);
}
static int graph_find_new_column_by_commit(struct git_graph *graph,
struct commit *commit)
{
int i;
for (i = 0; i < graph->num_new_columns; i++) {
if (graph->new_columns[i].commit == commit)
return i;
}
return -1;
}
static void graph_insert_into_new_columns(struct git_graph *graph,
struct commit *commit,
int *mapping_index)
{
int i;
int i = graph_find_new_column_by_commit(graph, commit);
/*
* If the commit is already in the new_columns list, we don't need to
* add it. Just update the mapping correctly.
*/
for (i = 0; i < graph->num_new_columns; i++) {
if (graph->new_columns[i].commit == commit) {
graph->mapping[*mapping_index] = i;
*mapping_index += 2;
return;
}
if (i >= 0) {
graph->mapping[*mapping_index] = i;
*mapping_index += 2;
return;
}
/*
@ -963,17 +972,6 @@ static void graph_output_commit_line(struct git_graph *graph, struct graph_line
graph_update_state(graph, GRAPH_COLLAPSING);
}
static struct column *find_new_column_by_commit(struct git_graph *graph,
struct commit *commit)
{
int i;
for (i = 0; i < graph->num_new_columns; i++) {
if (graph->new_columns[i].commit == commit)
return &graph->new_columns[i];
}
return NULL;
}
static void graph_output_post_merge_line(struct git_graph *graph, struct graph_line *line)
{
int seen_this = 0;
@ -1001,20 +999,20 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct graph_l
* edges.
*/
struct commit_list *parents = NULL;
struct column *par_column;
int par_column;
seen_this = 1;
parents = first_interesting_parent(graph);
assert(parents);
par_column = find_new_column_by_commit(graph, parents->item);
assert(par_column);
par_column = graph_find_new_column_by_commit(graph, parents->item);
assert(par_column >= 0);
graph_line_write_column(line, par_column, '|');
graph_line_write_column(line, &graph->new_columns[par_column], '|');
for (j = 0; j < graph->num_parents - 1; j++) {
parents = next_interesting_parent(graph, parents);
assert(parents);
par_column = find_new_column_by_commit(graph, parents->item);
assert(par_column);
graph_line_write_column(line, par_column, '\\');
par_column = graph_find_new_column_by_commit(graph, parents->item);
assert(par_column >= 0);
graph_line_write_column(line, &graph->new_columns[par_column], '\\');
graph_line_addch(line, ' ');
}
} else if (seen_this) {