graph: handle line padding in `graph_next_line()`

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>

James Coglan committed Oct 15, 2019 at 23:47 UTC 210179a20d585f6a96e0963db69790e590bd9433
1 file changed +20 -29
graph.c
+20 -29
@@ -732,16 +732,6 @@ static void graph_output_padding_line(struct git_graph *graph,
732 {
733 int i;
734
735 - /*
736 - * We could conceivable be called with a NULL commit
737 - * if our caller has a bug, and invokes graph_next_line()
738 - * immediately after graph_init(), without first calling
739 - * graph_update(). Return without outputting anything in this
740 - * case.
741 - */
742 - if (!graph->commit)
743 - return;
744 -
735 /*
736 * Output a padding row, that leaves all branch lines unchanged
737 */
@@ -749,8 +739,6 @@ static void graph_output_padding_line(struct git_graph *graph,
739 graph_line_write_column(line, &graph->new_columns[i], '|');
740 graph_line_addch(line, ' ');
741 }
752 -
753 - graph_pad_horizontally(graph, line);
742 }
743
744
@@ -767,7 +755,6 @@ static void graph_output_skip_line(struct git_graph *graph, struct graph_line *l
755 * of the graph is missing.
756 */
757 graph_line_addstr(line, "...");
770 - graph_pad_horizontally(graph, line);
758
759 if (graph->num_parents >= 3 &&
760 graph->commit_index < (graph->num_columns - 1))
@@ -832,8 +819,6 @@ static void graph_output_pre_commit_line(struct git_graph *graph,
819 graph_line_addch(line, ' ');
820 }
821
835 - graph_pad_horizontally(graph, line);
836 -
822 /*
823 * Increment graph->expansion_row,
824 * and move to state GRAPH_COMMIT if necessary
@@ -967,8 +952,6 @@ static void graph_output_commit_line(struct git_graph *graph, struct graph_line
952 graph_line_addch(line, ' ');
953 }
954
970 - graph_pad_horizontally(graph, line);
971 -
955 /*
956 * Update graph->state
957 */
@@ -1043,8 +1026,6 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct graph_l
1026 }
1027 }
1028
1046 - graph_pad_horizontally(graph, line);
1047 -
1029 /*
1030 * Update graph->state
1031 */
@@ -1186,8 +1167,6 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
1167 }
1168 }
1169
1189 - graph_pad_horizontally(graph, line);
1190 -
1170 /*
1171 * Swap mapping and new_mapping
1172 */
@@ -1204,31 +1183,43 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
1183
1184 int graph_next_line(struct git_graph *graph, struct strbuf *sb)
1185 {
1186 + int shown_commit_line = 0;
1187 struct graph_line line = { .buf = sb, .width = 0 };
1188
1189 + /*
1190 + * We could conceivable be called with a NULL commit
1191 + * if our caller has a bug, and invokes graph_next_line()
1192 + * immediately after graph_init(), without first calling
1193 + * graph_update(). Return without outputting anything in this
1194 + * case.
1195 + */
1196 + if (!graph->commit)
1197 + return -1;
1198 +
1199 switch (graph->state) {
1200 case GRAPH_PADDING:
1201 graph_output_padding_line(graph, &line);
1212 - return 0;
1202 + break;
1203 case GRAPH_SKIP:
1204 graph_output_skip_line(graph, &line);
1215 - return 0;
1205 + break;
1206 case GRAPH_PRE_COMMIT:
1207 graph_output_pre_commit_line(graph, &line);
1218 - return 0;
1208 + break;
1209 case GRAPH_COMMIT:
1210 graph_output_commit_line(graph, &line);
1221 - return 1;
1211 + shown_commit_line = 1;
1212 + break;
1213 case GRAPH_POST_MERGE:
1214 graph_output_post_merge_line(graph, &line);
1224 - return 0;
1215 + break;
1216 case GRAPH_COLLAPSING:
1217 graph_output_collapsing_line(graph, &line);
1227 - return 0;
1218 + break;
1219 }
1220
1230 - assert(0);
1231 - return 0;
1221 + graph_pad_horizontally(graph, &line);
1222 + return shown_commit_line;
1223 }
1224
1225 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)