Commit 610cff13a31964eeb2264a452424e1e3d49accc3

Edward Thomson 2016-10-09T16:05:48

Merge branch 'pr/3809'

diff --git a/src/diff_stats.c b/src/diff_stats.c
index 9c186d7..2005712 100644
--- a/src/diff_stats.c
+++ b/src/diff_stats.c
@@ -300,15 +300,24 @@ int git_diff_stats_to_buf(
 	}
 
 	if (format & GIT_DIFF_STATS_FULL || format & GIT_DIFF_STATS_SHORT) {
-		error = git_buf_printf(
-			out, " %" PRIuZ " file%s changed, %" PRIuZ
-			" insertion%s(+), %" PRIuZ " deletion%s(-)\n",
-			stats->files_changed, stats->files_changed != 1 ? "s" : "",
-			stats->insertions, stats->insertions != 1 ? "s" : "",
-			stats->deletions, stats->deletions != 1 ? "s" : "");
-
-		if (error < 0)
-			return error;
+		git_buf_printf(
+			out, " %" PRIuZ " file%s changed",
+			stats->files_changed, stats->files_changed != 1 ? "s" : "");
+
+		if (stats->insertions || stats->deletions == 0)
+			git_buf_printf(
+				out, ", %" PRIuZ " insertion%s(+)",
+				stats->insertions, stats->insertions != 1 ? "s" : "");
+
+		if (stats->deletions || stats->insertions == 0)
+			git_buf_printf(
+				out, ", %" PRIuZ " deletion%s(-)",
+				stats->deletions, stats->deletions != 1 ? "s" : "");
+
+		git_buf_putc(out, '\n');
+
+		if (git_buf_oom(out))
+			return -1;
 	}
 
 	if (format & GIT_DIFF_STATS_INCLUDE_SUMMARY) {
@@ -334,4 +343,3 @@ void git_diff_stats_free(git_diff_stats *stats)
 	git__free(stats->filestats);
 	git__free(stats);
 }
-
diff --git a/tests/diff/format_email.c b/tests/diff/format_email.c
index 9f8fe31..c3c0410 100644
--- a/tests/diff/format_email.c
+++ b/tests/diff/format_email.c
@@ -113,7 +113,7 @@ void test_diff_format_email__with_message(void)
 	"Also test if new paragraphs are included correctly.\n" \
 	"---\n" \
 	" file3.txt | 1 +\n" \
-	" 1 file changed, 1 insertion(+), 0 deletions(-)\n" \
+	" 1 file changed, 1 insertion(+)\n" \
 	"\n" \
 	"diff --git a/file3.txt b/file3.txt\n" \
 	"index 9a2d780..7309653 100644\n" \
@@ -156,7 +156,7 @@ void test_diff_format_email__multiple(void)
 	"---\n" \
 	" file2.txt | 5 +++++\n" \
 	" file3.txt | 5 +++++\n" \
-	" 2 files changed, 10 insertions(+), 0 deletions(-)\n" \
+	" 2 files changed, 10 insertions(+)\n" \
 	" create mode 100644 file2.txt\n" \
 	" create mode 100644 file3.txt\n" \
 	"\n" \
diff --git a/tests/diff/stats.c b/tests/diff/stats.c
index 8f146e2..3171a44 100644
--- a/tests/diff/stats.c
+++ b/tests/diff/stats.c
@@ -114,6 +114,42 @@ void test_diff_stats__shortstat(void)
 	git_buf_free(&buf);
 }
 
