git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Correct singular form in diff summary line for human interaction
@ 2012-01-31 14:24 Nguyễn Thái Ngọc Duy
  2012-01-31 15:20 ` Jonathan Nieder
  2012-02-01 12:55 ` [PATCH v2] Use correct grammar in diffstat summary line Nguyễn Thái Ngọc Duy
  0 siblings, 2 replies; 32+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-01-31 14:24 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Frederik Schwarzer,
	Junio C Hamano, Nguyễn Thái Ngọc Duy

"git diff --stat" and "git apply --stat" now learn to print the line
"%d files changed, %d insertions(+), %d deletions(-)" in singular form
whenever applicable.

This change uncondtionally would upset scripts because scripts are not
nice. They simply hate changes. They can be very hostile when that
happens. So only adjust the line for human consumption.

Convenient function interactive_use() is added for this purpose. The
command thinks it's in human hands when:

 - GIT_SCRIPTING environment variable is not set
 - pager is on or
 - both stdout and stderr point to tty device

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 The third attempt in the last couple months to remove three "s" in
 one line. I did not upset the test suite with this and hopefully
 won't upset any scripts around. Good start?

 builtin/apply.c |    3 ++-
 cache.h         |    1 +
 diff.c          |   33 ++++++++++++++++++++++++++++-----
 diff.h          |    3 +++
 pager.c         |    7 +++++++
 5 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index c24dc54..389898f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -14,6 +14,7 @@
 #include "builtin.h"
 #include "string-list.h"
 #include "dir.h"
+#include "diff.h"
 #include "parse-options.h"
 
 /*
@@ -3241,7 +3242,7 @@ static void stat_patch_list(struct patch *patch)
 		show_stats(patch);
 	}
 
-	printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
+	print_stat_summary(stdout, files, adds, dels);
 }
 
 static void numstat_patch_list(struct patch *patch)
diff --git a/cache.h b/cache.h
index 10afd71..7e0bb2b 100644
--- a/cache.h
+++ b/cache.h
@@ -1175,6 +1175,7 @@ extern void setup_pager(void);
 extern const char *pager_program;
 extern int pager_in_use(void);
 extern int pager_use_color;
+extern int interactive_use(void);
 
 extern const char *editor_program;
 extern const char *askpass_program;
diff --git a/diff.c b/diff.c
index 7e15426..2d63e9c 100644
--- a/diff.c
+++ b/diff.c
@@ -1322,6 +1322,32 @@ static void fill_print_name(struct diffstat_file *file)
 	file->print_name = pname;
 }
 
+int print_stat_summary(FILE *fp, int files, int insertions, int deletions)
+{
+	struct strbuf sb = STRBUF_INIT;
+	int ret;
+
+	if (!interactive_use())
+		return fprintf(fp, " %d files changed, %d insertions(+), %d deletions(-)\n",
+			       files, insertions, deletions);
+
+	strbuf_addf(&sb,
+		    ngettext(" %d file changed,", " %d files changed,",
+			     files),
+		    files);
+	strbuf_addf(&sb,
+		    ngettext(" %d insertion(+),", " %d insertions(+),",
+			     insertions),
+		    insertions);
+	strbuf_addf(&sb,
+		    ngettext(" %d deletion(-)\n", " %d deletions(-)\n",
+			     deletions),
+		    deletions);
+	ret = fputs(sb.buf, fp);
+	strbuf_release(&sb);
+	return ret;
+}
+
 static void show_stats(struct diffstat_t *data, struct diff_options *options)
 {
 	int i, len, add, del, adds = 0, dels = 0;
@@ -1475,9 +1501,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
 		extra_shown = 1;
 	}
 	fprintf(options->file, "%s", line_prefix);
-	fprintf(options->file,
-	       " %d files changed, %d insertions(+), %d deletions(-)\n",
-	       total_files, adds, dels);
+	print_stat_summary(options->file, total_files, adds, dels);
 }
 
 static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
@@ -1507,8 +1531,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
 				options->output_prefix_data);
 		fprintf(options->file, "%s", msg->buf);
 	}
-	fprintf(options->file, " %d files changed, %d insertions(+), %d deletions(-)\n",
-	       total_files, adds, dels);
+	print_stat_summary(options->file, total_files, adds, dels);
 }
 
 static void show_numstat(struct diffstat_t *data, struct diff_options *options)
diff --git a/diff.h b/diff.h
index ae71f4c..7af5f1e 100644
--- a/diff.h
+++ b/diff.h
@@ -324,4 +324,7 @@ extern struct userdiff_driver *get_textconv(struct diff_filespec *one);
 
 extern int parse_rename_score(const char **cp_p);
 
+extern int print_stat_summary(FILE *fp, int files,
+			      int insertions, int deletions);
+
 #endif /* DIFF_H */
