From 9e83266525aad5c6210b9a21be9b1c6996d5544f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 22 Dec 2006 22:06:08 +0100 Subject: commit-tree: encourage UTF-8 commit messages. Introduce is_utf() to check if a text looks like it is encoded in UTF-8, utf8_width() to count display width, and implements print_wrapped_text() using them. git-commit-tree warns if the commit message does not minimally conform to the UTF-8 encoding when i18n.commitencoding is either unset, or set to "utf-8". Signed-off-by: Junio C Hamano --- builtin-commit-tree.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'builtin-commit-tree.c') diff --git a/builtin-commit-tree.c b/builtin-commit-tree.c index bc28770664..f641787988 100644 --- a/builtin-commit-tree.c +++ b/builtin-commit-tree.c @@ -7,6 +7,7 @@ #include "commit.h" #include "tree.h" #include "builtin.h" +#include "utf8.h" #define BLOCKING (1ul << 14) @@ -32,7 +33,7 @@ static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...) len = vsnprintf(one_line, sizeof(one_line), fmt, args); va_end(args); size = *sizep; - newsize = size + len; + newsize = size + len + 1; alloc = (size + 32767) & ~32767; buf = *bufp; if (newsize > alloc) { @@ -40,7 +41,7 @@ static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...) buf = xrealloc(buf, alloc); *bufp = buf; } - *sizep = newsize; + *sizep = newsize - 1; memcpy(buf + size, one_line, len); } @@ -77,6 +78,11 @@ static int new_parent(int idx) return 1; } +static const char commit_utf8_warn[] = +"Warning: commit message does not conform to UTF-8.\n" +"You may want to amend it after fixing the message, or set the config\n" +"variable i18n.commitencoding to the encoding your project uses.\n"; + int cmd_commit_tree(int argc, const char **argv, const char *prefix) { int i; @@ -130,6 +136,11 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix) while (fgets(comment, sizeof(comment), stdin) != NULL) add_buffer(&buffer, &size, "%s", comment); + /* And check the encoding */ + buffer[size] = '\0'; + if (!strcmp(git_commit_encoding, "utf-8") && !is_utf8(buffer)) + fprintf(stderr, commit_utf8_warn); + if (!write_sha1_file(buffer, size, commit_type, commit_sha1)) { printf("%s\n", sha1_to_hex(commit_sha1)); return 0; -- cgit v1.2.3