@matheusbsbs / matheus-bagliano / commits / 0724cb86c5
Fork 0

graph API: eliminate unnecessary indentation

This change improves the calculation of the amount of horizontal padding, so that there is always exactly 1 space of padding. Previously, most commits had 3 spaces of padding, but commits that didn't have any children in the graph had only 1 space of padding. Signed-off-by: Adam Simpkins <adam@adamsimpkins.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adam Simpkins committed May 5, 2008 at 00:57 UTC 0724cb86c51a230090ef12d6bfe17a67351631a8
1 file changed +52 -14
graph.c
+52 -14
@@ -60,6 +60,12 @@ struct git_graph {
60 * this number)
61 */
62 int num_parents;
63 + /*
64 + * The width of the graph output for this commit.
65 + * All rows for this commit are padded to this width, so that
66 + * messages printed after the graph output are aligned.
67 + */
68 + int width;
69 /*
70 * The next expansion row to print
71 * when state is GRAPH_PRE_COMMIT
@@ -207,13 +213,48 @@ static void graph_insert_into_new_columns(struct git_graph *graph,
213 graph->num_new_columns++;
214 }
215
216 +static void graph_update_width(struct git_graph *graph,
217 + int is_commit_in_existing_columns)
218 +{
219 + /*
220 + * Compute the width needed to display the graph for this commit.
221 + * This is the maximum width needed for any row. All other rows
222 + * will be padded to this width.
223 + *
224 + * Compute the number of columns in the widest row:
225 + * Count each existing column (graph->num_columns), and each new
226 + * column added by this commit.
227 + */
228 + int max_cols = graph->num_columns + graph->num_parents;
229 +
230 + /*
231 + * Even if the current commit has no parents, it still takes up a
232 + * column for itself.
233 + */
234 + if (graph->num_parents < 1)
235 + max_cols++;
236 +
237 + /*
238 + * We added a column for the the current commit as part of
239 + * graph->num_parents. If the current commit was already in
240 + * graph->columns, then we have double counted it.
241 + */
242 + if (is_commit_in_existing_columns)
243 + max_cols--;
244 +
245 + /*
246 + * Each column takes up 2 spaces
247 + */
248 + graph->width = max_cols * 2;
249 +}
250 +
251 static void graph_update_columns(struct git_graph *graph)
252 {
253 struct commit_list *parent;
254 struct column *tmp_columns;
255 int max_new_columns;
256 int mapping_idx;
216 - int i, seen_this;
257 + int i, seen_this, is_commit_in_columns;
258
259 /*
260 * Swap graph->columns with graph->new_columns
@@ -259,11 +300,13 @@ static void graph_update_columns(struct git_graph *graph)
300 */
301 seen_this = 0;
302 mapping_idx = 0;
303 + is_commit_in_columns = 1;
304 for (i = 0; i <= graph->num_columns; i++) {
305 struct commit *col_commit;
306 if (i == graph->num_columns) {
307 if (seen_this)
308 break;
309 + is_commit_in_columns = 0;
310 col_commit = graph->commit;
311 } else {
312 col_commit = graph->columns[i].commit;
@@ -290,6 +333,11 @@ static void graph_update_columns(struct git_graph *graph)
333 while (graph->mapping_size > 1 &&
334 graph->mapping[graph->mapping_size - 1] < 0)
335 graph->mapping_size--;
336 +
337 + /*
338 + * Compute graph->width for this commit
339 + */
340 + graph_update_width(graph, is_commit_in_columns);
341 }
342
343 void graph_update(struct git_graph *graph, struct commit *commit)
@@ -368,22 +416,12 @@ static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb)
416 *
417 * This way, fields printed to the right of the graph will remain
418 * aligned for the entire commit.
371 - *
372 - * This computation results in 3 extra space to the right in most
373 - * cases, but only 1 extra space if the commit doesn't have any
374 - * children that have already been displayed in the graph (i.e.,
375 - * if the current commit isn't in graph->columns).
419 */
377 - size_t extra;
378 - size_t final_width = graph->num_columns + graph->num_parents;
379 - if (graph->num_parents < 1)
380 - final_width++;
381 - final_width *= 2;
382 -
383 - if (sb->len >= final_width)
420 + int extra;
421 + if (sb->len >= graph->width)
422 return;
423
386 - extra = final_width - sb->len;
424 + extra = graph->width - sb->len;
425 strbuf_addf(sb, "%*s", (int) extra, "");
426 }
427