3 test_description
='diff --relative tests'
5 TEST_PASSES_SANITIZE_LEAK
=true
8 test_expect_success
'setup' '
9 git commit --allow-empty -m empty &&
10 echo content >file1 &&
12 echo other content >subdir/file2 &&
13 blob_file1=$(git hash-object file1) &&
14 blob_file2=$(git hash-object subdir/file2) &&
24 short_blob
=$
(git rev-parse
--short $blob_file2)
26 diff --git a/$expect b/$expect
28 index 0000000..$short_blob
34 test_expect_success
"-p $*" "
35 git -C '$dir' diff -p $* HEAD^ >actual &&
36 test_cmp expected actual
48 test_expect_success
"--numstat $*" "
49 echo '1 0 $expect' >expected &&
50 git -C '$dir' diff --numstat $* HEAD^ >actual &&
51 test_cmp expected actual
62 1 file changed, 1 insertion(+)
64 test_expect_success
"--stat $*" "
65 git -C '$dir' diff --stat $* HEAD^ >actual &&
66 test_cmp expected actual
76 :000000 100644 $ZERO_OID $blob_file2 A $expect
78 test_expect_success
"--raw $*" "
79 git -C '$dir' diff --no-abbrev --raw $* HEAD^ >actual &&
80 test_cmp expected actual
84 for type in diff numstat stat raw
86 check_
$type . file2
--relative=subdir
/
87 check_
$type . file2
--relative=subdir
88 check_
$type subdir file2
--relative
89 check_
$type . dir
/file2
--relative=sub
92 check_diff_relative_option
() {
99 test_expect_success
"config diff.relative $relative_opt -p $*" "
100 short_blob=\$(git rev-parse --short $blob_file2) &&
101 cat >expected <<-EOF &&
102 diff --git a/$expect b/$expect
104 index 0000000..\$short_blob
110 test_config -C $dir diff.relative $relative_opt &&
111 git -C '$dir' diff -p $* HEAD^ >actual &&
112 test_cmp expected actual
116 check_diff_no_relative_option
() {
123 test_expect_success
"config diff.relative $relative_opt -p $*" "
124 short_blob_file1=\$(git rev-parse --short $blob_file1) &&
125 short_blob_file2=\$(git rev-parse --short $blob_file2) &&
126 cat >expected <<-EOF &&
127 diff --git a/file1 b/file1
129 index 0000000..\$short_blob_file1
134 diff --git a/$expect b/$expect
136 index 0000000..\$short_blob_file2
142 test_config -C $dir diff.relative $relative_opt &&
143 git -C '$dir' diff -p $* HEAD^ >actual &&
144 test_cmp expected actual
148 check_diff_no_relative_option . subdir
/file2 false
149 check_diff_no_relative_option . subdir
/file2 true
--no-relative
150 check_diff_no_relative_option . subdir
/file2 false
--no-relative
151 check_diff_no_relative_option subdir subdir
/file2 false
152 check_diff_no_relative_option subdir subdir
/file2 true
--no-relative
153 check_diff_no_relative_option subdir subdir
/file2 false
--no-relative
155 check_diff_relative_option . file2 false
--relative=subdir
/
156 check_diff_relative_option . file2 false
--relative=subdir
157 check_diff_relative_option . file2 true
--relative=subdir
/
158 check_diff_relative_option . file2 true
--relative=subdir
159 check_diff_relative_option subdir file2 false
--relative
160 check_diff_relative_option subdir file2 true
--relative
161 check_diff_relative_option subdir file2 true
162 check_diff_relative_option subdir file2 false
--no-relative --relative
163 check_diff_relative_option subdir file2 true
--no-relative --relative
164 check_diff_relative_option . file2 false
--no-relative --relative=subdir
165 check_diff_relative_option . file2 true
--no-relative --relative=subdir
167 test_expect_success
'external diff with --relative' '
168 test_when_finished "git reset --hard" &&
169 echo changed >file1 &&
170 echo changed >subdir/file2 &&
172 write_script mydiff <<-\EOF &&
173 # hacky pretend diff; the goal here is just to make sure we got
174 # passed sensible input that we _could_ diff, without relying on
175 # the specific output of a system diff tool.
176 echo "diff a/$1 b/$1" &&
179 echo "@@ -1 +0,0 @@" &&
184 cat >expect <<-\EOF &&
192 GIT_EXTERNAL_DIFF=./mydiff git diff --relative=subdir >actual &&
193 test_cmp expect actual
196 test_expect_success
'setup diff --relative unmerged' '
197 test_commit zero file0 &&
198 test_commit base subdir/file0 &&
200 test_commit one file0 &&
201 test_commit sub1 subdir/file0 &&
202 git switch -c br2 base &&
203 test_commit two file0 &&
205 test_commit sub3 subdir/file0
208 test_expect_success
'diff --relative without change in subdir' '
210 test_when_finished "git merge --abort" &&
211 test_must_fail git merge one &&
212 git -C subdir diff --relative >out &&
213 test_must_be_empty out &&
214 git -C subdir diff --relative --name-only >out &&
215 test_must_be_empty out
218 test_expect_success
'diff --relative --name-only with change in subdir' '
220 test_when_finished "git merge --abort" &&
221 test_must_fail git merge sub1 &&
222 test_write_lines file0 file0 >expected &&
223 git -C subdir diff --relative --name-only >out &&
224 test_cmp expected out
227 test_expect_failure
'diff --relative with change in subdir' '
229 br1_blob=$(git rev-parse --short --verify br1:subdir/file0) &&
230 br3_blob=$(git rev-parse --short --verify br3:subdir/file0) &&
231 test_when_finished "git merge --abort" &&
232 test_must_fail git merge br1 &&
233 cat >expected <<-EOF &&
235 index $br3_blob,$br1_blob..0000000
238 @@@ -1,1 -1,1 +1,5 @@@
245 git -C subdir diff --relative >out &&
246 test_cmp expected out