]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4045-diff-relative.sh
The third batch
[thirdparty/git.git] / t / t4045-diff-relative.sh
CommitLineData
d8faea9d
JN
1#!/bin/sh
2
3test_description='diff --relative tests'
03267e86
ÆAB
4
5TEST_PASSES_SANITIZE_LEAK=true
d8faea9d
JN
6. ./test-lib.sh
7
8test_expect_success 'setup' '
9 git commit --allow-empty -m empty &&
10 echo content >file1 &&
11 mkdir subdir &&
12 echo other content >subdir/file2 &&
c28ded83
LA
13 blob_file1=$(git hash-object file1) &&
14 blob_file2=$(git hash-object subdir/file2) &&
d8faea9d
JN
15 git add . &&
16 git commit -m one
17'
18
f1e4fb24
JH
19check_diff () {
20 dir=$1
21 shift
22 expect=$1
23 shift
c28ded83 24 short_blob=$(git rev-parse --short $blob_file2)
f1e4fb24
JH
25 cat >expected <<-EOF
26 diff --git a/$expect b/$expect
27 new file mode 100644
5c024287 28 index 0000000..$short_blob
f1e4fb24
JH
29 --- /dev/null
30 +++ b/$expect
31 @@ -0,0 +1 @@
32 +other content
33 EOF
34 test_expect_success "-p $*" "
35 git -C '$dir' diff -p $* HEAD^ >actual &&
36 test_cmp expected actual
37 "
d8faea9d
JN
38}
39
f1e4fb24
JH
40check_numstat () {
41 dir=$1
42 shift
43 expect=$1
44 shift
45 cat >expected <<-EOF
46 1 0 $expect
47 EOF
48 test_expect_success "--numstat $*" "
49 echo '1 0 $expect' >expected &&
50 git -C '$dir' diff --numstat $* HEAD^ >actual &&
51 test_cmp expected actual
52 "
6dd88832
JN
53}
54
f1e4fb24
JH
55check_stat () {
56 dir=$1
57 shift
58 expect=$1
59 shift
60 cat >expected <<-EOF
61 $expect | 1 +
62 1 file changed, 1 insertion(+)
63 EOF
64 test_expect_success "--stat $*" "
65 git -C '$dir' diff --stat $* HEAD^ >actual &&
1108cea7 66 test_cmp expected actual
f1e4fb24 67 "
d8faea9d
JN
68}
69
f1e4fb24
JH
70check_raw () {
71 dir=$1
72 shift
73 expect=$1
74 shift
75 cat >expected <<-EOF
c28ded83 76 :000000 100644 $ZERO_OID $blob_file2 A $expect
f1e4fb24
JH
77 EOF
78 test_expect_success "--raw $*" "
79 git -C '$dir' diff --no-abbrev --raw $* HEAD^ >actual &&
80 test_cmp expected actual
81 "
d8faea9d
JN
82}
83
6d7c17ec
JK
84for type in diff numstat stat raw
85do
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
d8faea9d
JN
90done
91
c28ded83
LA
92check_diff_relative_option () {
93 dir=$1
94 shift
95 expect=$1
96 shift
97 relative_opt=$1
98 shift
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
103 new file mode 100644
104 index 0000000..\$short_blob
105 --- /dev/null
106 +++ b/$expect
107 @@ -0,0 +1 @@
108 +other content
109 EOF
110 test_config -C $dir diff.relative $relative_opt &&
111 git -C '$dir' diff -p $* HEAD^ >actual &&
112 test_cmp expected actual
113 "
114}
115
116check_diff_no_relative_option () {
117 dir=$1
118 shift
119 expect=$1
120 shift
121 relative_opt=$1
122 shift
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
128 new file mode 100644
129 index 0000000..\$short_blob_file1
130 --- /dev/null
131 +++ b/file1
132 @@ -0,0 +1 @@
133 +content
134 diff --git a/$expect b/$expect
135 new file mode 100644
136 index 0000000..\$short_blob_file2
137 --- /dev/null
138 +++ b/$expect
139 @@ -0,0 +1 @@
140 +other content
141 EOF
142 test_config -C $dir diff.relative $relative_opt &&
143 git -C '$dir' diff -p $* HEAD^ >actual &&
144 test_cmp expected actual
145 "
146}
147
148check_diff_no_relative_option . subdir/file2 false
149check_diff_no_relative_option . subdir/file2 true --no-relative
150check_diff_no_relative_option . subdir/file2 false --no-relative
151check_diff_no_relative_option subdir subdir/file2 false
152check_diff_no_relative_option subdir subdir/file2 true --no-relative
153check_diff_no_relative_option subdir subdir/file2 false --no-relative
154
155check_diff_relative_option . file2 false --relative=subdir/
156check_diff_relative_option . file2 false --relative=subdir
157check_diff_relative_option . file2 true --relative=subdir/
158check_diff_relative_option . file2 true --relative=subdir
159check_diff_relative_option subdir file2 false --relative
160check_diff_relative_option subdir file2 true --relative
161check_diff_relative_option subdir file2 true
162check_diff_relative_option subdir file2 false --no-relative --relative
163check_diff_relative_option subdir file2 true --no-relative --relative
164check_diff_relative_option . file2 false --no-relative --relative=subdir
165check_diff_relative_option . file2 true --no-relative --relative=subdir
166
a0f83e77
JK
167test_expect_success 'external diff with --relative' '
168 test_when_finished "git reset --hard" &&
169 echo changed >file1 &&
170 echo changed >subdir/file2 &&
171
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" &&
177 echo "--- a/$1" &&
178 echo "+++ b/$1" &&
179 echo "@@ -1 +0,0 @@" &&
180 sed "s/^/-/" "$2" &&
181 sed "s/^/+/" "$5"
182 EOF
183
184 cat >expect <<-\EOF &&
185 diff a/file2 b/file2
186 --- a/file2
187 +++ b/file2
188 @@ -1 +0,0 @@
189 -other content
190 +changed
191 EOF
192 GIT_EXTERNAL_DIFF=./mydiff git diff --relative=subdir >actual &&
193 test_cmp expect actual
194'
195
8174627b
ĐTCD
196test_expect_success 'setup diff --relative unmerged' '
197 test_commit zero file0 &&
198 test_commit base subdir/file0 &&
199 git switch -c br1 &&
200 test_commit one file0 &&
201 test_commit sub1 subdir/file0 &&
202 git switch -c br2 base &&
203 test_commit two file0 &&
204 git switch -c br3 &&
205 test_commit sub3 subdir/file0
206'
207
208test_expect_success 'diff --relative without change in subdir' '
209 git switch br2 &&
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
216'
217
218test_expect_success 'diff --relative --name-only with change in subdir' '
219 git switch br3 &&
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
225'
226
227test_expect_failure 'diff --relative with change in subdir' '
228 git switch br3 &&
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 &&
234 diff --cc file0
235 index $br3_blob,$br1_blob..0000000
236 --- a/file0
237 +++ b/file0
238 @@@ -1,1 -1,1 +1,5 @@@
239 ++<<<<<<< HEAD
240 +sub3
241 ++=======
242 + sub1
243 ++>>>>>>> br1
244 EOF
245 git -C subdir diff --relative >out &&
246 test_cmp expected out
247'
248
d8faea9d 249test_done