clone_submodule: avoid using `access()` on directories
[git.git] / t / t4045-diff-relative.sh
blob5e1bbb57e725f08f347ba5baaf93c159c364bb97
1 #!/bin/sh
3 test_description='diff --relative tests'
4 . ./test-lib.sh
6 test_expect_success 'setup' '
7 git commit --allow-empty -m empty &&
8 echo content >file1 &&
9 mkdir subdir &&
10 echo other content >subdir/file2 &&
11 blob_file1=$(git hash-object file1) &&
12 blob_file2=$(git hash-object subdir/file2) &&
13 git add . &&
14 git commit -m one
17 check_diff () {
18 dir=$1
19 shift
20 expect=$1
21 shift
22 short_blob=$(git rev-parse --short $blob_file2)
23 cat >expected <<-EOF
24 diff --git a/$expect b/$expect
25 new file mode 100644
26 index 0000000..$short_blob
27 --- /dev/null
28 +++ b/$expect
29 @@ -0,0 +1 @@
30 +other content
31 EOF
32 test_expect_success "-p $*" "
33 git -C '$dir' diff -p $* HEAD^ >actual &&
34 test_cmp expected actual
38 check_numstat () {
39 dir=$1
40 shift
41 expect=$1
42 shift
43 cat >expected <<-EOF
44 1 0 $expect
45 EOF
46 test_expect_success "--numstat $*" "
47 echo '1 0 $expect' >expected &&
48 git -C '$dir' diff --numstat $* HEAD^ >actual &&
49 test_cmp expected actual
53 check_stat () {
54 dir=$1
55 shift
56 expect=$1
57 shift
58 cat >expected <<-EOF
59 $expect | 1 +
60 1 file changed, 1 insertion(+)
61 EOF
62 test_expect_success "--stat $*" "
63 git -C '$dir' diff --stat $* HEAD^ >actual &&
64 test_cmp expected actual
68 check_raw () {
69 dir=$1
70 shift
71 expect=$1
72 shift
73 cat >expected <<-EOF
74 :000000 100644 $ZERO_OID $blob_file2 A $expect
75 EOF
76 test_expect_success "--raw $*" "
77 git -C '$dir' diff --no-abbrev --raw $* HEAD^ >actual &&
78 test_cmp expected actual
82 for type in diff numstat stat raw
84 check_$type . file2 --relative=subdir/
85 check_$type . file2 --relative=subdir
86 check_$type subdir file2 --relative
87 check_$type . dir/file2 --relative=sub
88 done
90 check_diff_relative_option () {
91 dir=$1
92 shift
93 expect=$1
94 shift
95 relative_opt=$1
96 shift
97 test_expect_success "config diff.relative $relative_opt -p $*" "
98 short_blob=\$(git rev-parse --short $blob_file2) &&
99 cat >expected <<-EOF &&
100 diff --git a/$expect b/$expect
101 new file mode 100644
102 index 0000000..\$short_blob
103 --- /dev/null
104 +++ b/$expect
105 @@ -0,0 +1 @@
106 +other content
108 test_config -C $dir diff.relative $relative_opt &&
109 git -C '$dir' diff -p $* HEAD^ >actual &&
110 test_cmp expected actual
114 check_diff_no_relative_option () {
115 dir=$1
116 shift
117 expect=$1
118 shift
119 relative_opt=$1
120 shift
121 test_expect_success "config diff.relative $relative_opt -p $*" "
122 short_blob_file1=\$(git rev-parse --short $blob_file1) &&
123 short_blob_file2=\$(git rev-parse --short $blob_file2) &&
124 cat >expected <<-EOF &&
125 diff --git a/file1 b/file1
126 new file mode 100644
127 index 0000000..\$short_blob_file1
128 --- /dev/null
129 +++ b/file1
130 @@ -0,0 +1 @@
131 +content
132 diff --git a/$expect b/$expect
133 new file mode 100644
134 index 0000000..\$short_blob_file2
135 --- /dev/null
136 +++ b/$expect
137 @@ -0,0 +1 @@
138 +other content
140 test_config -C $dir diff.relative $relative_opt &&
141 git -C '$dir' diff -p $* HEAD^ >actual &&
142 test_cmp expected actual
146 check_diff_no_relative_option . subdir/file2 false
147 check_diff_no_relative_option . subdir/file2 true --no-relative
148 check_diff_no_relative_option . subdir/file2 false --no-relative
149 check_diff_no_relative_option subdir subdir/file2 false
150 check_diff_no_relative_option subdir subdir/file2 true --no-relative
151 check_diff_no_relative_option subdir subdir/file2 false --no-relative
153 check_diff_relative_option . file2 false --relative=subdir/
154 check_diff_relative_option . file2 false --relative=subdir
155 check_diff_relative_option . file2 true --relative=subdir/
156 check_diff_relative_option . file2 true --relative=subdir
157 check_diff_relative_option subdir file2 false --relative
158 check_diff_relative_option subdir file2 true --relative
159 check_diff_relative_option subdir file2 true
160 check_diff_relative_option subdir file2 false --no-relative --relative
161 check_diff_relative_option subdir file2 true --no-relative --relative
162 check_diff_relative_option . file2 false --no-relative --relative=subdir
163 check_diff_relative_option . file2 true --no-relative --relative=subdir
165 test_expect_success 'external diff with --relative' '
166 test_when_finished "git reset --hard" &&
167 echo changed >file1 &&
168 echo changed >subdir/file2 &&
170 write_script mydiff <<-\EOF &&
171 # hacky pretend diff; the goal here is just to make sure we got
172 # passed sensible input that we _could_ diff, without relying on
173 # the specific output of a system diff tool.
174 echo "diff a/$1 b/$1" &&
175 echo "--- a/$1" &&
176 echo "+++ b/$1" &&
177 echo "@@ -1 +0,0 @@" &&
178 sed "s/^/-/" "$2" &&
179 sed "s/^/+/" "$5"
182 cat >expect <<-\EOF &&
183 diff a/file2 b/file2
184 --- a/file2
185 +++ b/file2
186 @@ -1 +0,0 @@
187 -other content
188 +changed
190 GIT_EXTERNAL_DIFF=./mydiff git diff --relative=subdir >actual &&
191 test_cmp expect actual
194 test_expect_success 'setup diff --relative unmerged' '
195 test_commit zero file0 &&
196 test_commit base subdir/file0 &&
197 git switch -c br1 &&
198 test_commit one file0 &&
199 test_commit sub1 subdir/file0 &&
200 git switch -c br2 base &&
201 test_commit two file0 &&
202 git switch -c br3 &&
203 test_commit sub3 subdir/file0
206 test_expect_success 'diff --relative without change in subdir' '
207 git switch br2 &&
208 test_when_finished "git merge --abort" &&
209 test_must_fail git merge one &&
210 git -C subdir diff --relative >out &&
211 test_must_be_empty out &&
212 git -C subdir diff --relative --name-only >out &&
213 test_must_be_empty out
216 test_expect_success 'diff --relative --name-only with change in subdir' '
217 git switch br3 &&
218 test_when_finished "git merge --abort" &&
219 test_must_fail git merge sub1 &&
220 test_write_lines file0 file0 >expected &&
221 git -C subdir diff --relative --name-only >out &&
222 test_cmp expected out
225 test_expect_failure 'diff --relative with change in subdir' '
226 git switch br3 &&
227 br1_blob=$(git rev-parse --short --verify br1:subdir/file0) &&
228 br3_blob=$(git rev-parse --short --verify br3:subdir/file0) &&
229 test_when_finished "git merge --abort" &&
230 test_must_fail git merge br1 &&
231 cat >expected <<-EOF &&
232 diff --cc file0
233 index $br3_blob,$br1_blob..0000000
234 --- a/file0
235 +++ b/file0
236 @@@ -1,1 -1,1 +1,5 @@@
237 ++<<<<<<< HEAD
238 +sub3
239 ++=======
240 + sub1
241 ++>>>>>>> br1
243 git -C subdir diff --relative >out &&
244 test_cmp expected out
247 test_done