From e0335fcdad28da578904bb33fcf1dfbcdf172599 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Fri, 25 Feb 2011 23:09:41 -0600 Subject: [PATCH 1/9] wt-status: add helpers for printing wt-status lines Introduce status_printf{,_ln,_more} wrapper functions around color_vfprintf() which take care of adding "#" to the beginning of status lines automatically. The semantics: - status_printf() is just like color_fprintf() but it adds a "# " at the beginning of each line of output; - status_printf_ln() is a convenience function that additionally adds "\n" at the end; - status_printf_more() is a variant of status_printf() used to continue lines that have already started. It suppresses the "#" at the beginning of the first line. Helped-by: Junio C Hamano Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- color.c | 9 +++++++ color.h | 3 +++ wt-status.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ wt-status.h | 7 +++++ 4 files changed, 93 insertions(+) diff --git a/color.c b/color.c index 6a5a54ec66..417cf8fb28 100644 --- a/color.c +++ b/color.c @@ -175,6 +175,15 @@ int git_color_default_config(const char *var, const char *value, void *cb) return git_default_config(var, value, cb); } +void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb) +{ + if (*color) + fprintf(fp, "%s", color); + fprintf(fp, "%s", sb->buf); + if (*color) + fprintf(fp, "%s", GIT_COLOR_RESET); +} + static int color_vfprintf(FILE *fp, const char *color, const char *fmt, va_list args, const char *trail) { diff --git a/color.h b/color.h index 170ff4074d..c0528cf087 100644 --- a/color.h +++ b/color.h @@ -1,6 +1,8 @@ #ifndef COLOR_H #define COLOR_H +struct strbuf; + /* 2 + (2 * num_attrs) + 8 + 1 + 8 + 'm' + NUL */ /* "\033[1;2;4;5;7;38;5;2xx;48;5;2xxm\0" */ /* @@ -64,6 +66,7 @@ __attribute__((format (printf, 3, 4))) int color_fprintf(FILE *fp, const char *color, const char *fmt, ...); __attribute__((format (printf, 3, 4))) int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...); +void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb); int color_is_nil(const char *color); diff --git a/wt-status.c b/wt-status.c index 123582b6cb..1abd7c3382 100644 --- a/wt-status.c +++ b/wt-status.c @@ -32,6 +32,80 @@ static const char *color(int slot, struct wt_status *s) return c; } +static void status_vprintf(struct wt_status *s, int at_bol, const char *color, + const char *fmt, va_list ap, const char *trail) +{ + struct strbuf sb = STRBUF_INIT; + struct strbuf linebuf = STRBUF_INIT; + const char *line, *eol; + + strbuf_vaddf(&sb, fmt, ap); + if (!sb.len) { + strbuf_addch(&sb, '#'); + if (!trail) + strbuf_addch(&sb, ' '); + color_print_strbuf(s->fp, color, &sb); + if (trail) + fprintf(s->fp, "%s", trail); + strbuf_release(&sb); + return; + } + for (line = sb.buf; *line; line = eol + 1) { + eol = strchr(line, '\n'); + + strbuf_reset(&linebuf); + if (at_bol) { + strbuf_addch(&linebuf, '#'); + if (*line != '\n' && *line != '\t') + strbuf_addch(&linebuf, ' '); + } + if (eol) + strbuf_add(&linebuf, line, eol - line); + else + strbuf_addstr(&linebuf, line); + color_print_strbuf(s->fp, color, &linebuf); + if (eol) + fprintf(s->fp, "\n"); + else + break; + at_bol = 1; + } + if (trail) + fprintf(s->fp, "%s", trail); + strbuf_release(&linebuf); + strbuf_release(&sb); +} + +void status_printf_ln(struct wt_status *s, const char *color, + const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + status_vprintf(s, 1, color, fmt, ap, "\n"); + va_end(ap); +} + +void status_printf(struct wt_status *s, const char *color, + const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + status_vprintf(s, 1, color, fmt, ap, NULL); + va_end(ap); +} + +void status_printf_more(struct wt_status *s, const char *color, + const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + status_vprintf(s, 0, color, fmt, ap, NULL); + va_end(ap); +} + void wt_status_prepare(struct wt_status *s) { unsigned char sha1[20]; diff --git a/wt-status.h b/wt-status.h index 20b17cf439..595c5fa137 100644 --- a/wt-status.h +++ b/wt-status.h @@ -68,4 +68,11 @@ void wt_status_collect(struct wt_status *s); void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch); void wt_porcelain_print(struct wt_status *s, int null_termination); +void status_printf_ln(struct wt_status *s, const char *color, const char *fmt, ...) + ; +void status_printf(struct wt_status *s, const char *color, const char *fmt, ...) + ; +void status_printf_more(struct wt_status *s, const char *color, const char *fmt, ...) + __attribute__((format(printf, 3, 4))); + #endif /* STATUS_H */ From 3c624a30fa3f047d401e08cf019d39b3a79095ea Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Fri, 25 Feb 2011 23:10:49 -0600 Subject: [PATCH 2/9] commit: refer to commit template as s->fp Instead of maintaining a local variable for it, use s->fp to keep track of where the commit message template should be written. This prepares us to take advantage of the status_printf functions, which use a struct wt_status instead of a FILE pointer to determine where to send their output. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- builtin/commit.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index d7f55e3d46..ef5f0b24cb 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -568,7 +568,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix, int commitable, saved_color_setting; struct strbuf sb = STRBUF_INIT; char *buffer; - FILE *fp; const char *hook_arg1 = NULL; const char *hook_arg2 = NULL; int ident_shown = 0; @@ -657,8 +656,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix, hook_arg2 = ""; } - fp = fopen(git_path(commit_editmsg), "w"); - if (fp == NULL) + s->fp = fopen(git_path(commit_editmsg), "w"); + if (s->fp == NULL) die_errno("could not open '%s'", git_path(commit_editmsg)); if (cleanup_mode != CLEANUP_NONE) @@ -682,7 +681,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, strbuf_release(&sob); } - if (fwrite(sb.buf, 1, sb.len, fp) < sb.len) + if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len) die_errno("could not write commit template"); strbuf_release(&sb); @@ -695,7 +694,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, if (use_editor && include_status) { char *ai_tmp, *ci_tmp; if (in_merge) - fprintf(fp, + fprintf(s->fp, "#\n" "# It looks like you may be committing a MERGE.\n" "# If this is not correct, please remove the file\n" @@ -704,45 +703,45 @@ static int prepare_to_commit(const char *index_file, const char *prefix, "#\n", git_path("MERGE_HEAD")); - fprintf(fp, + fprintf(s->fp, "\n" "# Please enter the commit message for your changes."); if (cleanup_mode == CLEANUP_ALL) - fprintf(fp, + fprintf(s->fp, " Lines starting\n" "# with '#' will be ignored, and an empty" " message aborts the commit.\n"); else /* CLEANUP_SPACE, that is. */ - fprintf(fp, + fprintf(s->fp, " Lines starting\n" "# with '#' will be kept; you may remove them" " yourself if you want to.\n" "# An empty message aborts the commit.\n"); if (only_include_assumed) - fprintf(fp, "# %s\n", only_include_assumed); + fprintf(s->fp, "# %s\n", only_include_assumed); ai_tmp = cut_ident_timestamp_part(author_ident->buf); ci_tmp = cut_ident_timestamp_part(committer_ident.buf); if (strcmp(author_ident->buf, committer_ident.buf)) - fprintf(fp, + fprintf(s->fp, "%s" "# Author: %s\n", ident_shown++ ? "" : "#\n", author_ident->buf); if (!user_ident_sufficiently_given()) - fprintf(fp, + fprintf(s->fp, "%s" "# Committer: %s\n", ident_shown++ ? "" : "#\n", committer_ident.buf); if (ident_shown) - fprintf(fp, "#\n"); + fprintf(s->fp, "#\n"); saved_color_setting = s->use_color; s->use_color = 0; - commitable = run_status(fp, index_file, prefix, 1, s); + commitable = run_status(s->fp, index_file, prefix, 1, s); s->use_color = saved_color_setting; *ai_tmp = ' '; @@ -764,7 +763,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, } strbuf_release(&committer_ident); - fclose(fp); + fclose(s->fp); if (!commitable && !in_merge && !allow_empty && !(amend && is_a_merge(head_sha1))) { From 098d0e0e8e7467bea5306d5bc98b283bfe0174fb Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Fri, 25 Feb 2011 23:11:37 -0600 Subject: [PATCH 3/9] commit, status: use status_printf{,_ln,_more} helpers wt-status code is used to provide a reminder of changes included and not included for the commit message template opened in the operator's text editor by "git commit". Therefore each line of its output begins with the comment character "#": # Please enter the commit message for your changes. Lines starting Use the new status_printf{,_ln,_more} functions to take care of adding "#" to the beginning of such status lines automatically. Using these will have two advantages over the current code: - The obvious one is to force separation of the "#" from the translatable part of the message when git learns to translate its output. - Another advantage is that this makes it easier for us to drop "#" prefix in "git status" output in later versions of git if we want to. Explained-by: Junio C Hamano Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- builtin/commit.c | 47 +++++++++++++------------- wt-status.c | 86 ++++++++++++++++++++++++------------------------ 2 files changed, 67 insertions(+), 66 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index ef5f0b24cb..ae62a25f52 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -694,50 +694,51 @@ static int prepare_to_commit(const char *index_file, const char *prefix, if (use_editor && include_status) { char *ai_tmp, *ci_tmp; if (in_merge) - fprintf(s->fp, - "#\n" - "# It looks like you may be committing a MERGE.\n" - "# If this is not correct, please remove the file\n" - "# %s\n" - "# and try again.\n" - "#\n", + status_printf_ln(s, GIT_COLOR_NORMAL, + "\n" + "It looks like you may be committing a MERGE.\n" + "If this is not correct, please remove the file\n" + " %s\n" + "and try again.\n" + "", git_path("MERGE_HEAD")); - fprintf(s->fp, - "\n" - "# Please enter the commit message for your changes."); + fprintf(s->fp, "\n"); + status_printf(s, GIT_COLOR_NORMAL, + "Please enter the commit message for your changes."); if (cleanup_mode == CLEANUP_ALL) - fprintf(s->fp, + status_printf_more(s, GIT_COLOR_NORMAL, " Lines starting\n" - "# with '#' will be ignored, and an empty" + "with '#' will be ignored, and an empty" " message aborts the commit.\n"); else /* CLEANUP_SPACE, that is. */ - fprintf(s->fp, + status_printf_more(s, GIT_COLOR_NORMAL, " Lines starting\n" - "# with '#' will be kept; you may remove them" + "with '#' will be kept; you may remove them" " yourself if you want to.\n" - "# An empty message aborts the commit.\n"); + "An empty message aborts the commit.\n"); if (only_include_assumed) - fprintf(s->fp, "# %s\n", only_include_assumed); + status_printf_ln(s, GIT_COLOR_NORMAL, + "%s", only_include_assumed); ai_tmp = cut_ident_timestamp_part(author_ident->buf); ci_tmp = cut_ident_timestamp_part(committer_ident.buf); if (strcmp(author_ident->buf, committer_ident.buf)) - fprintf(s->fp, + status_printf_ln(s, GIT_COLOR_NORMAL, "%s" - "# Author: %s\n", - ident_shown++ ? "" : "#\n", + "Author: %s", + ident_shown++ ? "" : "\n", author_ident->buf); if (!user_ident_sufficiently_given()) - fprintf(s->fp, + status_printf_ln(s, GIT_COLOR_NORMAL, "%s" - "# Committer: %s\n", - ident_shown++ ? "" : "#\n", + "Committer: %s", + ident_shown++ ? "" : "\n", committer_ident.buf); if (ident_shown) - fprintf(s->fp, "#\n"); + status_printf_ln(s, GIT_COLOR_NORMAL, ""); saved_color_setting = s->use_color; s->use_color = 0; diff --git a/wt-status.c b/wt-status.c index 1abd7c3382..c14cbe48d1 100644 --- a/wt-status.c +++ b/wt-status.c @@ -131,33 +131,33 @@ static void wt_status_print_unmerged_header(struct wt_status *s) { const char *c = color(WT_STATUS_HEADER, s); - color_fprintf_ln(s->fp, c, "# Unmerged paths:"); + status_printf_ln(s, c, "Unmerged paths:"); if (!advice_status_hints) return; if (s->in_merge) ; else if (!s->is_initial) - color_fprintf_ln(s->fp, c, "# (use \"git reset %s ...\" to unstage)", s->reference); + status_printf_ln(s, c, " (use \"git reset %s ...\" to unstage)", s->reference); else - color_fprintf_ln(s->fp, c, "# (use \"git rm --cached ...\" to unstage)"); - color_fprintf_ln(s->fp, c, "# (use \"git add/rm ...\" as appropriate to mark resolution)"); - color_fprintf_ln(s->fp, c, "#"); + status_printf_ln(s, c, " (use \"git rm --cached ...\" to unstage)"); + status_printf_ln(s, c, " (use \"git add/rm ...\" as appropriate to mark resolution)"); + status_printf_ln(s, c, ""); } static void wt_status_print_cached_header(struct wt_status *s) { const char *c = color(WT_STATUS_HEADER, s); - color_fprintf_ln(s->fp, c, "# Changes to be committed:"); + status_printf_ln(s, c, "Changes to be committed:"); if (!advice_status_hints) return; if (s->in_merge) ; /* NEEDSWORK: use "git reset --unresolve"??? */ else if (!s->is_initial) - color_fprintf_ln(s->fp, c, "# (use \"git reset %s ...\" to unstage)", s->reference); + status_printf_ln(s, c, " (use \"git reset %s ...\" to unstage)", s->reference); else - color_fprintf_ln(s->fp, c, "# (use \"git rm --cached ...\" to unstage)"); - color_fprintf_ln(s->fp, c, "#"); + status_printf_ln(s, c, " (use \"git rm --cached ...\" to unstage)"); + status_printf_ln(s, c, ""); } static void wt_status_print_dirty_header(struct wt_status *s, @@ -166,17 +166,17 @@ static void wt_status_print_dirty_header(struct wt_status *s, { const char *c = color(WT_STATUS_HEADER, s); - color_fprintf_ln(s->fp, c, "# Changes not staged for commit:"); + status_printf_ln(s, c, "Changes not staged for commit:"); if (!advice_status_hints) return; if (!has_deleted) - color_fprintf_ln(s->fp, c, "# (use \"git add ...\" to update what will be committed)"); + status_printf_ln(s, c, " (use \"git add ...\" to update what will be committed)"); else - color_fprintf_ln(s->fp, c, "# (use \"git add/rm ...\" to update what will be committed)"); - color_fprintf_ln(s->fp, c, "# (use \"git checkout -- ...\" to discard changes in working directory)"); + status_printf_ln(s, c, " (use \"git add/rm ...\" to update what will be committed)"); + status_printf_ln(s, c, " (use \"git checkout -- ...\" to discard changes in working directory)"); if (has_dirty_submodules) - color_fprintf_ln(s->fp, c, "# (commit or discard the untracked or modified content in submodules)"); - color_fprintf_ln(s->fp, c, "#"); + status_printf_ln(s, c, " (commit or discard the untracked or modified content in submodules)"); + status_printf_ln(s, c, ""); } static void wt_status_print_other_header(struct wt_status *s, @@ -184,16 +184,16 @@ static void wt_status_print_other_header(struct wt_status *s, const char *how) { const char *c = color(WT_STATUS_HEADER, s); - color_fprintf_ln(s->fp, c, "# %s files:", what); + status_printf_ln(s, c, "%s files:", what); if (!advice_status_hints) return; - color_fprintf_ln(s->fp, c, "# (use \"git %s ...\" to include in what will be committed)", how); - color_fprintf_ln(s->fp, c, "#"); + status_printf_ln(s, c, " (use \"git %s ...\" to include in what will be committed)", how); + status_printf_ln(s, c, ""); } static void wt_status_print_trailer(struct wt_status *s) { - color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); + status_printf_ln(s, color(WT_STATUS_HEADER, s), ""); } #define quote_path quote_path_relative @@ -207,7 +207,7 @@ static void wt_status_print_unmerged_data(struct wt_status *s, const char *one, *how = "bug"; one = quote_path(it->string, -1, &onebuf, s->prefix); - color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t"); + status_printf(s, color(WT_STATUS_HEADER, s), "\t"); switch (d->stagemask) { case 1: how = "both deleted:"; break; case 2: how = "added by us:"; break; @@ -217,7 +217,7 @@ static void wt_status_print_unmerged_data(struct wt_status *s, case 6: how = "both added:"; break; case 7: how = "both modified:"; break; } - color_fprintf(s->fp, c, "%-20s%s\n", how, one); + status_printf_more(s, c, "%-20s%s\n", how, one); strbuf_release(&onebuf); } @@ -260,40 +260,40 @@ static void wt_status_print_change_data(struct wt_status *s, one = quote_path(one_name, -1, &onebuf, s->prefix); two = quote_path(two_name, -1, &twobuf, s->prefix); - color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t"); + status_printf(s, color(WT_STATUS_HEADER, s), "\t"); switch (status) { case DIFF_STATUS_ADDED: - color_fprintf(s->fp, c, "new file: %s", one); + status_printf_more(s, c, "new file: %s", one); break; case DIFF_STATUS_COPIED: - color_fprintf(s->fp, c, "copied: %s -> %s", one, two); + status_printf_more(s, c, "copied: %s -> %s", one, two); break; case DIFF_STATUS_DELETED: - color_fprintf(s->fp, c, "deleted: %s", one); + status_printf_more(s, c, "deleted: %s", one); break; case DIFF_STATUS_MODIFIED: - color_fprintf(s->fp, c, "modified: %s", one); + status_printf_more(s, c, "modified: %s", one); break; case DIFF_STATUS_RENAMED: - color_fprintf(s->fp, c, "renamed: %s -> %s", one, two); + status_printf_more(s, c, "renamed: %s -> %s", one, two); break; case DIFF_STATUS_TYPE_CHANGED: - color_fprintf(s->fp, c, "typechange: %s", one); + status_printf_more(s, c, "typechange: %s", one); break; case DIFF_STATUS_UNKNOWN: - color_fprintf(s->fp, c, "unknown: %s", one); + status_printf_more(s, c, "unknown: %s", one); break; case DIFF_STATUS_UNMERGED: - color_fprintf(s->fp, c, "unmerged: %s", one); + status_printf_more(s, c, "unmerged: %s", one); break; default: die("bug: unhandled diff status %c", status); } if (extra.len) { - color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "%s", extra.buf); + status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf); strbuf_release(&extra); } - fprintf(s->fp, "\n"); + status_printf_more(s, GIT_COLOR_NORMAL, "\n"); strbuf_release(&onebuf); strbuf_release(&twobuf); } @@ -647,9 +647,9 @@ static void wt_status_print_other(struct wt_status *s, for (i = 0; i < l->nr; i++) { struct string_list_item *it; it = &(l->items[i]); - color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t"); - color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", - quote_path(it->string, strlen(it->string), + status_printf(s, color(WT_STATUS_HEADER, s), "\t"); + status_printf_more(s, color(WT_STATUS_UNTRACKED, s), + "%s\n", quote_path(it->string, strlen(it->string), &buf, s->prefix)); } strbuf_release(&buf); @@ -716,17 +716,17 @@ void wt_status_print(struct wt_status *s) branch_status_color = color(WT_STATUS_NOBRANCH, s); on_what = "Not currently on any branch."; } - color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# "); - color_fprintf(s->fp, branch_status_color, "%s", on_what); - color_fprintf_ln(s->fp, branch_color, "%s", branch_name); + status_printf(s, color(WT_STATUS_HEADER, s), ""); + status_printf_more(s, branch_status_color, "%s", on_what); + status_printf_more(s, branch_color, "%s\n", branch_name); if (!s->is_initial) wt_status_print_tracking(s); } if (s->is_initial) { - color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); - color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit"); - color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); + status_printf_ln(s, color(WT_STATUS_HEADER, s), ""); + status_printf_ln(s, color(WT_STATUS_HEADER, s), "Initial commit"); + status_printf_ln(s, color(WT_STATUS_HEADER, s), ""); } wt_status_print_updated(s); @@ -743,7 +743,7 @@ void wt_status_print(struct wt_status *s) if (s->show_ignored_files) wt_status_print_other(s, &s->ignored, "Ignored", "add -f"); } else if (s->commitable) - fprintf(s->fp, "# Untracked files not listed%s\n", + status_printf_ln(s, GIT_COLOR_NORMAL, "Untracked files not listed%s", advice_status_hints ? " (use -u option to show untracked files)" : ""); @@ -751,7 +751,7 @@ void wt_status_print(struct wt_status *s) wt_status_print_verbose(s); if (!s->commitable) { if (s->amend) - fprintf(s->fp, "# No changes\n"); + status_printf_ln(s, GIT_COLOR_NORMAL, "No changes"); else if (s->nowarn) ; /* nothing */ else if (s->workdir_dirty) From 6578483036695820d05aa7cf482a38169ad321bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 22 Feb 2011 23:41:20 +0000 Subject: [PATCH 4/9] i18n: add no-op _() and N_() wrappers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The _ function is for translating strings into the user's chosen language. The N_ macro just marks translatable strings for the xgettext(1) tool without translating them; it is intended for use in contexts where a function call cannot be used. So, for example: fprintf(stderr, _("Expansion of alias '%s' failed; " "'%s' is not a git command\n"), cmd, argv[0]); and const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { /* ERROR_WOULD_OVERWRITE */ N_("Entry '%s' would be overwritten by merge. Cannot merge."), [...] Define such _ and N_ in a new gettext.h and include it in cache.h, so they can be used everywhere. Each just returns its argument for now. _ is a function rather than a macro like N_ to avoid the temptation to use _("foo") as a string literal (which would be a compile-time error once _(s) expands to an expression for the translation of s). Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- Makefile | 1 + cache.h | 1 + gettext.h | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 gettext.h diff --git a/Makefile b/Makefile index ade79232f4..c153f450c5 100644 --- a/Makefile +++ b/Makefile @@ -515,6 +515,7 @@ LIB_H += diff.h LIB_H += dir.h LIB_H += exec_cmd.h LIB_H += fsck.h +LIB_H += gettext.h LIB_H += git-compat-util.h LIB_H += graph.h LIB_H += grep.h diff --git a/cache.h b/cache.h index 4a758babec..8188169ac6 100644 --- a/cache.h +++ b/cache.h @@ -5,6 +5,7 @@ #include "strbuf.h" #include "hash.h" #include "advice.h" +#include "gettext.h" #include SHA1_HEADER #ifndef git_SHA_CTX diff --git a/gettext.h b/gettext.h new file mode 100644 index 0000000000..6949d736d5 --- /dev/null +++ b/gettext.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2010-2011 Ævar Arnfjörð Bjarmason + * + * This is a skeleton no-op implementation of gettext for Git. + * You can replace it with something that uses libintl.h and wraps + * gettext() to try out the translations. + */ + +#ifndef GETTEXT_H +#define GETTEXT_H + +#ifdef _ +#error "namespace conflict: '_' is pre-defined?" +#endif + +#define FORMAT_PRESERVING(n) __attribute__((format_arg(n))) + +static inline FORMAT_PRESERVING(1) const char *_(const char *msgid) +{ + return msgid; +} + +/* Mark msgid for translation but do not translate it. */ +#define N_(msgid) (msgid) + +#endif From bb946bba761288e24b3eb621a3782a4fa804f21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 22 Feb 2011 23:41:21 +0000 Subject: [PATCH 5/9] i18n: add GETTEXT_POISON to simulate unfriendly translator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new GETTEXT_POISON compile-time parameter to make _(msg) always return gibberish. So now you can run make GETTEXT_POISON=YesPlease to get a copy of git that functions correctly (one hopes) but produces output that is in nobody's native language at all. This is a debugging aid for people who are working on the i18n part of the system, to make sure that they are not marking plumbing messages that should never be translated with _(). As new strings get marked for translation, naturally a number of tests will be broken in this mode. Tests that depend on output from Porcelain will need to be marked with the new C_LOCALE_OUTPUT test prerequisite. Newly failing tests that do not depend on output from Porcelain would be bugs due to messages that should not have been marked for translation. Note that the string we're using ("# GETTEXT POISON #") intentionally starts the pound sign. Some of Git's tests such as t3404-rebase-interactive.sh rely on interactive editing with a fake editor, and will needlessly break if the message doesn't start with something the interactive editor considers a comment. A future patch will fix fix the underlying cause of that issue by adding "#" characters to the commit advice automatically. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- Makefile | 7 +++++++ gettext.h | 8 +++++++- t/test-lib.sh | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c153f450c5..c348bb7337 100644 --- a/Makefile +++ b/Makefile @@ -216,6 +216,9 @@ all:: # # Define NO_REGEX if you have no or inferior regex support in your C library. # +# Define GETTEXT_POISON if you are debugging the choice of strings marked +# for translation. This will turn all strings that use gettext into gibberish. +# # Define JSMIN to point to JavaScript minifier that functions as # a filter to have gitweb.js minified. # @@ -1370,6 +1373,9 @@ endif ifdef NO_SYMLINK_HEAD BASIC_CFLAGS += -DNO_SYMLINK_HEAD endif +ifdef GETTEXT_POISON + BASIC_CFLAGS += -DGETTEXT_POISON +endif ifdef NO_STRCASESTR COMPAT_CFLAGS += -DNO_STRCASESTR COMPAT_OBJS += compat/strcasestr.o @@ -2089,6 +2095,7 @@ endif ifdef GIT_TEST_CMP_USE_COPIED_CONTEXT @echo GIT_TEST_CMP_USE_COPIED_CONTEXT=YesPlease >>$@ endif + @echo GETTEXT_POISON=\''$(subst ','\'',$(subst ','\'',$(GETTEXT_POISON)))'\' >>$@ ### Detect Tck/Tk interpreter path changes ifndef NO_TCLTK diff --git a/gettext.h b/gettext.h index 6949d736d5..11d82b0a6e 100644 --- a/gettext.h +++ b/gettext.h @@ -15,9 +15,15 @@ #define FORMAT_PRESERVING(n) __attribute__((format_arg(n))) +#ifdef GETTEXT_POISON +#define use_gettext_poison() 1 +#else +#define use_gettext_poison() 0 +#endif + static inline FORMAT_PRESERVING(1) const char *_(const char *msgid) { - return msgid; + return use_gettext_poison() ? "# GETTEXT POISON #" : msgid; } /* Mark msgid for translation but do not translate it. */ diff --git a/t/test-lib.sh b/t/test-lib.sh index 0fdc541a7c..0840e4a5c9 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -1079,6 +1079,9 @@ esac test -z "$NO_PERL" && test_set_prereq PERL test -z "$NO_PYTHON" && test_set_prereq PYTHON +# Can we rely on git's output in the C locale? +test -z "$GETTEXT_POISON" && test_set_prereq C_LOCALE_OUTPUT + # test whether the filesystem supports symbolic links ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS rm -f y From 309552295af4b0f2ddd1af15d919441b1744523a Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Tue, 22 Feb 2011 23:41:22 +0000 Subject: [PATCH 6/9] i18n: do not poison translations unless GIT_GETTEXT_POISON envvar is set Tweak the GETTEXT_POISON facility so it is activated at run time instead of compile time. If the GIT_GETTEXT_POISON environment variable is set, _(msg) will result in gibberish as before; but if the GIT_GETTEXT_POISON variable is not set, it will return the message for human-readable output. So the behavior of mistranslated and untranslated git can be compared without rebuilding git in between. For simplicity we always set the GIT_GETTEXT_POISON variable in tests. This does not affect builds without the GETTEXT_POISON compile-time option set, so non-i18n git will not be slowed down. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- Makefile | 5 ++++- gettext.c | 14 ++++++++++++++ gettext.h | 2 +- t/test-lib.sh | 8 +++++++- 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 gettext.c diff --git a/Makefile b/Makefile index c348bb7337..4e9d935828 100644 --- a/Makefile +++ b/Makefile @@ -217,7 +217,9 @@ all:: # Define NO_REGEX if you have no or inferior regex support in your C library. # # Define GETTEXT_POISON if you are debugging the choice of strings marked -# for translation. This will turn all strings that use gettext into gibberish. +# for translation. In a GETTEXT_POISON build, you can turn all strings marked +# for translation into gibberish by setting the GIT_GETTEXT_POISON variable +# (to any value) in your environment. # # Define JSMIN to point to JavaScript minifier that functions as # a filter to have gitweb.js minified. @@ -1374,6 +1376,7 @@ ifdef NO_SYMLINK_HEAD BASIC_CFLAGS += -DNO_SYMLINK_HEAD endif ifdef GETTEXT_POISON + LIB_OBJS += gettext.o BASIC_CFLAGS += -DGETTEXT_POISON endif ifdef NO_STRCASESTR diff --git a/gettext.c b/gettext.c new file mode 100644 index 0000000000..ae5394a496 --- /dev/null +++ b/gettext.c @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2010 Ævar Arnfjörð Bjarmason + */ + +#include "git-compat-util.h" +#include "gettext.h" + +int use_gettext_poison(void) +{ + static int poison_requested = -1; + if (poison_requested == -1) + poison_requested = getenv("GIT_GETTEXT_POISON") ? 1 : 0; + return poison_requested; +} diff --git a/gettext.h b/gettext.h index 11d82b0a6e..04b5958e2b 100644 --- a/gettext.h +++ b/gettext.h @@ -16,7 +16,7 @@ #define FORMAT_PRESERVING(n) __attribute__((format_arg(n))) #ifdef GETTEXT_POISON -#define use_gettext_poison() 1 +extern int use_gettext_poison(void); #else #define use_gettext_poison() 0 #endif diff --git a/t/test-lib.sh b/t/test-lib.sh index 0840e4a5c9..f4c1e04e4f 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -1080,7 +1080,13 @@ test -z "$NO_PERL" && test_set_prereq PERL test -z "$NO_PYTHON" && test_set_prereq PYTHON # Can we rely on git's output in the C locale? -test -z "$GETTEXT_POISON" && test_set_prereq C_LOCALE_OUTPUT +if test -n "$GETTEXT_POISON" +then + GIT_GETTEXT_POISON=YesPlease + export GIT_GETTEXT_POISON +else + test_set_prereq C_LOCALE_OUTPUT +fi # test whether the filesystem supports symbolic links ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS From 0c9ea33b90f6ed73b9c9151c2cad9ce341d6778b Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Wed, 9 Mar 2011 21:17:58 -0600 Subject: [PATCH 7/9] i18n: add stub Q_() wrapper for ngettext The Q_ function translates a string representing some pharse with an alternative plural form and uses the 'count' argument to choose which form to return. Use of Q_ solves the "%d noun(s)" problem in a way that is portable to languages outside the Germanic and Romance families. In English, the semantics of Q_(sing, plur, count) are roughly equivalent to count == 1 ? _(sing) : _(plur) while in other languages there can be more variants (count == 0; more random-looking rules based on the historical pronunciation of the number). Behind the scenes, the singular form is used to look up a family of translations and the plural form is ignored unless no translation is available. Define such a Q_ in gettext.h with the English semantics so C code can start using it to mark phrases with a count for translation. The name "Q_" is taken from subversion and stands for "quantity". Many projects just use ngettext directly without a wrapper analogous to _; we should not do so because git's gettext.h is meant not to conflict with system headers that might include libintl.h. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- gettext.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/gettext.h b/gettext.h index 04b5958e2b..1b253b7e76 100644 --- a/gettext.h +++ b/gettext.h @@ -9,8 +9,8 @@ #ifndef GETTEXT_H #define GETTEXT_H -#ifdef _ -#error "namespace conflict: '_' is pre-defined?" +#if defined(_) || defined(Q_) +#error "namespace conflict: '_' or 'Q_' is pre-defined?" #endif #define FORMAT_PRESERVING(n) __attribute__((format_arg(n))) @@ -26,6 +26,14 @@ static inline FORMAT_PRESERVING(1) const char *_(const char *msgid) return use_gettext_poison() ? "# GETTEXT POISON #" : msgid; } +static inline FORMAT_PRESERVING(1) FORMAT_PRESERVING(2) +const char *Q_(const char *msgid, const char *plu, unsigned long n) +{ + if (use_gettext_poison()) + return "# GETTEXT POISON #"; + return n == 1 ? msgid : plu; +} + /* Mark msgid for translation but do not translate it. */ #define N_(msgid) (msgid) From cd5513a7168915b31bb5747216473b618c19679e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 22 Feb 2011 23:41:23 +0000 Subject: [PATCH 8/9] i18n: Makefile: "pot" target to extract messages marked for translation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add rules to generate a template (po/git.pot) listing messages marked for translation in the C portion of git. To get started translating, just run "make pot". Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- Makefile | 16 ++++++++++++++++ po/.gitignore | 1 + 2 files changed, 17 insertions(+) create mode 100644 po/.gitignore diff --git a/Makefile b/Makefile index 4e9d935828..1cc6c6087b 100644 --- a/Makefile +++ b/Makefile @@ -321,6 +321,7 @@ INSTALL = install RPMBUILD = rpmbuild TCL_PATH = tclsh TCLTK_PATH = wish +XGETTEXT = xgettext PTHREAD_LIBS = -lpthread PTHREAD_CFLAGS = GCOV = gcov @@ -1590,6 +1591,7 @@ ifndef V QUIET_BUILT_IN = @echo ' ' BUILTIN $@; QUIET_GEN = @echo ' ' GEN $@; QUIET_LNCP = @echo ' ' LN/CP $@; + QUIET_XGETTEXT = @echo ' ' XGETTEXT $@; QUIET_GCOV = @echo ' ' GCOV $@; QUIET_SUBDIR0 = +@subdir= QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo ' ' SUBDIR $$subdir; \ @@ -2057,6 +2059,20 @@ info: pdf: $(MAKE) -C Documentation pdf +XGETTEXT_FLAGS = \ + --force-po \ + --add-comments \ + --msgid-bugs-address="Git Mailing List " \ + --from-code=UTF-8 +XGETTEXT_FLAGS_C = $(XGETTEXT_FLAGS) --keyword=_ --keyword=N_ --language=C +LOCALIZED_C := $(C_OBJ:o=c) + +po/git.pot: $(LOCALIZED_C) + $(QUIET_XGETTEXT)$(XGETTEXT) -o$@+ $(XGETTEXT_FLAGS_C) $(LOCALIZED_C) && \ + mv $@+ $@ + +pot: po/git.pot + $(ETAGS_TARGET): FORCE $(RM) $(ETAGS_TARGET) $(FIND) . -name '*.[hcS]' -print | xargs etags -a -o $(ETAGS_TARGET) diff --git a/po/.gitignore b/po/.gitignore new file mode 100644 index 0000000000..a242a86e93 --- /dev/null +++ b/po/.gitignore @@ -0,0 +1 @@ +/git.pot From 92a684b916edf9e0f4f7962865e62ff71a988445 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Fri, 25 Feb 2011 01:22:12 -0600 Subject: [PATCH 9/9] i18n: "make distclean" should clean up after "make pot" This is in "make distclean" and not "make clean" to avoid needlessly changing the POT-Creation-Date in the following scenario: make clean; # cleaning up after an old build git pull make pot; # regenerate po template if necessary msgmerge po/my_language.po po/git.pot Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 1cc6c6087b..749783511c 100644 --- a/Makefile +++ b/Makefile @@ -2340,6 +2340,7 @@ dist-doc: distclean: clean $(RM) configure + $(RM) po/git.pot clean: $(RM) *.o block-sha1/*.o ppc/*.o compat/*.o compat/*/*.o xdiff/*.o vcs-svn/*.o \