git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Josef Kufner <josef@kufner.cz>
Date
Sep 11, 2015, 13:47 UTC
Message-ID
<1441979260-1494-1-git-send-email-josef@kufner.cz>

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(+)
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
Next: Junio C Hamano
Message 1 of 8 in “Pass graph width to pretty formatting, so '%>|' can work properly”
  1. Pass graph width to pretty formatting, so '%>|' can work properlyJosef Kufner, Sep 11, 2015
  2. Junio C HamanoSep 11, 2015
  3. Add tests for "Pass graph width to pretty formatting, so '%>|' can work properly"Josef Kufner, Sep 11, 2015
  4. Eric SunshineSep 11, 2015
  5. Junio C HamanoSep 11, 2015
  6. Add tests for "Pass graph width to pretty formatting, so '%>|' can work properly"Josef Kufner, Sep 11, 2015
  7. Josef KufnerSep 11, 2015
  8. Josef KufnerSep 11, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.