threads / patch / 40326

patchPass graph width to pretty formatting, so '%>|' can work properly

Subject: [PATCH] Pass graph width to pretty formatting, so '%>|' can work properly

## tl;dr

8 messages between Sep 11, 2015 and Sep 11, 2015. Diffs are folded; open one to read it.

replies: 7people: 3as markdown or json

Josef Kufner· Sep 11, 2015, 13:47 UTC · lore

Pass graph width to pretty formatting function, so it can handle '%>|(N)' paddings correctly when --graph option is used.

Example:
  git log --all --graph --pretty='format: [%>|(20)%h] %ar%d'
  All commit hashes should be aligned at 20th column from edge of the
  terminal, not from the edge of the graph.
Signed-off-by: Josef Kufner <josef@kufner.cz>
---
 commit.h   | 1 +
 graph.c    | 7 +++++++
 graph.h    | 5 +++++
 log-tree.c | 2 ++
 pretty.c   | 1 +
 5 files changed, 16 insertions(+)
Show changes to 5 files +16 −0

commit.h, graph.c, graph.h, log-tree.c, pretty.c

diff --git a/commit.h b/commit.h
index 5d58be0..0a9a707 100644
--- a/commit.h
+++ b/commit.h
@@ -160,6 +160,7 @@ struct pretty_print_context {
 	 * should not be counted on by callers.
 	 */
 	struct string_list in_body_headers;
+	int graph_width;
 };
 
 struct userformat_want {
diff --git a/graph.c b/graph.c
index c25a09a..4802411 100644
--- a/graph.c
+++ b/graph.c
@@ -671,6 +671,13 @@ static void graph_output_padding_line(struct git_graph *graph,
 	graph_pad_horizontally(graph, sb, graph->num_new_columns * 2);
 }
 
+
+int graph_width(struct git_graph *graph)
+{
+	return graph->width;
+}
+
+
 static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
 {
 	/*
diff --git a/graph.h b/graph.h
index 0be62bd..3f48c19 100644
--- a/graph.h
+++ b/graph.h
@@ -68,6 +68,11 @@ int graph_next_line(struct git_graph *graph, struct strbuf *sb);
 
 
 /*
+ * Return current width of the graph in on-screen characters.
+ */
+int graph_width(struct git_graph *graph);
+
+/*
  * graph_show_*: helper functions for printing to stdout
  */
 
diff --git a/log-tree.c b/log-tree.c
index 7b1b57a..08fd5b6 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -686,6 +686,8 @@ void show_log(struct rev_info *opt)
 	ctx.output_encoding = get_log_output_encoding();
 	if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
 		ctx.from_ident = &opt->from_ident;
+	if (opt->graph)
+		ctx.graph_width = graph_width(opt->graph);
 	pretty_print_commit(&ctx, commit, &msgbuf);
 
 	if (opt->add_signoff)
diff --git a/pretty.c b/pretty.c
index 151c2ae..f1cf9e2 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1297,6 +1297,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
 		if (!start)
 			start = sb->buf;
 		occupied = utf8_strnwidth(start, -1, 1);
+		occupied += c->pretty_ctx->graph_width;
 		padding = (-padding) - occupied;
 	}
 	while (1) {
-- 
2.5.1
Junio C Hamano· Sep 11, 2015, 16:54 UTC · re: Josef Kufner · lore

Re: [PATCH] Pass graph width to pretty formatting, so '%>|' can work properly

This "feels" correct ;-), but let me summon Duy who did the %>< padding at around a5752342 (pretty: support padding placeholders, %< %> and %><, 2013-04-19) for an extra set of eyes.

Care to add a test or two while at it?
Thanks.  
Josef Kufner· Sep 11, 2015, 17:50 UTC · re: Junio C Hamano · lore

[PATCH] Add tests for "Pass graph width to pretty formatting, so '%>|' can work properly"

---
 t/t4205-log-pretty-formats.sh | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
Show changes to t/t4205-log-pretty-formats.sh +23 −0
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index 7398605..3358837 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -319,6 +319,18 @@ EOF
 	test_cmp expected actual
 '
 
+# Note: Space between 'message' and 'two' should be in the same column as in previous test.
+test_expect_success 'right alignment formatting at the nth column with --graph. i18n.logOutputEncoding' '
+	git -c i18n.logOutputEncoding=$test_encoding log --graph --pretty="tformat:%h %>|(40)%s" >actual &&
+	qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
+* $head1                    message two
+* $head2                    message one
+* $head3                        add bar
+* $head4            $(commit_msg)
+EOF
+	test_cmp expected actual
+'
+
 test_expect_success 'right alignment formatting with no padding' '
 	git log --pretty="tformat:%>(1)%s" >actual &&
 	cat <<EOF >expected &&
@@ -330,6 +342,17 @@ EOF
 	test_cmp expected actual
 '
 
+test_expect_success 'right alignment formatting with no padding and with --graph' '
+	git log --graph --pretty="tformat:%>(1)%s" >actual &&
+	cat <<EOF >expected &&
+* message two
+* message one
+* add bar
+* $(commit_msg)
+EOF
+	test_cmp expected actual
+'
+
 test_expect_success 'right alignment formatting with no padding. i18n.logOutputEncoding' '
 	git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(1)%s" >actual &&
 	cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
-- 
2.5.1
Eric Sunshine· Sep 11, 2015, 19:37 UTC · re: Josef Kufner · lore

Re: [PATCH] Add tests for "Pass graph width to pretty formatting, so '%>|' can work properly"

On Fri, Sep 11, 2015 at 1:50 PM, Josef Kufner <josef@kufner.cz> wrote:
> ---

Missing sign-off. Or is this intended to be concatenated with the patch you sent earlier?

Show 12 quoted lines
> diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
> index 7398605..3358837 100755
> --- a/t/t4205-log-pretty-formats.sh
> +++ b/t/t4205-log-pretty-formats.sh
> @@ -319,6 +319,18 @@ EOF
>         test_cmp expected actual
>  '
>
> +# Note: Space between 'message' and 'two' should be in the same column as in previous test.
> +test_expect_success 'right alignment formatting at the nth column with --graph. i18n.logOutputEncoding' '
> +       git -c i18n.logOutputEncoding=$test_encoding log --graph --pretty="tformat:%h %>|(40)%s" >actual &&
> +       qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&

You don't seem to be taking advantage of qz_to_tab_space's transliteration of Q to tab and Z to space, so s/qz_to_tab_space/cat/ would make the code clearer.

Show 25 quoted lines
> +* $head1                    message two
> +* $head2                    message one
> +* $head3                        add bar
> +* $head4            $(commit_msg)
> +EOF
> +       test_cmp expected actual
> +'
> +
>  test_expect_success 'right alignment formatting with no padding' '
>         git log --pretty="tformat:%>(1)%s" >actual &&
>         cat <<EOF >expected &&
> @@ -330,6 +342,17 @@ EOF
>         test_cmp expected actual
>  '
>
> +test_expect_success 'right alignment formatting with no padding and with --graph' '
> +       git log --graph --pretty="tformat:%>(1)%s" >actual &&
> +       cat <<EOF >expected &&
> +* message two
> +* message one
> +* add bar
> +* $(commit_msg)
> +EOF
> +       test_cmp expected actual
> +'
Junio C Hamano· Sep 11, 2015, 20:19 UTC · re: Eric Sunshine · lore

Re: [PATCH] Add tests for "Pass graph width to pretty formatting, so '%>|' can work properly"

Eric Sunshine <sunshine@sunshineco.com> writes:
Show 5 quoted lines
>> +       qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
>
> You don't seem to be taking advantage of qz_to_tab_space's
> transliteration of Q to tab and Z to space, so s/qz_to_tab_space/cat/
> would make the code clearer.

Don't cat into a pipe unless you are concatenating multiple files, though ;-).

Josef Kufner· Sep 11, 2015, 20:22 UTC · re: Eric Sunshine · lore

[PATCH] Add tests for "Pass graph width to pretty formatting, so '%>|' can work properly"

Signed-off-by: Josef Kufner <josef@kufner.cz>
---
 t/t4205-log-pretty-formats.sh | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
Show changes to t/t4205-log-pretty-formats.sh +23 −0
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index 7398605..3358837 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -319,6 +319,18 @@ EOF
 	test_cmp expected actual
 '
 
+# Note: Space between 'message' and 'two' should be in the same column as in previous test.
+test_expect_success 'right alignment formatting at the nth column with --graph. i18n.logOutputEncoding' '
+	git -c i18n.logOutputEncoding=$test_encoding log --graph --pretty="tformat:%h %>|(40)%s" >actual &&
+	qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
+* $head1                    message two
+* $head2                    message one
+* $head3                        add bar
+* $head4            $(commit_msg)
+EOF
+	test_cmp expected actual
+'
+
 test_expect_success 'right alignment formatting with no padding' '
 	git log --pretty="tformat:%>(1)%s" >actual &&
 	cat <<EOF >expected &&
@@ -330,6 +342,17 @@ EOF
 	test_cmp expected actual
 '
 
+test_expect_success 'right alignment formatting with no padding and with --graph' '
+	git log --graph --pretty="tformat:%>(1)%s" >actual &&
+	cat <<EOF >expected &&
+* message two
+* message one
+* add bar
+* $(commit_msg)
+EOF
+	test_cmp expected actual
+'
+
 test_expect_success 'right alignment formatting with no padding. i18n.logOutputEncoding' '
 	git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(1)%s" >actual &&
 	cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
-- 
2.5.1
Josef Kufner· Sep 11, 2015, 20:25 UTC · re: Eric Sunshine · lore

Re: [PATCH] Add tests for "Pass graph width to pretty formatting, so '%>|' can work properly"

Eric Sunshine wrote, on 11.9.2015 21:37:
Show 5 quoted lines
> On Fri, Sep 11, 2015 at 1:50 PM, Josef Kufner <josef@kufner.cz> wrote:
>> ---
> 
> Missing sign-off. Or is this intended to be concatenated with the
> patch you sent earlier?
Just forgot to add it. Fixed patch on the way.
Show 16 quoted lines
>> diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
>> index 7398605..3358837 100755
>> --- a/t/t4205-log-pretty-formats.sh
>> +++ b/t/t4205-log-pretty-formats.sh
>> @@ -319,6 +319,18 @@ EOF
>>         test_cmp expected actual
>>  '
>>
>> +# Note: Space between 'message' and 'two' should be in the same column as in previous test.
>> +test_expect_success 'right alignment formatting at the nth column with --graph. i18n.logOutputEncoding' '
>> +       git -c i18n.logOutputEncoding=$test_encoding log --graph --pretty="tformat:%h %>|(40)%s" >actual &&
>> +       qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
> 
> You don't seem to be taking advantage of qz_to_tab_space's
> transliteration of Q to tab and Z to space, so s/qz_to_tab_space/cat/
> would make the code clearer.

I've copied another test which tests the padding without --graph and added it to test the new case. I have no idea what qz_to_tab_space can do. If you wish some clean up, it should be done on older tests too :)

Josef Kufner· Sep 11, 2015, 17:52 UTC · re: Junio C Hamano · lore

Re: [PATCH] Pass graph width to pretty formatting, so '%>|' can work properly

Ok, there are the two tests (see the second e-mail). It should cover two cases where something may go wrong.

Junio C Hamano wrote, on 11.9.2015 18:54:
Show 8 quoted lines
> This "feels" correct ;-), but let me summon Duy who did the %><
> padding at around a5752342 (pretty: support padding placeholders, %<
> %> and %><, 2013-04-19) for an extra set of eyes.
> 
> Care to add a test or two while at it?
> 
> Thanks.  
> 

← back to recent threads