revision: fix leaking parents when simplifying commits
[git/gitster.git] / t / t4053-diff-no-index.sh
blob651ec776606bb0990edc40bebf13ccc4937bb0ce
1 #!/bin/sh
3 test_description='diff --no-index'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 test_expect_success 'setup' '
9 mkdir a &&
10 mkdir b &&
11 echo 1 >a/1 &&
12 echo 2 >a/2 &&
13 git init repo &&
14 echo 1 >repo/a &&
15 mkdir -p non/git &&
16 echo 1 >non/git/a &&
17 echo 1 >non/git/b
20 test_expect_success 'git diff --no-index --exit-code' '
21 git diff --no-index --exit-code a/1 non/git/a &&
22 test_expect_code 1 git diff --no-index --exit-code a/1 a/2
25 test_expect_success 'git diff --no-index directories' '
26 test_expect_code 1 git diff --no-index a b >cnt &&
27 test_line_count = 14 cnt
30 test_expect_success 'git diff --no-index relative path outside repo' '
32 cd repo &&
33 test_expect_code 0 git diff --no-index a ../non/git/a &&
34 test_expect_code 0 git diff --no-index ../non/git/a ../non/git/b
38 test_expect_success 'git diff --no-index with broken index' '
40 cd repo &&
41 echo broken >.git/index &&
42 git diff --no-index a ../non/git/a
46 test_expect_success 'git diff outside repo with broken index' '
48 cd repo &&
49 git diff ../non/git/a ../non/git/b
53 test_expect_success 'git diff --no-index executed outside repo gives correct error message' '
55 GIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&
56 export GIT_CEILING_DIRECTORIES &&
57 cd non/git &&
58 test_must_fail git diff --no-index a 2>actual.err &&
59 test_grep "usage: git diff --no-index" actual.err
63 test_expect_success 'diff D F and diff F D' '
65 cd repo &&
66 echo in-repo >a &&
67 echo non-repo >../non/git/a &&
68 mkdir sub &&
69 echo sub-repo >sub/a &&
71 test_must_fail git diff --no-index sub/a ../non/git/a >expect &&
72 test_must_fail git diff --no-index sub/a ../non/git/ >actual &&
73 test_cmp expect actual &&
75 test_must_fail git diff --no-index a ../non/git/a >expect &&
76 test_must_fail git diff --no-index a ../non/git/ >actual &&
77 test_cmp expect actual &&
79 test_must_fail git diff --no-index ../non/git/a a >expect &&
80 test_must_fail git diff --no-index ../non/git a >actual &&
81 test_cmp expect actual
85 test_expect_success 'turning a file into a directory' '
87 cd non/git &&
88 mkdir d e e/sub &&
89 echo 1 >d/sub &&
90 echo 2 >e/sub/file &&
91 printf "D\td/sub\nA\te/sub/file\n" >expect &&
92 test_must_fail git diff --no-index --name-status d e >actual &&
93 test_cmp expect actual
97 test_expect_success 'diff from repo subdir shows real paths (explicit)' '
98 echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
99 test_expect_code 1 \
100 git -C repo/sub \
101 diff --no-index ../../non/git/a ../../non/git/b >actual &&
102 head -n 1 <actual >actual.head &&
103 test_cmp expect actual.head
106 test_expect_success 'diff from repo subdir shows real paths (implicit)' '
107 echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
108 test_expect_code 1 \
109 git -C repo/sub \
110 diff ../../non/git/a ../../non/git/b >actual &&
111 head -n 1 <actual >actual.head &&
112 test_cmp expect actual.head
115 test_expect_success 'diff --no-index from repo subdir respects config (explicit)' '
116 echo "diff --git ../../non/git/a ../../non/git/b" >expect &&
117 test_config -C repo diff.noprefix true &&
118 test_expect_code 1 \
119 git -C repo/sub \
120 diff --no-index ../../non/git/a ../../non/git/b >actual &&
121 head -n 1 <actual >actual.head &&
122 test_cmp expect actual.head
125 test_expect_success 'diff --no-index from repo subdir respects config (implicit)' '
126 echo "diff --git ../../non/git/a ../../non/git/b" >expect &&
127 test_config -C repo diff.noprefix true &&
128 test_expect_code 1 \
129 git -C repo/sub \
130 diff ../../non/git/a ../../non/git/b >actual &&
131 head -n 1 <actual >actual.head &&
132 test_cmp expect actual.head
135 test_expect_success 'diff --no-index from repo subdir with absolute paths' '
136 cat <<-EOF >expect &&
137 1 1 $(pwd)/non/git/{a => b}
139 test_expect_code 1 \
140 git -C repo/sub diff --numstat \
141 "$(pwd)/non/git/a" "$(pwd)/non/git/b" >actual &&
142 test_cmp expect actual
145 test_expect_success 'diff --no-index allows external diff' '
146 test_expect_code 1 \
147 env GIT_EXTERNAL_DIFF="echo external ;:" \
148 git diff --no-index non/git/a non/git/b >actual &&
149 echo external >expect &&
150 test_cmp expect actual
153 test_expect_success 'diff --no-index normalizes mode: no changes' '
154 echo foo >x &&
155 cp x y &&
156 git diff --no-index x y >out &&
157 test_must_be_empty out
160 test_expect_success POSIXPERM 'diff --no-index normalizes mode: chmod +x' '
161 chmod +x y &&
162 cat >expected <<-\EOF &&
163 diff --git a/x b/y
164 old mode 100644
165 new mode 100755
167 test_expect_code 1 git diff --no-index x y >actual &&
168 test_cmp expected actual
171 test_expect_success POSIXPERM 'diff --no-index normalizes: mode not like git mode' '
172 chmod 666 x &&
173 chmod 777 y &&
174 cat >expected <<-\EOF &&
175 diff --git a/x b/y
176 old mode 100644
177 new mode 100755
179 test_expect_code 1 git diff --no-index x y >actual &&
180 test_cmp expected actual
183 test_expect_success POSIXPERM,SYMLINKS 'diff --no-index normalizes: mode not like git mode (symlink)' '
184 ln -s y z &&
185 X_OID=$(git hash-object --stdin <x) &&
186 Z_OID=$(printf y | git hash-object --stdin) &&
187 cat >expected <<-EOF &&
188 diff --git a/x b/x
189 deleted file mode 100644
190 index $X_OID..$ZERO_OID
191 --- a/x
192 +++ /dev/null
193 @@ -1 +0,0 @@
194 -foo
195 diff --git a/z b/z
196 new file mode 120000
197 index $ZERO_OID..$Z_OID
198 --- /dev/null
199 +++ b/z
200 @@ -0,0 +1 @@
202 \ No newline at end of file
204 test_expect_code 1 git -c core.abbrev=no diff --no-index x z >actual &&
205 test_cmp expected actual
208 test_expect_success POSIXPERM 'external diff with mode-only change' '
209 echo content >not-executable &&
210 echo content >executable &&
211 chmod +x executable &&
212 echo executable executable $(test_oid zero) 100755 \
213 not-executable $(test_oid zero) 100644 not-executable \
214 >expect &&
215 test_expect_code 1 git -c diff.external=echo diff \
216 --no-index executable not-executable >actual &&
217 test_cmp expect actual
220 test_expect_success "diff --no-index treats '-' as stdin" '
221 cat >expect <<-EOF &&
222 diff --git a/- b/a/1
223 index $ZERO_OID..$(git hash-object --stdin <a/1) 100644
224 --- a/-
225 +++ b/a/1
226 @@ -1 +1 @@
231 test_write_lines x | test_expect_code 1 \
232 git -c core.abbrev=no diff --no-index -- - a/1 >actual &&
233 test_cmp expect actual &&
235 test_write_lines 1 | git diff --no-index -- a/1 - >actual &&
236 test_must_be_empty actual
239 test_expect_success "diff --no-index -R treats '-' as stdin" '
240 cat >expect <<-EOF &&
241 diff --git b/a/1 a/-
242 index $(git hash-object --stdin <a/1)..$ZERO_OID 100644
243 --- b/a/1
244 +++ a/-
245 @@ -1 +1 @@
250 test_write_lines x | test_expect_code 1 \
251 git -c core.abbrev=no diff --no-index -R -- - a/1 >actual &&
252 test_cmp expect actual &&
254 test_write_lines 1 | git diff --no-index -R -- a/1 - >actual &&
255 test_must_be_empty actual
258 test_expect_success 'diff --no-index refuses to diff stdin and a directory' '
259 test_must_fail git diff --no-index -- - a </dev/null 2>err &&
260 grep "fatal: cannot compare stdin to a directory" err
263 test_expect_success PIPE 'diff --no-index refuses to diff a named pipe and a directory' '
264 test_when_finished "rm -f pipe" &&
265 mkfifo pipe &&
266 test_must_fail git diff --no-index -- pipe a 2>err &&
267 grep "fatal: cannot compare a named pipe to a directory" err
270 test_expect_success PIPE,SYMLINKS 'diff --no-index reads from pipes' '
271 test_when_finished "rm -f old new new-link" &&
272 mkfifo old &&
273 mkfifo new &&
274 ln -s new new-link &&
276 (test_write_lines a b c >old) &
277 } &&
278 test_when_finished "kill $! || :" &&
280 (test_write_lines a x c >new) &
281 } &&
282 test_when_finished "kill $! || :" &&
284 cat >expect <<-EOF &&
285 diff --git a/old b/new-link
286 --- a/old
287 +++ b/new-link
288 @@ -1,3 +1,3 @@
295 test_expect_code 1 git diff --no-index old new-link >actual &&
296 test_cmp expect actual
299 test_done