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

[PATCH 4/4] Only re-encode certain parts in commit object, not the whole

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Feb 21, 2012, 14:24 UTC
Message-ID
<1329834292-2511-4-git-send-email-pclouds@gmail.com>
In-Reply-To
<1329834292-2511-1-git-send-email-pclouds@gmail.com>

Commit object has its own format, which happens to be in ascii, but not really subject to re-encoding.

There are only four areas that may be re-encoded: author line, committer line, mergetag lines and commit body. Encoding of tags embedded in mergetag lines is not decided by commit encoding, so leave it out and consider it binary.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 pretty.c |   58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 57 insertions(+), 1 deletions(-)
diff --git a/pretty.c b/pretty.c
index 5c433a2..6ccc091 100644
--- a/pretty.c
+++ b/pretty.c
@@ -489,6 +489,62 @@ static char *replace_encoding_header(char *buf, const char *encoding)
 	return strbuf_detach(&tmp, NULL);
 }
 
+/*
+ * Re-encode author, committer and commit body only, leaving the rest
+ * in ascii (or whatever the encoding it is in mergetag lines)
+ * regardless output encoding. We assume the commit is good, so no
+ * validation.
+ */
+static char *reencode_commit(const char *buffer,
+			     const char *out_enc, const char *in_enc)
+{
+	struct strbuf out = STRBUF_INIT;
+	struct strbuf buf = STRBUF_INIT;
+	char *reencoded, *s, *e;
+
+	strbuf_addstr(&buf, buffer);
+
+	s = strstr(buf.buf, "\nauthor ");
+	assert(s != NULL);
+	s += 8;			/* "\nauthor " */
+	strbuf_add(&out, buf.buf, s - buf.buf);
+	e = strchr(s, '\n');
+	*e = '\0';
+	reencoded = reencode_string(s, out_enc, in_enc);
+	if (reencoded && strchr(reencoded, '\n'))
+		die("your chosen encoding produces \\n out of nowhere?");
+	strbuf_addstr(&out, reencoded ? reencoded : s);
+	free(reencoded);
+
+	strbuf_addstr(&out, "\ncommitter ");
+	assert(!strncmp(e + 1, "committer ", 10));
+	s = e + 11;		/* "\ncommitter " */
+	e = strchr(s, '\n');
+	*e = '\0';
+	reencoded = reencode_string(s, out_enc, in_enc);
+	if (reencoded && strchr(reencoded, '\n'))
+		die("your chosen encoding produces \\n out of nowhere?");
+	strbuf_addstr(&out, reencoded ? reencoded : s);
+	free(reencoded);
+	*e = '\n';
+
+	s = e;
+	e = strstr(s, "\n\n");
+	if (e) {
+		e += 2;		/* "\n\n" */
+		strbuf_add(&out, s, e - s);
+
+		s = e;
+		reencoded = reencode_string(s, out_enc, in_enc);
+		strbuf_addstr(&out, reencoded ? reencoded : s);
+		free(reencoded);
+	} else
+		strbuf_addstr(&out, s);
+
+	strbuf_release(&buf);
+	return strbuf_detach(&out, NULL);
+}
+
 char *logmsg_reencode(const struct commit *commit,
 		      const char *output_encoding)
 {
@@ -514,7 +570,7 @@ char *logmsg_reencode(const struct commit *commit,
 		else
 			return NULL; /* nothing to do */
 	else
-		out = reencode_string(commit->buffer,
+		out = reencode_commit(commit->buffer,
 				      output_encoding, use_encoding);
 	if (out)
 		out = replace_encoding_header(out, output_encoding);
-- 
1.7.8.36.g69ee2
Previous: Junio C HamanoNext: Jeff King
Message 9 of 12 in “t3900: add missing UTF-16.txt and mark the test successful”
  1. 1/4 t3900: add missing UTF-16.txt and mark the test successfulNguyễn Thái Ngọc Duy, Feb 21, 2012
  2. 2/4 Do attempt pretty print in ASCII-incompatible encodingsNguyễn Thái Ngọc Duy, Feb 21, 2012
  3. Nguyen Thai Ngoc DuyFeb 21, 2012
  4. Jeff KingFeb 21, 2012
  5. Nguyen Thai Ngoc DuyFeb 22, 2012
  6. Peter KreftingFeb 23, 2012
  7. 3/4 utf8: die if failed to re-encodingNguyễn Thái Ngọc Duy, Feb 21, 2012
  8. Junio C HamanoFeb 21, 2012
  9. 4/4 Only re-encode certain parts in commit object, not the wholeNguyễn Thái Ngọc Duy, Feb 21, 2012
  10. Jeff KingFeb 21, 2012
  11. Nguyen Thai Ngoc DuyFeb 22, 2012
  12. Junio C HamanoFeb 22, 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.