]> granicus.if.org Git - git/commitdiff
pretty: pass graph width to pretty formatting for use in '%>|(N)'
authorJosef Kufner <josef@kufner.cz>
Thu, 16 Jun 2016 13:18:37 +0000 (20:18 +0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Jun 2016 18:43:36 +0000 (11:43 -0700)
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>
commit.h
graph.c
graph.h
log-tree.c
pretty.c
t/t4205-log-pretty-formats.sh

index b06db4d5d9004e329a2a04de3d9af71a3578ef9d..0c923f05f73eacecf38602208c28211acd0b8f9e 100644 (file)
--- a/commit.h
+++ b/commit.h
@@ -161,6 +161,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 1350bdde3be4346e8bc2038c57e4883d0ab12b25..ad766facad65e174b4354ab969cef715ad687470 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -669,6 +669,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 0be62bd8b1227a16b8e67f6309605d6f4a98c247..3f48c19b6208712233f80c1afae3783951e967a9 100644 (file)
--- a/graph.h
+++ b/graph.h
@@ -67,6 +67,11 @@ int graph_is_commit_finished(struct git_graph const *graph);
 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
  */
index 78a5381d0eaebb9092a8bb456fa656462ec65170..8d393150c094f55638e4035e9c6121973d3ce9db 100644 (file)
@@ -687,6 +687,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)
index 87c44971a1d2070e4d256818e96fccc29472ec4a..4f33b09a25620b81042757dd0098cc1e2f6f5ffd 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -1299,6 +1299,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) {
index 7398605e7b894259548f257aaf3e92d9481534eb..d75cdbbf9cce8390d8d98df6e544f40371b25f88 100755 (executable)
@@ -319,6 +319,19 @@ 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 &&
+       iconv -f utf-8 -t $test_encoding >expected <<EOF&&
+* $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 +343,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 &&