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