Sync with 'master'
[alt-git.git] / t / t4204-patch-id.sh
blob8e0f283c2b8f4d8c648914cd0051e5d53d56c669
1 #!/bin/sh
3 test_description='git patch-id'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 test_expect_success 'setup' '
12 str="ab cd ef gh ij kl mn op" &&
13 test_write_lines $str >foo &&
14 test_write_lines $str >bar &&
15 git add foo bar &&
16 git commit -a -m initial &&
17 test_write_lines $str b >foo &&
18 test_write_lines $str b >bar &&
19 git commit -a -m first &&
20 git checkout -b same main &&
21 git commit --amend -m same-msg &&
22 git checkout -b notsame main &&
23 echo c >foo &&
24 echo c >bar &&
25 git commit --amend -a -m notsame-msg &&
26 git checkout -b with_space main~ &&
27 cat >foo <<-\EOF &&
28 a b
29 c d
30 e f
31 g h
32 i j
33 k l
34 m n
36 EOF
37 cp foo bar &&
38 git add foo bar &&
39 git commit --amend -m "with spaces" &&
40 test_write_lines bar foo >bar-then-foo &&
41 test_write_lines foo bar >foo-then-bar
45 test_expect_success 'patch-id output is well-formed' '
46 git log -p -1 >log.output &&
47 git patch-id <log.output >output &&
48 grep "^$OID_REGEX $(git rev-parse HEAD)$" output
51 #calculate patch id. Make sure output is not empty.
52 calc_patch_id () {
53 patch_name="$1"
54 shift
55 git patch-id "$@" >patch-id.output &&
56 sed "s/ .*//" patch-id.output >patch-id_"$patch_name" &&
57 test_line_count -eq 1 patch-id_"$patch_name"
60 get_top_diff () {
61 git log -p -1 "$@" -O bar-then-foo --full-index --
64 get_patch_id () {
65 get_top_diff "$1" >top-diff.output &&
66 calc_patch_id <top-diff.output "$@"
69 test_expect_success 'patch-id detects equality' '
70 get_patch_id main &&
71 get_patch_id same &&
72 test_cmp patch-id_main patch-id_same
75 test_expect_success 'patch-id detects inequality' '
76 get_patch_id main &&
77 get_patch_id notsame &&
78 ! test_cmp patch-id_main patch-id_notsame
80 test_expect_success 'patch-id detects equality binary' '
81 cat >.gitattributes <<-\EOF &&
82 foo binary
83 bar binary
84 EOF
85 get_patch_id main &&
86 get_patch_id same &&
87 git log -p -1 --binary main >top-diff.output &&
88 calc_patch_id <top-diff.output main_binpatch &&
89 git log -p -1 --binary same >top-diff.output &&
90 calc_patch_id <top-diff.output same_binpatch &&
91 test_cmp patch-id_main patch-id_main_binpatch &&
92 test_cmp patch-id_same patch-id_same_binpatch &&
93 test_cmp patch-id_main patch-id_same &&
94 test_when_finished "rm .gitattributes"
97 test_expect_success 'patch-id detects inequality binary' '
98 cat >.gitattributes <<-\EOF &&
99 foo binary
100 bar binary
102 get_patch_id main &&
103 get_patch_id notsame &&
104 ! test_cmp patch-id_main patch-id_notsame &&
105 test_when_finished "rm .gitattributes"
108 test_expect_success 'patch-id supports git-format-patch output' '
109 get_patch_id main &&
110 git checkout same &&
111 git format-patch -1 --stdout >format-patch.output &&
112 calc_patch_id same <format-patch.output &&
113 test_cmp patch-id_main patch-id_same &&
114 set $(git patch-id <format-patch.output) &&
115 test "$2" = $(git rev-parse HEAD)
118 test_expect_success 'whitespace is irrelevant in footer' '
119 get_patch_id main &&
120 git checkout same &&
121 git format-patch -1 --stdout >format-patch.output &&
122 sed "s/ \$//" format-patch.output | calc_patch_id same &&
123 test_cmp patch-id_main patch-id_same
126 cmp_patch_id () {
128 test "$1" = "relevant"
129 then
130 ! test_cmp patch-id_"$2" patch-id_"$3"
131 else
132 test_cmp patch-id_"$2" patch-id_"$3"
136 test_patch_id_file_order () {
137 relevant="$1"
138 shift
139 name="order-${1}-$relevant"
140 shift
141 get_top_diff "main" >top-diff.output &&
142 calc_patch_id <top-diff.output "$name" "$@" &&
143 git checkout same &&
144 git format-patch -1 --stdout -O foo-then-bar >format-patch.output &&
145 calc_patch_id <format-patch.output "ordered-$name" "$@" &&
146 cmp_patch_id $relevant "$name" "ordered-$name"
149 test_patch_id_whitespace () {
150 relevant="$1"
151 shift
152 name="ws-${1}-$relevant"
153 shift
154 get_top_diff "main~" >top-diff.output &&
155 calc_patch_id <top-diff.output "$name" "$@" &&
156 get_top_diff "with_space" >top-diff.output &&
157 calc_patch_id <top-diff.output "ws-$name" "$@" &&
158 cmp_patch_id $relevant "$name" "ws-$name"
162 # combined test for options: add more tests here to make them
163 # run with all options
164 test_patch_id () {
165 test_patch_id_file_order "$@"
168 # small tests with detailed diagnostic for basic options.
169 test_expect_success 'file order is irrelevant with --stable' '
170 test_patch_id_file_order irrelevant --stable --stable
173 test_expect_success 'file order is relevant with --unstable' '
174 test_patch_id_file_order relevant --unstable --unstable
177 test_expect_success 'whitespace is relevant with --verbatim' '
178 test_patch_id_whitespace relevant --verbatim --verbatim
181 test_expect_success 'whitespace is irrelevant without --verbatim' '
182 test_patch_id_whitespace irrelevant --stable --stable
185 #Now test various option combinations.
186 test_expect_success 'default is unstable' '
187 test_patch_id relevant default
190 test_expect_success 'patchid.stable = true is stable' '
191 test_config patchid.stable true &&
192 test_patch_id irrelevant patchid.stable=true
195 test_expect_success 'patchid.stable = false is unstable' '
196 test_config patchid.stable false &&
197 test_patch_id relevant patchid.stable=false
200 test_expect_success 'patchid.verbatim = true is correct and stable' '
201 test_config patchid.verbatim true &&
202 test_patch_id_whitespace relevant patchid.verbatim=true &&
203 test_patch_id irrelevant patchid.verbatim=true
206 test_expect_success 'patchid.verbatim = false is unstable' '
207 test_config patchid.verbatim false &&
208 test_patch_id relevant patchid.verbatim=false
211 test_expect_success '--unstable overrides patchid.stable = true' '
212 test_config patchid.stable true &&
213 test_patch_id relevant patchid.stable=true--unstable --unstable
216 test_expect_success '--stable overrides patchid.stable = false' '
217 test_config patchid.stable false &&
218 test_patch_id irrelevant patchid.stable=false--stable --stable
221 test_expect_success '--verbatim overrides patchid.stable = false' '
222 test_config patchid.stable false &&
223 test_patch_id_whitespace relevant stable=false--verbatim --verbatim
226 test_expect_success 'patch-id supports git-format-patch MIME output' '
227 get_patch_id main &&
228 git checkout same &&
229 git format-patch -1 --attach --stdout >format-patch.output &&
230 calc_patch_id <format-patch.output same &&
231 test_cmp patch-id_main patch-id_same
234 test_expect_success 'patch-id respects config from subdir' '
235 test_config patchid.stable true &&
236 mkdir subdir &&
238 # copy these because test_patch_id() looks for them in
239 # the current directory
240 cp bar-then-foo foo-then-bar subdir &&
243 cd subdir &&
244 test_patch_id irrelevant patchid.stable=true
248 test_expect_success 'patch-id handles no-nl-at-eof markers' '
249 cat >nonl <<-\EOF &&
250 diff --git i/a w/a
251 index e69de29..2e65efe 100644
252 --- i/a
253 +++ w/a
254 @@ -0,0 +1 @@
256 \ No newline at end of file
257 diff --git i/b w/b
258 index e69de29..6178079 100644
259 --- i/b
260 +++ w/b
261 @@ -0,0 +1 @@
264 cat >withnl <<-\EOF &&
265 diff --git i/a w/a
266 index e69de29..7898192 100644
267 --- i/a
268 +++ w/a
269 @@ -0,0 +1 @@
271 diff --git i/b w/b
272 index e69de29..6178079 100644
273 --- i/b
274 +++ w/b
275 @@ -0,0 +1 @@
278 calc_patch_id nonl <nonl &&
279 calc_patch_id withnl <withnl &&
280 test_cmp patch-id_nonl patch-id_withnl &&
281 calc_patch_id nonl-inc-ws --verbatim <nonl &&
282 calc_patch_id withnl-inc-ws --verbatim <withnl &&
283 ! test_cmp patch-id_nonl-inc-ws patch-id_withnl-inc-ws
286 test_expect_success 'patch-id handles diffs with one line of before/after' '
287 cat >diffu1 <<-\EOF &&
288 diff --git a/bar b/bar
289 index bdaf90f..31051f6 100644
290 --- a/bar
291 +++ b/bar
292 @@ -2 +2,2 @@
295 diff --git a/car b/car
296 index 00750ed..2ae5e34 100644
297 --- a/car
298 +++ b/car
299 @@ -1 +1,2 @@
302 diff --git a/foo b/foo
303 index e439850..7146eb8 100644
304 --- a/foo
305 +++ b/foo
306 @@ -2 +2,2 @@
310 calc_patch_id diffu1 <diffu1 &&
311 test_config patchid.stable true &&
312 calc_patch_id diffu1stable <diffu1
315 test_expect_failure 'patch-id computes same ID with different object hashes' '
316 test_when_finished "rm -rf repo-sha1 repo-sha256" &&
318 cat >diff <<-\EOF &&
319 diff --git a/bar b/bar
320 index bdaf90f..31051f6 100644
321 --- a/bar
322 +++ b/bar
323 @@ -2 +2,2 @@
328 git init --object-format=sha1 repo-sha1 &&
329 git -C repo-sha1 patch-id <diff >patch-id-sha1 &&
330 git init --object-format=sha256 repo-sha256 &&
331 git -C repo-sha256 patch-id <diff >patch-id-sha256 &&
332 test_cmp patch-id-sha1 patch-id-sha256
335 test_expect_success 'patch-id without repository' '
336 cat >diff <<-\EOF &&
337 diff --git a/bar b/bar
338 index bdaf90f..31051f6 100644
339 --- a/bar
340 +++ b/bar
341 @@ -2 +2,2 @@
345 nongit git patch-id <diff
348 test_done