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

[PATCH 2/3] pretty: factor out format_subject()

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Dec 27, 2008, 00:39 UTC
Message-ID
<1230338375.8363.91.camel@ubuntu.ubuntu-domain>
In-Reply-To
<1230337969.8363.84.camel@ubuntu.ubuntu-domain>
The next patch will use it.

In the version that was factored out, we can't rely on the len of the struct strbuf to find out if a line separator needs to be added, as it might already contain something. Add a guard variable ("first") instead.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 pretty.c |   42 ++++++++++++++++++++++++------------------
 1 files changed, 24 insertions(+), 18 deletions(-)
diff --git a/pretty.c b/pretty.c
index c43497b..632abc5 100644
--- a/pretty.c
+++ b/pretty.c
@@ -495,6 +495,28 @@ static void parse_commit_header(struct format_commit_context *context)
 	context->commit_header_parsed = 1;
 }
 
+static const char *format_subject(struct strbuf *sb, const char *msg,
+				  const char *line_separator)
+{
+	int first = 1;
+
+	for (;;) {
+		const char *line = msg;
+		int linelen = get_one_line(line);
+
+		msg += linelen;
+		if (!linelen || is_empty_line(line, &linelen))
+			break;
+
+		strbuf_grow(sb, linelen + 2);
+		if (!first)
+			strbuf_addstr(sb, line_separator);
+		strbuf_add(sb, line, linelen);
+		first = 0;
+	}
+	return msg;
+}
+
 static void format_decoration(struct strbuf *sb, const struct commit *commit)
 {
 	struct name_decoration *d;
@@ -718,27 +740,11 @@ void pp_title_line(enum cmit_fmt fmt,
 		   const char *encoding,
 		   int need_8bit_cte)
 {
+	const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
 	struct strbuf title;
 
 	strbuf_init(&title, 80);
-
-	for (;;) {
-		const char *line = *msg_p;
-		int linelen = get_one_line(line);
-
-		*msg_p += linelen;
-		if (!linelen || is_empty_line(line, &linelen))
-			break;
-
-		strbuf_grow(&title, linelen + 2);
-		if (title.len) {
-			if (fmt == CMIT_FMT_EMAIL) {
-				strbuf_addch(&title, '\n');
-			}
-			strbuf_addch(&title, ' ');
-		}
-		strbuf_add(&title, line, linelen);
-	}
+	*msg_p = format_subject(&title, *msg_p, line_separator);
 
 	strbuf_grow(sb, title.len + 1024);
 	if (subject) {
-- 
1.6.1
Previous: René ScharfeNext: René Scharfe
Message 2 of 15 in “pretty: factor out skip_empty_lines()”
  1. 1/3 pretty: factor out skip_empty_lines()René Scharfe, Dec 27, 2008
  2. 2/3 pretty: factor out format_subject()René Scharfe, Dec 27, 2008
  3. 3/3 pretty: support multiline subjects with format:René Scharfe, Dec 27, 2008
  4. Markus HeidelbergDec 27, 2008
  5. Nanako ShiraishiDec 28, 2008
  6. Miklos VajnaDec 28, 2008
  7. Markus HeidelbergDec 28, 2008
  8. René ScharfeDec 28, 2008
  9. Markus HeidelbergDec 28, 2008
  10. René ScharfeDec 29, 2008
  11. Junio C HamanoJan 4, 2009
  12. 4/3 shortlog: handle multi-line subjects like log --pretty=oneline et. al. doRené Scharfe, Jan 6, 2009
  13. Junio C HamanoJan 7, 2009
  14. Junio C HamanoDec 28, 2008
  15. Markus HeidelbergDec 28, 2008

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.