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