Commit 0cd976c831ff32b1303bfca5ce4939af3a812473

Edward Thomson 2018-10-07T12:00:06

Merge pull request #4830 from pks-t/pks/diff-stats-rename-common diff_stats: use git's formatting of renames with common directories

diff --git a/src/diff_stats.c b/src/diff_stats.c
index 583c684..3af5914 100644
--- a/src/diff_stats.c
+++ b/src/diff_stats.c
@@ -61,15 +61,29 @@ int git_diff_file_stats__full_to_buf(
 	old_size = delta->old_file.size;
 	new_size = delta->new_file.size;
 
-	if (git_buf_printf(out, " %s", old_path) < 0)
-		goto on_error;
-
 	if (strcmp(old_path, new_path) != 0) {
+		size_t common_dirlen;
+		int error;
+
 		padding = stats->max_name - strlen(old_path) - strlen(new_path);
 
-		if (git_buf_printf(out, DIFF_RENAME_FILE_SEPARATOR "%s", new_path) < 0)
+		if ((common_dirlen = git_path_common_dirlen(old_path, new_path)) &&
+		    common_dirlen <= INT_MAX) {
+			error = git_buf_printf(out, " %.*s{%s"DIFF_RENAME_FILE_SEPARATOR"%s}",
+					       (int) common_dirlen, old_path,
+					       old_path + common_dirlen,
+					       new_path + common_dirlen);
+		} else {
+			error = git_buf_printf(out, " %s" DIFF_RENAME_FILE_SEPARATOR "%s",
+					       old_path, new_path);
+		}
+
+		if (error < 0)
 			goto on_error;
 	} else {
+		if (git_buf_printf(out, " %s", old_path) < 0)
+			goto on_error;
+
 		padding = stats->max_name - strlen(old_path);
 
 		if (stats->renames > 0)
diff --git a/tests/diff/stats.c b/tests/diff/stats.c
index 24316f7..1503556 100644
--- a/tests/diff/stats.c
+++ b/tests/diff/stats.c
@@ -210,6 +210,25 @@ void test_diff_stats__rename_and_modifiy(void)
 	git_buf_dispose(&buf);
 }
 
+void test_diff_stats__rename_in_subdirectory(void)
+{
+	git_buf buf = GIT_BUF_INIT;
+	const char *stat =
+	" dir/{orig.txt => renamed.txt} | 0\n"
+	" 1 file changed, 0 insertions(+), 0 deletions(-)\n";
+
+	diff_stats_from_commit_oid(
+		&_stats, "0db2a262bc8c5c3cba55254730045a8258da7a37", true);
+
+	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(0, git_diff_stats_deletions(_stats));
+
+	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
+	cl_assert_equal_s(stat, git_buf_cstr(&buf));
+	git_buf_dispose(&buf);
+}
+
 void test_diff_stats__rename_no_find(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 092a888..4514a6b 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/07/594f324ebcf92902334c6016e30e716431dfbc b/tests/resources/diff_format_email/.gitted/objects/07/594f324ebcf92902334c6016e30e716431dfbc
new file mode 100644
index 0000000..bd27379
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/07/594f324ebcf92902334c6016e30e716431dfbc differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/0d/593fca18d1ab11deb6e8025c9fe417456fe883 b/tests/resources/diff_format_email/.gitted/objects/0d/593fca18d1ab11deb6e8025c9fe417456fe883
new file mode 100644
index 0000000..e49e107
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/0d/593fca18d1ab11deb6e8025c9fe417456fe883 differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/0d/b2a262bc8c5c3cba55254730045a8258da7a37 b/tests/resources/diff_format_email/.gitted/objects/0d/b2a262bc8c5c3cba55254730045a8258da7a37
new file mode 100644
index 0000000..1399d7f
--- /dev/null
+++ b/tests/resources/diff_format_email/.gitted/objects/0d/b2a262bc8c5c3cba55254730045a8258da7a37
@@ -0,0 +1,2 @@
+xA
+0@Q9f6 ztBCm-i
?4VUS)EhET|}Bd66=&98Kƀ<Ɓd6*ܸՒfx4-ulp6鱗\z""k4w]yQPV2j>TI
\ No newline at end of file
diff --git a/tests/resources/diff_format_email/.gitted/objects/39/ed6bbd76bca81c50db3aaca261456284f5d5b8 b/tests/resources/diff_format_email/.gitted/objects/39/ed6bbd76bca81c50db3aaca261456284f5d5b8
new file mode 100644
index 0000000..e08f03c
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/39/ed6bbd76bca81c50db3aaca261456284f5d5b8 differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/53/92ef3c959f744b892bbebb168bbbb7b05c03f3 b/tests/resources/diff_format_email/.gitted/objects/53/92ef3c959f744b892bbebb168bbbb7b05c03f3
new file mode 100644
index 0000000..308c599
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/53/92ef3c959f744b892bbebb168bbbb7b05c03f3 differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/85/fa91713734c588c897dd6dd67a39576f67ae50 b/tests/resources/diff_format_email/.gitted/objects/85/fa91713734c588c897dd6dd67a39576f67ae50
new file mode 100644
index 0000000..54d3412
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/85/fa91713734c588c897dd6dd67a39576f67ae50 differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/8c/637df9661edd808932b2f5d383f2c41ced9f06 b/tests/resources/diff_format_email/.gitted/objects/8c/637df9661edd808932b2f5d383f2c41ced9f06
new file mode 100644
index 0000000..e7d672b
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/8c/637df9661edd808932b2f5d383f2c41ced9f06 differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/94/f84b3e65e8bcbe8bffef2c885339343a802dd4 b/tests/resources/diff_format_email/.gitted/objects/94/f84b3e65e8bcbe8bffef2c885339343a802dd4
new file mode 100644
index 0000000..03b4848
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/94/f84b3e65e8bcbe8bffef2c885339343a802dd4 differ
diff --git a/tests/resources/diff_format_email/.gitted/objects/bb/07a00e58b260c4eb9a82f8afbc1d80ad9739bf b/tests/resources/diff_format_email/.gitted/objects/bb/07a00e58b260c4eb9a82f8afbc1d80ad9739bf
new file mode 100644
index 0000000..d2cb6d0
Binary files /dev/null and b/tests/resources/diff_format_email/.gitted/objects/bb/07a00e58b260c4eb9a82f8afbc1d80ad9739bf differ
diff --git a/tests/resources/diff_format_email/.gitted/refs/heads/master b/tests/resources/diff_format_email/.gitted/refs/heads/master
index 4024b97..7d5e03f 100644
--- a/tests/resources/diff_format_email/.gitted/refs/heads/master
+++ b/tests/resources/diff_format_email/.gitted/refs/heads/master
@@ -1 +1 @@
-5219b9784f9a92d7bd7cb567a6d6a21bfb86697e
+0db2a262bc8c5c3cba55254730045a8258da7a37
diff --git a/tests/resources/diff_format_email/dir/renamed.txt b/tests/resources/diff_format_email/dir/renamed.txt
new file mode 100644
index 0000000..7aff11d
--- /dev/null
+++ b/tests/resources/diff_format_email/dir/renamed.txt
@@ -0,0 +1,5 @@
+file2
+file2
+file2
+file2!
+file2