diff --git a/pager.c b/pager.c
index 975955b..9a3d3d8 100644
--- a/pager.c
+++ b/pager.c
@@ -110,3 +110,10 @@ int pager_in_use(void)
 	env = getenv("GIT_PAGER_IN_USE");
 	return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
 }
+
+int interactive_use(void)
+{
+	const char *env;
+	env = getenv("GIT_SCRIPTING");
+	return !env && (pager_in_use() || (isatty(1) && isatty(2)));
+}
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2012-03-13  6:14 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-01-31 14:24 [PATCH] Correct singular form in diff summary line for human interaction Nguyễn Thái Ngọc Duy
2012-01-31 15:20 ` Jonathan Nieder
2012-01-31 17:50   ` Junio C Hamano
2012-02-01  1:32     ` Nguyen Thai Ngoc Duy
2012-02-01  1:45       ` Thomas Dickey
2012-02-01  1:56       ` Thomas Dickey
2012-02-01  2:37         ` Nguyen Thai Ngoc Duy
2012-02-01  3:04         ` Junio C Hamano
2012-02-01  9:40           ` Thomas Dickey
2012-02-01 12:55 ` [PATCH v2] Use correct grammar in diffstat summary line Nguyễn Thái Ngọc Duy
2012-02-01 21:26   ` Junio C Hamano
2012-02-02 14:22     ` Nguyen Thai Ngoc Duy
2012-02-02 18:24       ` Junio C Hamano
2012-02-03  1:11         ` Nguyen Thai Ngoc Duy
2012-02-03  1:18           ` Junio C Hamano
2012-02-03 12:24           ` Jeff King
2012-02-01 23:35   ` Jonathan Nieder
2012-02-02  0:20     ` [RFC/PATCH] i18n: do not define gettext/ngettext in NO_GETTEXT case Jonathan Nieder
2012-02-02  0:27     ` [PATCH v2] Use correct grammar in diffstat summary line Jonathan Nieder
2012-02-02  0:31     ` Jonathan Nieder
2012-03-13  4:51   ` [RFC/PATCH 0/7] tests: diffstat summary line varies by locale (Re: [PATCH v2] Use correct grammar in diffstat summary line) Jonathan Nieder
2012-03-13  4:54     ` [PATCH 1/7] test: use test_i18ncmp when checking --stat output Jonathan Nieder
2012-03-13  6:00       ` Junio C Hamano
2012-03-13  6:05         ` Junio C Hamano
2012-03-13  6:13           ` Jonathan Nieder
2012-03-13  6:06         ` Jonathan Nieder
2012-03-13  4:58     ` [PATCH 2/7] test: use numstat instead of diffstat in funny-names test Jonathan Nieder
2012-03-13  4:59     ` [PATCH 3/7] test: modernize funny-names test style Jonathan Nieder
2012-03-13  5:00     ` [PATCH 4/7] test: test cherry-pick functionality and output separately Jonathan Nieder
2012-03-13  5:01     ` [PATCH 5/7] test: use --numstat instead of --stat in "git stash show" tests Jonathan Nieder
2012-03-13  5:02     ` [PATCH 6/7] test: use numstat instead of diffstat in binary-diff test Jonathan Nieder
2012-03-13  5:05     ` [PATCH 7/7] diffstat summary line varies by locale: miscellany Jonathan Nieder

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).