refs/files: skip creation of "refs/{heads,tags}" for worktrees
[git.git] / t / t4053-diff-no-index.sh
blob5ce345d309eb70425b9c04cdbfa57d1611675af8
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 "diff --no-index treats '-' as stdin" '
209 cat >expect <<-EOF &&
210 diff --git a/- b/a/1
211 index $ZERO_OID..$(git hash-object --stdin <a/1) 100644
212 --- a/-
213 +++ b/a/1
214 @@ -1 +1 @@
219 test_write_lines x | test_expect_code 1 \
220 git -c core.abbrev=no diff --no-index -- - a/1 >actual &&
221 test_cmp expect actual &&
223 test_write_lines 1 | git diff --no-index -- a/1 - >actual &&
224 test_must_be_empty actual
227 test_expect_success "diff --no-index -R treats '-' as stdin" '
228 cat >expect <<-EOF &&
229 diff --git b/a/1 a/-
230 index $(git hash-object --stdin <a/1)..$ZERO_OID 100644
231 --- b/a/1
232 +++ a/-
233 @@ -1 +1 @@
238 test_write_lines x | test_expect_code 1 \
239 git -c core.abbrev=no diff --no-index -R -- - a/1 >actual &&
240 test_cmp expect actual &&
242 test_write_lines 1 | git diff --no-index -R -- a/1 - >actual &&
243 test_must_be_empty actual
246 test_expect_success 'diff --no-index refuses to diff stdin and a directory' '
247 test_must_fail git diff --no-index -- - a </dev/null 2>err &&
248 grep "fatal: cannot compare stdin to a directory" err
251 test_expect_success PIPE 'diff --no-index refuses to diff a named pipe and a directory' '
252 test_when_finished "rm -f pipe" &&
253 mkfifo pipe &&
254 test_must_fail git diff --no-index -- pipe a 2>err &&
255 grep "fatal: cannot compare a named pipe to a directory" err
258 test_expect_success PIPE,SYMLINKS 'diff --no-index reads from pipes' '
259 test_when_finished "rm -f old new new-link" &&
260 mkfifo old &&
261 mkfifo new &&
262 ln -s new new-link &&
264 (test_write_lines a b c >old) &
265 } &&
266 test_when_finished "kill $! || :" &&
268 (test_write_lines a x c >new) &
269 } &&
270 test_when_finished "kill $! || :" &&
272 cat >expect <<-EOF &&
273 diff --git a/old b/new-link
274 --- a/old
275 +++ b/new-link
276 @@ -1,3 +1,3 @@
283 test_expect_code 1 git diff --no-index old new-link >actual &&
284 test_cmp expect actual
287 test_done