graph: fix extra spaces in graph_padding_line

The graph_padding_line() function outputs a series of "|" columns, and then pads with spaces to graph->width by calling graph_pad_horizontally(). However, we tell the latter that we wrote graph->num_columns characters, which is not true; we also needed spaces between the columns. Let's keep a count of how many characters we've written, which is what all the other callers of graph_pad_horizontally() do. Without this, any output that is written at the end of a padding line will be bumped out by at least an extra graph->num_columns spaces. Presumably nobody ever noticed the bug because there's no code path that actually writes to the end of a padding line. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 29, 2016 at 04:37 UTC 16477935245a522d99d0dd7e346638c02542f1d0
1 file changed +12 -4
graph.c
+12 -4
@@ -1138,6 +1138,7 @@ int graph_next_line(struct git_graph *graph, struct strbuf *sb)
1138 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
1139 {
1140 int i;
1141 + int chars_written = 0;
1142
1143 if (graph->state != GRAPH_COMMIT) {
1144 graph_next_line(graph, sb);
@@ -1153,14 +1154,21 @@ static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
1154 */
1155 for (i = 0; i < graph->num_columns; i++) {
1156 struct column *col = &graph->columns[i];
1157 +
1158 strbuf_write_column(sb, col, '|');
1157 - if (col->commit == graph->commit && graph->num_parents > 2)
1158 - strbuf_addchars(sb, ' ', (graph->num_parents - 2) * 2);
1159 - else
1159 + chars_written++;
1160 +
1161 + if (col->commit == graph->commit && graph->num_parents > 2) {
1162 + int len = (graph->num_parents - 2) * 2;
1163 + strbuf_addchars(sb, ' ', len);
1164 + chars_written += len;
1165 + } else {
1166 strbuf_addch(sb, ' ');
1167 + chars_written++;
1168 + }
1169 }
1170
1163 - graph_pad_horizontally(graph, sb, graph->num_columns);
1171 + graph_pad_horizontally(graph, sb, chars_written);
1172
1173 /*
1174 * Update graph->prev_state since we have output a padding line