blob: 717034bb50b57f3edc5d19989f0b178e912e08e6 [file] [log] [blame]
Johannes Schindelin975b31d2005-12-26 22:31:42 +01001#!/bin/sh
2#
3# Copyright (c) 2005 Johannes Schindelin
4#
5
6test_description='Test diff of symlinks.
7
8'
9. ./test-lib.sh
Junio C Hamanobfdbee92008-08-08 02:26:28 -070010. "$TEST_DIRECTORY"/diff-lib.sh
Johannes Schindelin975b31d2005-12-26 22:31:42 +010011
brian m. carlson840624f2019-10-28 00:58:59 +000012# Print the short OID of a symlink with the given name.
13symlink_oid () {
14 local oid=$(printf "%s" "$1" | git hash-object --stdin) &&
15 git rev-parse --short "$oid"
16}
17
18# Print the short OID of the given file.
19short_oid () {
20 local oid=$(git hash-object "$1") &&
21 git rev-parse --short "$oid"
22}
23
Johannes Sixt70836a62013-06-07 22:53:34 +020024test_expect_success 'diff new symlink and file' '
brian m. carlson840624f2019-10-28 00:58:59 +000025 symlink=$(symlink_oid xyzzy) &&
26 cat >expected <<-EOF &&
Junio C Hamano13a48992012-02-29 18:14:14 -080027 diff --git a/frotz b/frotz
28 new file mode 120000
brian m. carlson840624f2019-10-28 00:58:59 +000029 index 0000000..$symlink
Junio C Hamano13a48992012-02-29 18:14:14 -080030 --- /dev/null
31 +++ b/frotz
32 @@ -0,0 +1 @@
33 +xyzzy
34 \ No newline at end of file
Junio C Hamano5597e842012-02-29 18:14:15 -080035 diff --git a/nitfol b/nitfol
36 new file mode 100644
brian m. carlson840624f2019-10-28 00:58:59 +000037 index 0000000..$symlink
Junio C Hamano5597e842012-02-29 18:14:15 -080038 --- /dev/null
39 +++ b/nitfol
40 @@ -0,0 +1 @@
41 +xyzzy
Junio C Hamano13a48992012-02-29 18:14:14 -080042 EOF
Johannes Sixt70836a62013-06-07 22:53:34 +020043
44 # the empty tree
Junio C Hamano13a48992012-02-29 18:14:14 -080045 git update-index &&
46 tree=$(git write-tree) &&
Johannes Sixt70836a62013-06-07 22:53:34 +020047
48 test_ln_s_add xyzzy frotz &&
49 echo xyzzy >nitfol &&
50 git update-index --add nitfol &&
Junio C Hamano13a48992012-02-29 18:14:14 -080051 GIT_DIFF_OPTS=--unified=0 git diff-index -M -p $tree >current &&
52 compare_diff_patch expected current
53'
Johannes Schindelin975b31d2005-12-26 22:31:42 +010054
Johannes Sixt70836a62013-06-07 22:53:34 +020055test_expect_success 'diff unchanged symlink and file' '
Junio C Hamano13a48992012-02-29 18:14:14 -080056 tree=$(git write-tree) &&
Junio C Hamano5597e842012-02-29 18:14:15 -080057 git update-index frotz nitfol &&
Junio C Hamano13a48992012-02-29 18:14:14 -080058 test -z "$(git diff-index --name-only $tree)"
59'
Johannes Schindelin975b31d2005-12-26 22:31:42 +010060
Johannes Sixt70836a62013-06-07 22:53:34 +020061test_expect_success 'diff removed symlink and file' '
brian m. carlson840624f2019-10-28 00:58:59 +000062 cat >expected <<-EOF &&
Junio C Hamano13a48992012-02-29 18:14:14 -080063 diff --git a/frotz b/frotz
64 deleted file mode 120000
brian m. carlson840624f2019-10-28 00:58:59 +000065 index $symlink..0000000
Junio C Hamano13a48992012-02-29 18:14:14 -080066 --- a/frotz
67 +++ /dev/null
68 @@ -1 +0,0 @@
69 -xyzzy
70 \ No newline at end of file
Junio C Hamano5597e842012-02-29 18:14:15 -080071 diff --git a/nitfol b/nitfol
72 deleted file mode 100644
brian m. carlson840624f2019-10-28 00:58:59 +000073 index $symlink..0000000
Junio C Hamano5597e842012-02-29 18:14:15 -080074 --- a/nitfol
75 +++ /dev/null
76 @@ -1 +0,0 @@
77 -xyzzy
Junio C Hamano13a48992012-02-29 18:14:14 -080078 EOF
79 mv frotz frotz2 &&
Junio C Hamano5597e842012-02-29 18:14:15 -080080 mv nitfol nitfol2 &&
Junio C Hamano13a48992012-02-29 18:14:14 -080081 git diff-index -M -p $tree >current &&
82 compare_diff_patch expected current
83'
Johannes Schindelin975b31d2005-12-26 22:31:42 +010084
Johannes Sixt70836a62013-06-07 22:53:34 +020085test_expect_success 'diff identical, but newly created symlink and file' '
Junio C Hamanob3f01ff2012-02-29 18:14:16 -080086 >expected &&
Junio C Hamano5597e842012-02-29 18:14:15 -080087 rm -f frotz nitfol &&
88 echo xyzzy >nitfol &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +010089 test-tool chmtime +10 nitfol &&
Johannes Sixt70836a62013-06-07 22:53:34 +020090 if test_have_prereq SYMLINKS
91 then
92 ln -s xyzzy frotz
93 else
94 printf xyzzy >frotz
95 # the symlink property propagates from the index
96 fi &&
Junio C Hamano13a48992012-02-29 18:14:14 -080097 git diff-index -M -p $tree >current &&
Junio C Hamano5597e842012-02-29 18:14:15 -080098 compare_diff_patch expected current &&
99
100 >expected &&
101 git diff-index -M -p -w $tree >current &&
Junio C Hamano13a48992012-02-29 18:14:14 -0800102 compare_diff_patch expected current
103'
Johannes Schindelin975b31d2005-12-26 22:31:42 +0100104
Johannes Sixt70836a62013-06-07 22:53:34 +0200105test_expect_success 'diff different symlink and file' '
brian m. carlson840624f2019-10-28 00:58:59 +0000106 new=$(symlink_oid yxyyz) &&
107 cat >expected <<-EOF &&
Junio C Hamano13a48992012-02-29 18:14:14 -0800108 diff --git a/frotz b/frotz
brian m. carlson840624f2019-10-28 00:58:59 +0000109 index $symlink..$new 120000
Junio C Hamano13a48992012-02-29 18:14:14 -0800110 --- a/frotz
111 +++ b/frotz
112 @@ -1 +1 @@
113 -xyzzy
114 \ No newline at end of file
115 +yxyyz
116 \ No newline at end of file
Junio C Hamano5597e842012-02-29 18:14:15 -0800117 diff --git a/nitfol b/nitfol
brian m. carlson840624f2019-10-28 00:58:59 +0000118 index $symlink..$new 100644
Junio C Hamano5597e842012-02-29 18:14:15 -0800119 --- a/nitfol
120 +++ b/nitfol
121 @@ -1 +1 @@
122 -xyzzy
123 +yxyyz
Junio C Hamano13a48992012-02-29 18:14:14 -0800124 EOF
125 rm -f frotz &&
Johannes Sixt70836a62013-06-07 22:53:34 +0200126 if test_have_prereq SYMLINKS
127 then
128 ln -s yxyyz frotz
129 else
130 printf yxyyz >frotz
131 # the symlink property propagates from the index
132 fi &&
Junio C Hamano5597e842012-02-29 18:14:15 -0800133 echo yxyyz >nitfol &&
Junio C Hamano13a48992012-02-29 18:14:14 -0800134 git diff-index -M -p $tree >current &&
135 compare_diff_patch expected current
136'
Johannes Schindelin975b31d2005-12-26 22:31:42 +0100137
Junio C Hamano13a48992012-02-29 18:14:14 -0800138test_expect_success SYMLINKS 'diff symlinks with non-existing targets' '
139 ln -s narf pinky &&
140 ln -s take\ over brain &&
141 test_must_fail git diff --no-index pinky brain >output 2>output.err &&
142 grep narf output &&
SZEDER Gáborec10b012018-08-19 23:57:22 +0200143 test_must_be_empty output.err
Junio C Hamano13a48992012-02-29 18:14:14 -0800144'
Jeff Kingd391c0f2010-09-21 17:01:24 -0400145
146test_expect_success SYMLINKS 'setup symlinks with attributes' '
147 echo "*.bin diff=bin" >>.gitattributes &&
148 echo content >file.bin &&
149 ln -s file.bin link.bin &&
150 git add -N file.bin link.bin
151'
152
Jeff Kingd391c0f2010-09-21 17:01:24 -0400153test_expect_success SYMLINKS 'symlinks do not respect userdiff config by path' '
brian m. carlson840624f2019-10-28 00:58:59 +0000154 file=$(short_oid file.bin) &&
155 link=$(symlink_oid file.bin) &&
156 cat >expect <<-EOF &&
Junio C Hamano13a48992012-02-29 18:14:14 -0800157 diff --git a/file.bin b/file.bin
Nguyễn Thái Ngọc Duy0231ae72018-05-26 14:08:44 +0200158 new file mode 100644
brian m. carlson840624f2019-10-28 00:58:59 +0000159 index 0000000..$file
Nguyễn Thái Ngọc Duy0231ae72018-05-26 14:08:44 +0200160 Binary files /dev/null and b/file.bin differ
Junio C Hamano13a48992012-02-29 18:14:14 -0800161 diff --git a/link.bin b/link.bin
Nguyễn Thái Ngọc Duy0231ae72018-05-26 14:08:44 +0200162 new file mode 120000
brian m. carlson840624f2019-10-28 00:58:59 +0000163 index 0000000..$link
Nguyễn Thái Ngọc Duy0231ae72018-05-26 14:08:44 +0200164 --- /dev/null
Junio C Hamano13a48992012-02-29 18:14:14 -0800165 +++ b/link.bin
166 @@ -0,0 +1 @@
167 +file.bin
168 \ No newline at end of file
169 EOF
Jeff Kingd391c0f2010-09-21 17:01:24 -0400170 git config diff.bin.binary true &&
171 git diff file.bin link.bin >actual &&
172 test_cmp expect actual
173'
174
Johannes Schindelin975b31d2005-12-26 22:31:42 +0100175test_done