Skip to content

Commit

Permalink
graph: handle line padding in graph_next_line()
Browse files Browse the repository at this point in the history
Now that the display width of graph lines is implicitly tracked via the
`graph_line` interface, the calls to `graph_pad_horizontally()` no
longer need to be located inside the individual output functions, where
the character counting was previously being done.

All the functions called by `graph_next_line()` generate a line of
output, then call `graph_pad_horizontally()`, and finally change the
graph state if necessary. As padding is the final change to the output
done by all these functions, it can be removed from all of them and done
in `graph_next_line()` instead.

I've also moved the guard in `graph_output_padding_line()` that checks
the graph has a commit; this function is only called by
`graph_next_line()` and we must not pad the `graph_line` if no commit is
set.

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 fbccf25 commit 210179a
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -732,25 +732,13 @@ static void graph_output_padding_line(struct git_graph *graph,
{
int i;

/*
* We could conceivable be called with a NULL commit
* if our caller has a bug, and invokes graph_next_line()
* immediately after graph_init(), without first calling
* graph_update(). Return without outputting anything in this
* case.
*/
if (!graph->commit)
return;

/*
* Output a padding row, that leaves all branch lines unchanged
*/
for (i = 0; i < graph->num_new_columns; i++) {
graph_line_write_column(line, &graph->new_columns[i], '|');
graph_line_addch(line, ' ');
}

graph_pad_horizontally(graph, line);
}


Expand All @@ -767,7 +755,6 @@ static void graph_output_skip_line(struct git_graph *graph, struct graph_line *l
* of the graph is missing.
*/
graph_line_addstr(line, "...");
graph_pad_horizontally(graph, line);

if (graph->num_parents >= 3 &&
graph->commit_index < (graph->num_columns - 1))
Expand Down Expand Up @@ -832,8 +819,6 @@ static void graph_output_pre_commit_line(struct git_graph *graph,
graph_line_addch(line, ' ');
}

graph_pad_horizontally(graph, line);

/*
* Increment graph->expansion_row,
* and move to state GRAPH_COMMIT if necessary
Expand Down Expand Up @@ -967,8 +952,6 @@ static void graph_output_commit_line(struct git_graph *graph, struct graph_line
graph_line_addch(line, ' ');
}

graph_pad_horizontally(graph, line);

/*
* Update graph->state
*/
Expand Down Expand Up @@ -1043,8 +1026,6 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct graph_l
}
}

graph_pad_horizontally(graph, line);

/*
* Update graph->state
*/
Expand Down Expand Up @@ -1186,8 +1167,6 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
}
}

graph_pad_horizontally(graph, line);

/*
* Swap mapping and new_mapping
*/
Expand All @@ -1204,31 +1183,43 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l

int graph_next_line(struct git_graph *graph, struct strbuf *sb)
{
int shown_commit_line = 0;
struct graph_line line = { .buf = sb, .width = 0 };

/*
* We could conceivable be called with a NULL commit
* if our caller has a bug, and invokes graph_next_line()
* immediately after graph_init(), without first calling
* graph_update(). Return without outputting anything in this
* case.
*/
if (!graph->commit)
return -1;

switch (graph->state) {
case GRAPH_PADDING:
graph_output_padding_line(graph, &line);
return 0;
break;
case GRAPH_SKIP:
graph_output_skip_line(graph, &line);
return 0;
break;
case GRAPH_PRE_COMMIT:
graph_output_pre_commit_line(graph, &line);
return 0;
break;
case GRAPH_COMMIT:
graph_output_commit_line(graph, &line);
return 1;
shown_commit_line = 1;
break;
case GRAPH_POST_MERGE:
graph_output_post_merge_line(graph, &line);
return 0;
break;
case GRAPH_COLLAPSING:
graph_output_collapsing_line(graph, &line);
return 0;
break;
}

assert(0);
return 0;
graph_pad_horizontally(graph, &line);
return shown_commit_line;
}

static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
Expand Down

0 comments on commit 210179a

Please sign in to comment.