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

[PATCH 7/6] pretty: trim trailing spaces due to padding

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Sep 22, 2012, 04:26 UTC
Message-ID
<1348288000-12485-1-git-send-email-pclouds@gmail.com>
In-Reply-To
<1348287739-12128-1-git-send-email-pclouds@gmail.com>
TODO: should only be enabled explicitly to avoid unexpected trimming
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 And I did not remember I was in interactive rebase mode when I made
 the series. This patch is for discussion only. My screen seems to
 flash (I think) printing trailing spaces.
 pretty.c          |  1 +
 strbuf.c          | 29 +++++++++++++++++++++++++++++
 strbuf.h          |  1 +
 t/t9010-svn-fe.sh |  2 +-
 4 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/pretty.c b/pretty.c
index 6662f52..70f776b 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1327,6 +1327,7 @@ void format_commit_message(const struct commit *commit,
 
 	strbuf_expand(sb, format, format_commit_item, &context);
 	rewrap_message_tail(sb, &context, 0, 0, 0);
+	delete_trailing_whitespace(sb);
 
 	if (context.message != commit->buffer)
 		free(context.message);
diff --git a/strbuf.c b/strbuf.c
index 0510f76..e001175 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -488,3 +488,32 @@ int fprintf_ln(FILE *fp, const char *fmt, ...)
 		return -1;
 	return ret + 1;
 }
+
+void delete_trailing_whitespace(struct strbuf *sb)
+{
+	char *src = sb->buf, *dst = sb->buf;
+	char *end = src + sb->len, *anchor = NULL;
+	while (src < end) {
+		if (*src == ' ') {
+			if (anchor)
+				src++;
+			else
+				anchor = src++;
+		} else if (*src == '\n') {
+			if (anchor) {
+				*dst++ = *src++;
+				anchor = NULL;
+			} else
+				*dst++ = *src++;
+		} else {
+			if (anchor) {
+				memcpy(dst, anchor, src - anchor);
+				dst += src - anchor;
+				anchor = NULL;
+			}
+			*dst++ = *src++;
+		}
+	}
+	sb->len = dst - sb->buf;
+	sb->buf[sb->len] = '\0';
+}
diff --git a/strbuf.h b/strbuf.h
index be941ee..75a8908 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -42,6 +42,7 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
 extern void strbuf_trim(struct strbuf *);
 extern void strbuf_rtrim(struct strbuf *);
 extern void strbuf_ltrim(struct strbuf *);
+extern void delete_trailing_whitespace(struct strbuf *);
 extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
 
 extern struct strbuf **strbuf_split_buf(const char *, size_t,
diff --git a/t/t9010-svn-fe.sh b/t/t9010-svn-fe.sh
index b7eed24..b603e70 100755
--- a/t/t9010-svn-fe.sh
+++ b/t/t9010-svn-fe.sh
@@ -96,7 +96,7 @@ test_expect_failure PIPE 'empty revision' '
 
 test_expect_success PIPE 'empty properties' '
 	reinit_git &&
-	printf "rev <nobody, nobody@local>: %s\n" "" "" >expect &&
+	printf "rev <nobody, nobody@local>: %s\n" "" "" | sed "s/ *$//" >expect &&
 	cat >emptyprop.dump <<-\EOF &&
 	SVN-fs-dump-format-version: 3
 
-- 
1.7.12.1.384.g7b808e7
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 8 of 21 in “Advanced --oneline layout”
  1. 0/6 Advanced --oneline layoutNguyễn Thái Ngọc Duy, Sep 22, 2012
  2. 1/6 pretty: share code between format_decoration and show_decorationsNguyễn Thái Ngọc Duy, Sep 22, 2012
  3. 2/6 pretty: split parsing %C into a separate functionNguyễn Thái Ngọc Duy, Sep 22, 2012
  4. 3/6 pretty: support %C(auto[,N]) to turn on coloring on next placeholder(s)Nguyễn Thái Ngọc Duy, Sep 22, 2012
  5. 4/6 utf8.c: move display_mode_esc_sequence_len() for use by other functionsNguyễn Thái Ngọc Duy, Sep 22, 2012
  6. 5/6 utf8.c: add utf8_strnwidth() with the ability to skip ansi sequencesNguyễn Thái Ngọc Duy, Sep 22, 2012
  7. 6/6 pretty: support padding placeholders, %< %> and %<>Nguyễn Thái Ngọc Duy, Sep 22, 2012
  8. 7/6 pretty: trim trailing spaces due to paddingNguyễn Thái Ngọc Duy, Sep 22, 2012
  9. 0/9 Advanced --oneline layoutNguyễn Thái Ngọc Duy, Sep 23, 2012
  10. 1/9 pretty: share code between format_decoration and show_decorationsNguyễn Thái Ngọc Duy, Sep 23, 2012
  11. 2/9 pretty: split parsing %C into a separate functionNguyễn Thái Ngọc Duy, Sep 23, 2012
  12. 3/9 pretty: support %C(auto[,N]) to turn on coloring on next placeholder(s)Nguyễn Thái Ngọc Duy, Sep 23, 2012
  13. 4/9 utf8.c: move display_mode_esc_sequence_len() for use by other functionsNguyễn Thái Ngọc Duy, Sep 23, 2012
  14. 5/9 utf8.c: add utf8_strnwidth() with the ability to skip ansi sequencesNguyễn Thái Ngọc Duy, Sep 23, 2012
  15. 6/9 pretty: two phase conversion for non utf-8 commitsNguyễn Thái Ngọc Duy, Sep 23, 2012
  16. Robin RosenbergSep 23, 2012
  17. Nguyen Thai Ngoc DuySep 24, 2012
  18. 7/9 pretty: support padding placeholders, %< %> and %><Nguyễn Thái Ngọc Duy, Sep 23, 2012
  19. Jeff KingOct 24, 2012
  20. 8/9 pretty: support truncating in %>, %< and %><Nguyễn Thái Ngọc Duy, Sep 23, 2012
  21. 9/9 pretty: support %>> that steal trailing spacesNguyễn Thái Ngọc Duy, Sep 23, 2012

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.