+void test_diff_stats__shortstat_noinsertions(void)
+{
+	git_buf buf = GIT_BUF_INIT;
+	const char *stat =
+	" 1 file changed, 2 deletions(-)\n";
+
+	diff_stats_from_commit_oid(
+		&_stats, "06b7b69a62cbd1e53c6c4e0c3f16473dcfdb4af6", false);
+
+	cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
+	cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
+	cl_assert_equal_sz(2, git_diff_stats_deletions(_stats));
+
+	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
+	cl_assert_equal_s(stat, git_buf_cstr(&buf));
+	git_buf_free(&buf);
+}
+
+void test_diff_stats__shortstat_nodeletions(void)
+{
+	git_buf buf = GIT_BUF_INIT;
+	const char *stat =
+	" 1 file changed, 3 insertions(+)\n";
+
+	diff_stats_from_commit_oid(
+		&_stats, "5219b9784f9a92d7bd7cb567a6d6a21bfb86697e", false);
+
+	cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
+	cl_assert_equal_sz(3, git_diff_stats_insertions(_stats));
+	cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));
+
+	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
+	cl_assert_equal_s(stat, git_buf_cstr(&buf));
+	git_buf_free(&buf);
+}
+
 void test_diff_stats__rename(void)
 {
 	git_buf buf = GIT_BUF_INIT;
diff --git a/tests/resources/diff_format_email/.gitted/index b/tests/resources/diff_format_email/.gitted/index
index d94f87d..092a888 100644
Binary files a/tests/resources/diff_format_email/.gitted/index and b/tests/resources/diff_format_email/.gitted/index differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/06/b7b69a62cbd1e53c6c4e0c3f16473dcfdb4af6 b/tests/resources/diff_format_email/.gitted/objects/06/b7b69a62cbd1e53c6c4e0c3f16473dcfdb4af6
new file mode 100644
index 0000000..37588f1
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/06/b7b69a62cbd1e53c6c4e0c3f16473dcfdb4af6 differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/52/19b9784f9a92d7bd7cb567a6d6a21bfb86697e b/tests/resources/diff_format_email/.gitted/objects/52/19b9784f9a92d7bd7cb567a6d6a21bfb86697e
new file mode 100644
index 0000000..534e3b0
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/52/19b9784f9a92d7bd7cb567a6d6a21bfb86697e differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/53/525d4cc3ef3ba4a5cbf69492fdffb4e4a74558 b/tests/resources/diff_format_email/.gitted/objects/53/525d4cc3ef3ba4a5cbf69492fdffb4e4a74558
new file mode 100644
index 0000000..b74d31f
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/53/525d4cc3ef3ba4a5cbf69492fdffb4e4a74558 differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/a7/a65f98355b5a7567bcc395f6f7936c9252cf57 b/tests/resources/diff_format_email/.gitted/objects/a7/a65f98355b5a7567bcc395f6f7936c9252cf57
new file mode 100644
index 0000000..36a4b7b
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/a7/a65f98355b5a7567bcc395f6f7936c9252cf57 differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/c7/1a05d36025c806496a74d46d7d596eb23295c4 b/tests/resources/diff_format_email/.gitted/objects/c7/1a05d36025c806496a74d46d7d596eb23295c4
new file mode 100644
index 0000000..2822fe3
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/c7/1a05d36025c806496a74d46d7d596eb23295c4 differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/d3/b6b38486f620b5b532a8cc6e0198ab7c3f52e4 b/tests/resources/diff_format_email/.gitted/objects/d3/b6b38486f620b5b532a8cc6e0198ab7c3f52e4
new file mode 100644
index 0000000..5b2e664
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/d3/b6b38486f620b5b532a8cc6e0198ab7c3f52e4 differ
diff --git a/tests/resources/diff_format_email/.gitted/refs/heads/master b/tests/resources/diff_format_email/.gitted/refs/heads/master
index 3bc734d..4024b97 100644
--- a/tests/resources/diff_format_email/.gitted/refs/heads/master
+++ b/tests/resources/diff_format_email/.gitted/refs/heads/master
@@ -1 +1 @@
-627e7e12d87e07a83fad5b6bfa25e86ead4a5270
+5219b9784f9a92d7bd7cb567a6d6a21bfb86697e
diff --git a/tests/resources/diff_format_email/file3.txt b/tests/resources/diff_format_email/file3.txt
index 7309653..c71a05d 100644
--- a/tests/resources/diff_format_email/file3.txt
+++ b/tests/resources/diff_format_email/file3.txt
@@ -4,3 +4,4 @@ file3
 file3
 file3
 file3
+file3