Merge branch 'main' of github.com:git/git
[alt-git.git] / t / t4011-diff-symlink.sh
blobd7a5f7ae780c0319514fd949dacac98cfe6ce23b
1 #!/bin/sh
3 # Copyright (c) 2005 Johannes Schindelin
6 test_description='Test diff of symlinks.
10 TEST_PASSES_SANITIZE_LEAK=true
11 . ./test-lib.sh
12 . "$TEST_DIRECTORY"/lib-diff.sh
14 # Print the short OID of a symlink with the given name.
15 symlink_oid () {
16 local oid=$(printf "%s" "$1" | git hash-object --stdin) &&
17 git rev-parse --short "$oid"
20 # Print the short OID of the given file.
21 short_oid () {
22 local oid=$(git hash-object "$1") &&
23 git rev-parse --short "$oid"
26 test_expect_success 'diff new symlink and file' '
27 symlink=$(symlink_oid xyzzy) &&
28 cat >expected <<-EOF &&
29 diff --git a/frotz b/frotz
30 new file mode 120000
31 index 0000000..$symlink
32 --- /dev/null
33 +++ b/frotz
34 @@ -0,0 +1 @@
35 +xyzzy
36 \ No newline at end of file
37 diff --git a/nitfol b/nitfol
38 new file mode 100644
39 index 0000000..$symlink
40 --- /dev/null
41 +++ b/nitfol
42 @@ -0,0 +1 @@
43 +xyzzy
44 EOF
46 # the empty tree
47 git update-index &&
48 tree=$(git write-tree) &&
50 test_ln_s_add xyzzy frotz &&
51 echo xyzzy >nitfol &&
52 git update-index --add nitfol &&
53 GIT_DIFF_OPTS=--unified=0 git diff-index -M -p $tree >current &&
54 compare_diff_patch expected current
57 test_expect_success 'diff unchanged symlink and file' '
58 tree=$(git write-tree) &&
59 git update-index frotz nitfol &&
60 test -z "$(git diff-index --name-only $tree)"
63 test_expect_success 'diff removed symlink and file' '
64 cat >expected <<-EOF &&
65 diff --git a/frotz b/frotz
66 deleted file mode 120000
67 index $symlink..0000000
68 --- a/frotz
69 +++ /dev/null
70 @@ -1 +0,0 @@
71 -xyzzy
72 \ No newline at end of file
73 diff --git a/nitfol b/nitfol
74 deleted file mode 100644
75 index $symlink..0000000
76 --- a/nitfol
77 +++ /dev/null
78 @@ -1 +0,0 @@
79 -xyzzy
80 EOF
81 mv frotz frotz2 &&
82 mv nitfol nitfol2 &&
83 git diff-index -M -p $tree >current &&
84 compare_diff_patch expected current
87 test_expect_success 'diff identical, but newly created symlink and file' '
88 >expected &&
89 rm -f frotz nitfol &&
90 echo xyzzy >nitfol &&
91 test-tool chmtime +10 nitfol &&
92 if test_have_prereq SYMLINKS
93 then
94 ln -s xyzzy frotz
95 else
96 printf xyzzy >frotz
97 # the symlink property propagates from the index
98 fi &&
99 git diff-index -M -p $tree >current &&
100 compare_diff_patch expected current &&
102 >expected &&
103 git diff-index -M -p -w $tree >current &&
104 compare_diff_patch expected current
107 test_expect_success 'diff different symlink and file' '
108 new=$(symlink_oid yxyyz) &&
109 cat >expected <<-EOF &&
110 diff --git a/frotz b/frotz
111 index $symlink..$new 120000
112 --- a/frotz
113 +++ b/frotz
114 @@ -1 +1 @@
115 -xyzzy
116 \ No newline at end of file
117 +yxyyz
118 \ No newline at end of file
119 diff --git a/nitfol b/nitfol
120 index $symlink..$new 100644
121 --- a/nitfol
122 +++ b/nitfol
123 @@ -1 +1 @@
124 -xyzzy
125 +yxyyz
127 rm -f frotz &&
128 if test_have_prereq SYMLINKS
129 then
130 ln -s yxyyz frotz
131 else
132 printf yxyyz >frotz
133 # the symlink property propagates from the index
134 fi &&
135 echo yxyyz >nitfol &&
136 git diff-index -M -p $tree >current &&
137 compare_diff_patch expected current
140 test_expect_success SYMLINKS 'diff symlinks with non-existing targets' '
141 ln -s narf pinky &&
142 ln -s take\ over brain &&
143 test_must_fail git diff --no-index pinky brain >output 2>output.err &&
144 grep narf output &&
145 test_must_be_empty output.err
148 test_expect_success SYMLINKS 'setup symlinks with attributes' '
149 echo "*.bin diff=bin" >>.gitattributes &&
150 echo content >file.bin &&
151 ln -s file.bin link.bin &&
152 git add -N file.bin link.bin
155 test_expect_success SYMLINKS 'symlinks do not respect userdiff config by path' '
156 file=$(short_oid file.bin) &&
157 link=$(symlink_oid file.bin) &&
158 cat >expect <<-EOF &&
159 diff --git a/file.bin b/file.bin
160 new file mode 100644
161 index 0000000..$file
162 Binary files /dev/null and b/file.bin differ
163 diff --git a/link.bin b/link.bin
164 new file mode 120000
165 index 0000000..$link
166 --- /dev/null
167 +++ b/link.bin
168 @@ -0,0 +1 @@
169 +file.bin
170 \ No newline at end of file
172 git config diff.bin.binary true &&
173 git diff file.bin link.bin >actual &&
174 test_cmp expect actual
177 test_done