Skip to content

Commit

Permalink
graph: reuse find_new_column_by_commit()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jcoglan authored and gitster committed Oct 16, 2019
1 parent 210179a commit 9157a2a
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9157a2a

Please sign in to comment.