Merge branch 'es/worktree-repair-copied' into cw/worktrees-relative
[git/gitster.git] / t / t4045-diff-relative.sh
blob9b46c4c1befece1f3e231b9672291b7a538c3f6f
1 #!/bin/sh
3 test_description='diff --relative tests'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 test_expect_success 'setup' '
9 git commit --allow-empty -m empty &&
10 echo content >file1 &&
11 mkdir subdir &&
12 echo other content >subdir/file2 &&
13 blob_file1=$(git hash-object file1) &&
14 blob_file2=$(git hash-object subdir/file2) &&
15 git add . &&
16 git commit -m one
19 check_diff () {
20 dir=$1
21 shift
22 expect=$1
23 shift
24 short_blob=$(git rev-parse --short $blob_file2)
25 cat >expected <<-EOF
26 diff --git a/$expect b/$expect
27 new file mode 100644
28 index 0000000..$short_blob
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
40 check_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
55 check_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 &&
66 test_cmp expected actual
70 check_raw () {
71 dir=$1
72 shift
73 expect=$1
74 shift
75 cat >expected <<-EOF
76 :000000 100644 $ZERO_OID $blob_file2 A $expect
77 EOF
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
90 done
92 check_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
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 () {
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
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" &&
177 echo "--- a/$1" &&
178 echo "+++ b/$1" &&
179 echo "@@ -1 +0,0 @@" &&
180 sed "s/^/-/" "$2" &&
181 sed "s/^/+/" "$5"
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
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 &&
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
208 test_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
218 test_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
227 test_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
245 git -C subdir diff --relative >out &&
246 test_cmp expected out
249 test_done