Subject: [PATCH] Correct singular form in diff summary line for human interaction

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Tue, 31 Jan 2012 21:24:00 +0700

"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 <[email-masked]>
---
 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