Skip to content

Commit 3ad87c8

Browse files
jkufnergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 05219a1 commit 3ad87c8

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
12991299
if (!start)
13001300
start = sb->buf;
13011301
occupied = utf8_strnwidth(start, -1, 1);
1302+
occupied += c->pretty_ctx->graph_width;
13021303
padding = (-padding) - occupied;
13031304
}
13041305
while (1) {

t/t4205-log-pretty-formats.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,19 @@ EOF
319319
test_cmp expected actual
320320
'
321321

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+
322335
test_expect_success 'right alignment formatting with no padding' '
323336
git log --pretty="tformat:%>(1)%s" >actual &&
324337
cat <<EOF >expected &&
@@ -330,6 +343,17 @@ EOF
330343
test_cmp expected actual
331344
'
332345

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+
333357
test_expect_success 'right alignment formatting with no padding. i18n.logOutputEncoding' '
334358
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(1)%s" >actual &&
335359
cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&

0 commit comments

Comments
 (0)