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

[PATCHv2] git-log --format: Add %B tag with %B(x) option

From
Johannes Gilger <heipei@hackvalue.de>
Date
Sep 18, 2009, 18:00 UTC
Message-ID
<1253296845-17219-1-git-send-email-heipei@hackvalue.de>
In-Reply-To
<7vk4zxgnim.fsf@alter.siamese.dyndns.org>

Since one can simply use spaces to indent any other --pretty field we should have an option to do that with the body too.

Also the %B flag strips the trailing newlines, to enable more compact display.

Signed-off-by: Johannes Gilger <heipei@hackvalue.de>
---
Hi again,

I followed Junio's implementation-advice. Since we have two placeholders which take () arguments I put the scanning for those at the top, to avoid repetition. I used strtol in place of atoi and I also had to to add a check for ident > 0 since the indent determines the growth-size of the buffer, and negative values produced nasty stuff (obviously).

As for general extendability: The current code deals with %B(42) as well as %B(42,23[,...]), so even old versions could be used with "new" pretty-formats. %B(c5) is simply no ident, while %B(5c) is 5 spaces indent. Don't know if this is unwanted behaviour, but that's what strtol gives us.

Dscho sent me a pointer to a patch [1], which not only adds indent but also rewrapping. But since this is my second patch and Dscho's patch depended on two other patches I didn't want to get in over my head by making his patches a prerequisite.

A last word on future formats: We can use (x,y,z) easily, another thing one might think of (or at least I do) is using an %an[20] syntax, returning only the first 20 chars of %an, so one can make onelined outputs nicely column-aligned for fields like the author.

Greetings, Jojo

[1] - http://repo.or.cz/w/git/dscho.git?a=commit;h=ad48dfca58169c35e227e135638b4970fe4dc9a5
 Documentation/pretty-formats.txt |    2 ++
 pretty.c                         |   25 ++++++++++++++++++++-----
 2 files changed, 22 insertions(+), 5 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 2a845b1..533bc5e 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -123,6 +123,8 @@ The placeholders are:
 - '%s': subject
 - '%f': sanitized subject line, suitable for a filename
 - '%b': body
+- '%B': body without trailing newline
+- '%B(x)': body indented by x spaces
 - '%Cred': switch color to red
 - '%Cgreen': switch color to green
 - '%Cblue': switch color to blue
diff --git a/pretty.c b/pretty.c
index f5983f8..7b88827 100644
--- a/pretty.c
+++ b/pretty.c
@@ -605,13 +605,17 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 	int h1, h2;
 
 	/* these are independent of the commit */
+
+	const char *body = msg + c->body_off;
+	const char *end = NULL;
+	/* check if we have arguments to the placeholder */
+	if (placeholder[1] == '(')
+		end = strchr(placeholder + 2, ')');
+
 	switch (placeholder[0]) {
 	case 'C':
-		if (placeholder[1] == '(') {
-			const char *end = strchr(placeholder + 2, ')');
+		if (end) {
 			char color[COLOR_MAXLEN];
-			if (!end)
-				return 0;
 			color_parse_mem(placeholder + 2,
 					end - (placeholder + 2),
 					"--pretty format", color);
@@ -733,7 +737,16 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 		format_sanitized_subject(sb, msg + c->subject_off);
 		return 1;
 	case 'b':	/* body */
-		strbuf_addstr(sb, msg + c->body_off);
+		strbuf_addstr(sb, body);
+		return 1;
+	case 'B':	/* body without trailing newline */
+		if (end) {
+			pp_remainder(CMIT_FMT_MEDIUM, &body, sb, strtol(placeholder + 2, NULL, 10));
+			strbuf_rtrim(sb);
+			return end - placeholder + 1;
+		}
+		strbuf_addstr(sb, body);
+		strbuf_rtrim(sb);
 		return 1;
 	}
 	return 0;	/* unknown placeholder */
@@ -875,6 +888,8 @@ void pp_remainder(enum cmit_fmt fmt,
 		}
 		first = 0;
 
+		if (indent < 0)
+			indent = 0;
 		strbuf_grow(sb, linelen + indent + 20);
 		if (indent) {
 			memset(sb->buf + sb->len, ' ', indent);
-- 
1.6.5.rc1.20.geb7d9
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 18 in “git-log --format: Add %B tag with %B(x) option”
  1. git-log --format: Add %B tag with %B(x) optionJohannes Gilger, Sep 17, 2009
  2. Junio C HamanoSep 17, 2009
  3. [PATCHv2] git-log --format: Add %B tag with %B(x) optionJohannes Gilger, Sep 18, 2009
  4. Junio C HamanoSep 18, 2009
  5. [PATCHv3] git-log --format: Add %B tag with %B(x) optionJohannes Gilger, Sep 19, 2009
  6. Junio C HamanoSep 22, 2009
  7. [PATCHv4] git-log --format: Add %B tag with %B(n) optionJohannes Gilger, Sep 22, 2009
  8. 0/3 Add a pretty format to rewrapping/indenting commit messagesJohannes Schindelin, Sep 23, 2009
  9. 1/3 print_wrapped_text(): allow hard newlinesJohannes Schindelin, Sep 23, 2009
  10. 2/3 Add strbuf_add_wrapped_text() to utf8.[ch]Johannes Schindelin, Sep 23, 2009
  11. 3/3 Add "%w" to pretty formats, which rewraps the commit messageJohannes Schindelin, Sep 23, 2009
  12. Johannes GilgerSep 23, 2009
  13. Junio C HamanoSep 23, 2009
  14. Junio C HamanoOct 5, 2009
  15. Linus TorvaldsSep 24, 2009
  16. Johannes SchindelinSep 24, 2009
  17. Johannes SchindelinSep 25, 2009
  18. Junio C HamanoOct 10, 2009

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.