pretty: pass graph width to pretty formatting for use in '%>|(N)'
Pass graph width to pretty formatting, to make N in '%>|(N)' include columns consumed by graph rendered when --graph option is in use. For example, in the output of git log --all --graph --pretty='format: [%>|(20)%h] %ar%d' this change will make all commit hashes align at 20th column from the edge of the terminal, not from the edge of the graph. Signed-off-by: Josef Kufner <josef@kufner.cz> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Josef Kufner committed
Jun 16, 2016 at 20:18 UTC
3ad87c807c2b6cbfbdfb2c78412781ecc7db593d
6 files changed
+40
commit.h
+1
@@ -161,6 +161,7 @@ struct pretty_print_context {
161
* should not be counted on by callers.
162
*/
163
struct string_list in_body_headers;
164
+ int graph_width;
165
};
166
167
struct userformat_want {
graph.c
+7
@@ -669,6 +669,13 @@ static void graph_output_padding_line(struct git_graph *graph,
669
graph_pad_horizontally(graph, sb, graph->num_new_columns * 2);
670
}
671
672
+
673
+int graph_width(struct git_graph *graph)
674
+{
675
+ return graph->width;
676
+}
677
+
678
+
679
static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
680
{
681
/*
graph.h
+5
@@ -67,6 +67,11 @@ int graph_is_commit_finished(struct git_graph const *graph);
67
int graph_next_line(struct git_graph *graph, struct strbuf *sb);
68
69
70
+/*
71
+ * Return current width of the graph in on-screen characters.
72
+ */
73
+int graph_width(struct git_graph *graph);
74
+
75
/*
76
* graph_show_*: helper functions for printing to stdout
77
*/
log-tree.c
+2
@@ -687,6 +687,8 @@ void show_log(struct rev_info *opt)
687
ctx.output_encoding = get_log_output_encoding();
688
if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
689
ctx.from_ident = &opt->from_ident;
690
+ if (opt->graph)
691
+ ctx.graph_width = graph_width(opt->graph);
692
pretty_print_commit(&ctx, commit, &msgbuf);
693
694
if (opt->add_signoff)
pretty.c
+1
@@ -1299,6 +1299,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
1299
if (!start)
1300
start = sb->buf;
1301
occupied = utf8_strnwidth(start, -1, 1);
1302
+ occupied += c->pretty_ctx->graph_width;
1303
padding = (-padding) - occupied;
1304
}
1305
while (1) {
t/t4205-log-pretty-formats.sh
+24
@@ -319,6 +319,19 @@ EOF
319
test_cmp expected actual
320
'
321
322
+# Note: Space between 'message' and 'two' should be in the same column
323
+# as in previous test.
324
+test_expect_success 'right alignment formatting at the nth column with --graph. i18n.logOutputEncoding' '
325
+ git -c i18n.logOutputEncoding=$test_encoding log --graph --pretty="tformat:%h %>|(40)%s" >actual &&
326
+ iconv -f utf-8 -t $test_encoding >expected <<EOF&&
327
+* $head1 message two
328
+* $head2 message one
329
+* $head3 add bar
330
+* $head4 $(commit_msg)
331
+EOF
332
+ test_cmp expected actual
333
+'
334
+
335
test_expect_success 'right alignment formatting with no padding' '
336
git log --pretty="tformat:%>(1)%s" >actual &&
337
cat <<EOF >expected &&
@@ -330,6 +343,17 @@ EOF
343
test_cmp expected actual
344
'
345
346
+test_expect_success 'right alignment formatting with no padding and with --graph' '
347
+ git log --graph --pretty="tformat:%>(1)%s" >actual &&
348
+ cat <<EOF >expected &&
349
+* message two
350
+* message one
351
+* add bar
352
+* $(commit_msg)
353
+EOF
354
+ test_cmp expected actual
355
+'
356
+
357
test_expect_success 'right alignment formatting with no padding. i18n.logOutputEncoding' '
358
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(1)%s" >actual &&
359
cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&