Skip to content

Commit dc5cfdb

Browse files
Sim DomingoEdward Thomson
authored andcommitted
make git_diff_stats_to_buf not show 0 insertions or 0 deletions
1 parent f0ee795 commit dc5cfdb

File tree

12 files changed

+58
-13
lines changed

12 files changed

+58
-13
lines changed

src/diff_stats.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,24 @@ int git_diff_stats_to_buf(
299299
}
300300

301301
if (format & GIT_DIFF_STATS_FULL || format & GIT_DIFF_STATS_SHORT) {
302-
error = git_buf_printf(
303-
out, " %" PRIuZ " file%s changed, %" PRIuZ
304-
" insertion%s(+), %" PRIuZ " deletion%s(-)\n",
305-
stats->files_changed, stats->files_changed != 1 ? "s" : "",
306-
stats->insertions, stats->insertions != 1 ? "s" : "",
307-
stats->deletions, stats->deletions != 1 ? "s" : "");
308-
309-
if (error < 0)
310-
return error;
302+
git_buf_printf(
303+
out, " %" PRIuZ " file%s changed",
304+
stats->files_changed, stats->files_changed != 1 ? "s" : "");
305+
306+
if (stats->insertions || stats->deletions == 0)
307+
git_buf_printf(
308+
out, ", %" PRIuZ " insertion%s(+)",
309+
stats->insertions, stats->insertions != 1 ? "s" : "");
310+
311+
if (stats->deletions || stats->insertions == 0)
312+
git_buf_printf(
313+
out, ", %" PRIuZ " deletion%s(-)",
314+
stats->deletions, stats->deletions != 1 ? "s" : "");
315+
316+
git_buf_putc(out, '\n');
317+
318+
if (git_buf_oom(out))
319+
return -1;
311320
}
312321

313322
if (format & GIT_DIFF_STATS_INCLUDE_SUMMARY) {
@@ -333,4 +342,3 @@ void git_diff_stats_free(git_diff_stats *stats)
333342
git__free(stats->filestats);
334343
git__free(stats);
335344
}
336-

tests/diff/format_email.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void test_diff_format_email__with_message(void)
112112
"Also test if new paragraphs are included correctly.\n" \
113113
"---\n" \
114114
" file3.txt | 1 +\n" \
115-
" 1 file changed, 1 insertion(+), 0 deletions(-)\n" \
115+
" 1 file changed, 1 insertion(+)\n" \
116116
"\n" \
117117
"diff --git a/file3.txt b/file3.txt\n" \
118118
"index 9a2d780..7309653 100644\n" \
@@ -155,7 +155,7 @@ void test_diff_format_email__multiple(void)
155155
"---\n" \
156156
" file2.txt | 5 +++++\n" \
157157
" file3.txt | 5 +++++\n" \
158-
" 2 files changed, 10 insertions(+), 0 deletions(-)\n" \
158+
" 2 files changed, 10 insertions(+)\n" \
159159
" create mode 100644 file2.txt\n" \
160160
" create mode 100644 file3.txt\n" \
161161
"\n" \

tests/diff/stats.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,42 @@ void test_diff_stats__shortstat(void)
113113
git_buf_free(&buf);
114114
}
115115

116+
void test_diff_stats__shortstat_noinsertions(void)
117+
{
118+
git_buf buf = GIT_BUF_INIT;
119+
const char *stat =
120+
" 1 file changed, 2 deletions(-)\n";
121+
122+
diff_stats_from_commit_oid(
123+
&_stats, "06b7b69a62cbd1e53c6c4e0c3f16473dcfdb4af6", false);
124+
125+
cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
126+
cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
127+
cl_assert_equal_sz(2, git_diff_stats_deletions(_stats));
128+
129+
cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
130+
cl_assert_equal_s(stat, git_buf_cstr(&buf));
131+
git_buf_free(&buf);
132+
}
133+
134+
void test_diff_stats__shortstat_nodeletions(void)
135+
{
136+
git_buf buf = GIT_BUF_INIT;
137+
const char *stat =
138+
" 1 file changed, 3 insertions(+)\n";
139+
140+
diff_stats_from_commit_oid(
141+
&_stats, "5219b9784f9a92d7bd7cb567a6d6a21bfb86697e", false);
142+
143+
cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
144+
cl_assert_equal_sz(3, git_diff_stats_insertions(_stats));
145+
cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));
146+
147+
cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
148+
cl_assert_equal_s(stat, git_buf_cstr(&buf));
149+
git_buf_free(&buf);
150+
}
151+
116152
void test_diff_stats__rename(void)
117153
{
118154
git_buf buf = GIT_BUF_INIT;
0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)