diff mbox series

[v2,2/2] diff --no-index tests: test mode normalization

Message ID a1ab6a323f2e0aeed781a95d286740d3013ff5f0.1616366036.git.avarab@gmail.com (mailing list archive)
State Superseded
Headers show
Series diff --no-index: fix test blind spots | expand

Commit Message

Ævar Arnfjörð Bjarmason March 21, 2021, 10:36 p.m. UTC
When "git diff --no-index X Y" is run the modes of the files being
differ are normalized by canon_mode() in fill_filespec().

I recently broke that behavior in a patch of mine[1] which would pass
all tests, or not, depending on the umask of the git.git checkout.

Let's test for this explicitly. Arguably this should not be the
behavior of "git diff --no-index". We aren't diffing our own objects
or the index, so it might be useful to show mode differences between
files.

On the other hand diff(1) does not do that, and it would be needlessly
distracting when e.g. diffing an extracted tar archive whose contents
is the same, but whose file modes are different.

1. https://lore.kernel.org/git/20210316155829.31242-2-avarab@gmail.com/

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/t4053-diff-no-index.sh | 55 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

Comments

Junio C Hamano March 22, 2021, 7:22 p.m. UTC | #1
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> +test_expect_success POSIXPERM,SYMLINKS 'diff --no-index normalizes: mode not like git mode (symlink)' '
> +	ln -s y z &&
> +	X_OID=$(git hash-object --stdin <x) &&
> +	Z_OID=$(echo -n y | git hash-object --stdin) &&

s/echo -n/printf/

t4053-diff-no-index.sh:185: error: echo with option is not portable
(use printf): Z_OID=$(echo -n y | git hash-object --stdin) &&
gmake[1]: *** [Makefile:95: test-lint-shell-syntax] Error 1
gmake[1]: Leaving directory '/home/jch/w/buildfarm/jch/t'
gmake: *** [Makefile:2855: test] Error 2
diff mbox series

Patch

diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
index 44b932fbb20..39d1da1f362 100755
--- a/t/t4053-diff-no-index.sh
+++ b/t/t4053-diff-no-index.sh
@@ -149,4 +149,59 @@  test_expect_success 'diff --no-index allows external diff' '
 	test_cmp expect actual
 '
 
+test_expect_success 'diff --no-index normalizes mode: no changes' '
+	echo foo >x &&
+	cp x y &&
+	git diff --no-index x y >out &&
+	test_must_be_empty out
+'
+
+test_expect_success POSIXPERM 'diff --no-index normalizes mode: chmod +x' '
+	chmod +x y &&
+	cat >expected <<-\EOF &&
+	diff --git a/x b/y
+	old mode 100644
+	new mode 100755
+	EOF
+	test_expect_code 1 git diff --no-index x y >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success POSIXPERM 'diff --no-index normalizes: mode not like git mode' '
+	chmod 666 x &&
+	chmod 777 y &&
+	cat >expected <<-\EOF &&
+	diff --git a/x b/y
+	old mode 100644
+	new mode 100755
+	EOF
+	test_expect_code 1 git diff --no-index x y >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success POSIXPERM,SYMLINKS 'diff --no-index normalizes: mode not like git mode (symlink)' '
+	ln -s y z &&
+	X_OID=$(git hash-object --stdin <x) &&
+	Z_OID=$(echo -n y | git hash-object --stdin) &&
+	cat >expected <<-EOF &&
+	diff --git a/x b/x
+	deleted file mode 100644
+	index $X_OID..$ZERO_OID
+	--- a/x
+	+++ /dev/null
+	@@ -1 +0,0 @@
+	-foo
+	diff --git a/z b/z
+	new file mode 120000
+	index $ZERO_OID..$Z_OID
+	--- /dev/null
+	+++ b/z
+	@@ -0,0 +1 @@
+	+y
+	\ No newline at end of file
+	EOF
+	test_expect_code 1 git -c core.abbrev=no diff --no-index x z >actual &&
+	test_cmp expected actual
+'
+
 test_done