Skip to content

Commit 5f30bb4

Browse files
committed
Merge branch 'nd/graph-width-padded' into maint
"log --graph --format=" learned that "%>|(N)" specifies the width relative to the terminal's left edge, not relative to the area to draw text that is to the right of the ancestry-graph section. It also now accepts negative N that means the column limit is relative to the right border. * nd/graph-width-padded: pretty.c: support <direction>|(<negative number>) forms pretty: pass graph width to pretty formatting for use in '%>|(N)'
2 parents 52debb6 + 066790d commit 5f30bb4

File tree

6 files changed

+80
-1
lines changed

6 files changed

+80
-1
lines changed

commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ struct pretty_print_context {
161161
* should not be counted on by callers.
162162
*/
163163
struct string_list in_body_headers;
164+
int graph_width;
164165
};
165166

166167
struct userformat_want {

graph.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,13 @@ static void graph_output_padding_line(struct git_graph *graph,
669669
graph_pad_horizontally(graph, sb, graph->num_new_columns * 2);
670670
}
671671

672+
673+
int graph_width(struct git_graph *graph)
674+
{
675+
return graph->width;
676+
}
677+
678+
672679
static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
673680
{
674681
/*

graph.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ int graph_is_commit_finished(struct git_graph const *graph);
6767
int graph_next_line(struct git_graph *graph, struct strbuf *sb);
6868

6969

70+
/*
71+
* Return current width of the graph in on-screen characters.
72+
*/
73+
int graph_width(struct git_graph *graph);
74+
7075
/*
7176
* graph_show_*: helper functions for printing to stdout
7277
*/

log-tree.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,8 @@ void show_log(struct rev_info *opt)
687687
ctx.output_encoding = get_log_output_encoding();
688688
if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
689689
ctx.from_ident = &opt->from_ident;
690+
if (opt->graph)
691+
ctx.graph_width = graph_width(opt->graph);
690692
pretty_print_commit(&ctx, commit, &msgbuf);
691693

692694
if (opt->add_signoff)

pretty.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,15 @@ static size_t parse_padding_placeholder(struct strbuf *sb,
10221022
int width;
10231023
if (!end || end == start)
10241024
return 0;
1025-
width = strtoul(start, &next, 10);
1025+
width = strtol(start, &next, 10);
10261026
if (next == start || width == 0)
10271027
return 0;
1028+
if (width < 0) {
1029+
if (to_column)
1030+
width += term_columns();
1031+
if (width < 0)
1032+
return 0;
1033+
}
10281034
c->padding = to_column ? -width : width;
10291035
c->flush_type = flush_type;
10301036

@@ -1299,6 +1305,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
12991305
if (!start)
13001306
start = sb->buf;
13011307
occupied = utf8_strnwidth(start, -1, 1);
1308+
occupied += c->pretty_ctx->graph_width;
13021309
padding = (-padding) - occupied;
13031310
}
13041311
while (1) {

t/t4205-log-pretty-formats.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ EOF
176176
test_cmp expected actual
177177
'
178178

179+
test_expect_success 'left alignment formatting at the nth column' '
180+
COLUMNS=50 git log --pretty="tformat:%h %<|(-10)%s" >actual &&
181+
qz_to_tab_space <<EOF >expected &&
182+
$head1 message two Z
183+
$head2 message one Z
184+
$head3 add bar Z
185+
$head4 $(commit_msg) Z
186+
EOF
187+
test_cmp expected actual
188+
'
189+
179190
test_expect_success 'left alignment formatting at the nth column. i18n.logOutputEncoding' '
180191
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %<|(40)%s" >actual &&
181192
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
@@ -308,6 +319,17 @@ EOF
308319
test_cmp expected actual
309320
'
310321

322+
test_expect_success 'right alignment formatting at the nth column' '
323+
COLUMNS=50 git log --pretty="tformat:%h %>|(-10)%s" >actual &&
324+
qz_to_tab_space <<EOF >expected &&
325+
$head1 message two
326+
$head2 message one
327+
$head3 add bar
328+
$head4 $(commit_msg)
329+
EOF
330+
test_cmp expected actual
331+
'
332+
311333
test_expect_success 'right alignment formatting at the nth column. i18n.logOutputEncoding' '
312334
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %>|(40)%s" >actual &&
313335
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
@@ -319,6 +341,19 @@ EOF
319341
test_cmp expected actual
320342
'
321343

344+
# Note: Space between 'message' and 'two' should be in the same column
345+
# as in previous test.
346+
test_expect_success 'right alignment formatting at the nth column with --graph. i18n.logOutputEncoding' '
347+
git -c i18n.logOutputEncoding=$test_encoding log --graph --pretty="tformat:%h %>|(40)%s" >actual &&
348+
iconv -f utf-8 -t $test_encoding >expected <<EOF&&
349+
* $head1 message two
350+
* $head2 message one
351+
* $head3 add bar
352+
* $head4 $(commit_msg)
353+
EOF
354+
test_cmp expected actual
355+
'
356+
322357
test_expect_success 'right alignment formatting with no padding' '
323358
git log --pretty="tformat:%>(1)%s" >actual &&
324359
cat <<EOF >expected &&
@@ -330,6 +365,17 @@ EOF
330365
test_cmp expected actual
331366
'
332367

368+
test_expect_success 'right alignment formatting with no padding and with --graph' '
369+
git log --graph --pretty="tformat:%>(1)%s" >actual &&
370+
cat <<EOF >expected &&
371+
* message two
372+
* message one
373+
* add bar
374+
* $(commit_msg)
375+
EOF
376+
test_cmp expected actual
377+
'
378+
333379
test_expect_success 'right alignment formatting with no padding. i18n.logOutputEncoding' '
334380
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(1)%s" >actual &&
335381
cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
@@ -373,6 +419,17 @@ EOF
373419
test_cmp expected actual
374420
'
375421

422+
test_expect_success 'center alignment formatting at the nth column' '
423+
COLUMNS=70 git log --pretty="tformat:%h %><|(-30)%s" >actual &&
424+
qz_to_tab_space <<EOF >expected &&
425+
$head1 message two Z
426+
$head2 message one Z
427+
$head3 add bar Z
428+
$head4 $(commit_msg) Z
429+
EOF
430+
test_cmp expected actual
431+
'
432+
376433
test_expect_success 'center alignment formatting at the nth column. i18n.logOutputEncoding' '
377434
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %><|(40)%s" >actual &&
378435
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&

0 commit comments

Comments
 (0)