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 <junkio@cox.net>
This commit is contained in:
Johannes Schindelin
2006-12-22 22:06:08 +01:00
committed by Junio C Hamano
parent 8e554429e8
commit 9e83266525
4 changed files with 303 additions and 4 deletions

View File

@ -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;