commit-graph: factor out chain opening function
[git.git] / t / t4204-patch-id.sh
bloba7fa94ce0a212e5062a6cc0356261d29af973e32
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-lib.sh
10 test_expect_success 'setup' '
11 str="ab cd ef gh ij kl mn op" &&
12 test_write_lines $str >foo &&
13 test_write_lines $str >bar &&
14 git add foo bar &&
15 git commit -a -m initial &&
16 test_write_lines $str b >foo &&
17 test_write_lines $str b >bar &&
18 git commit -a -m first &&
19 git checkout -b same main &&
20 git commit --amend -m same-msg &&
21 git checkout -b notsame main &&
22 echo c >foo &&
23 echo c >bar &&
24 git commit --amend -a -m notsame-msg &&
25 git checkout -b with_space main~ &&
26 cat >foo <<-\EOF &&
27 a b
28 c d
29 e f
30 g h
31 i j
32 k l
33 m n
35 EOF
36 cp foo bar &&
37 git add foo bar &&
38 git commit --amend -m "with spaces" &&
39 test_write_lines bar foo >bar-then-foo &&
40 test_write_lines foo bar >foo-then-bar
44 test_expect_success 'patch-id output is well-formed' '
45 git log -p -1 >log.output &&
46 git patch-id <log.output >output &&
47 grep "^$OID_REGEX $(git rev-parse HEAD)$" output
50 #calculate patch id. Make sure output is not empty.
51 calc_patch_id () {
52 patch_name="$1"
53 shift
54 git patch-id "$@" >patch-id.output &&
55 sed "s/ .*//" patch-id.output >patch-id_"$patch_name" &&
56 test_line_count -eq 1 patch-id_"$patch_name"
59 get_top_diff () {
60 git log -p -1 "$@" -O bar-then-foo --full-index --
63 get_patch_id () {
64 get_top_diff "$1" >top-diff.output &&
65 calc_patch_id <top-diff.output "$@"
68 test_expect_success 'patch-id detects equality' '
69 get_patch_id main &&
70 get_patch_id same &&
71 test_cmp patch-id_main patch-id_same
74 test_expect_success 'patch-id detects inequality' '
75 get_patch_id main &&
76 get_patch_id notsame &&
77 ! test_cmp patch-id_main patch-id_notsame
79 test_expect_success 'patch-id detects equality binary' '
80 cat >.gitattributes <<-\EOF &&
81 foo binary
82 bar binary
83 EOF
84 get_patch_id main &&
85 get_patch_id same &&
86 git log -p -1 --binary main >top-diff.output &&
87 calc_patch_id <top-diff.output main_binpatch &&
88 git log -p -1 --binary same >top-diff.output &&
89 calc_patch_id <top-diff.output same_binpatch &&
90 test_cmp patch-id_main patch-id_main_binpatch &&
91 test_cmp patch-id_same patch-id_same_binpatch &&
92 test_cmp patch-id_main patch-id_same &&
93 test_when_finished "rm .gitattributes"
96 test_expect_success 'patch-id detects inequality binary' '
97 cat >.gitattributes <<-\EOF &&
98 foo binary
99 bar binary
101 get_patch_id main &&
102 get_patch_id notsame &&
103 ! test_cmp patch-id_main patch-id_notsame &&
104 test_when_finished "rm .gitattributes"
107 test_expect_success 'patch-id supports git-format-patch output' '
108 get_patch_id main &&
109 git checkout same &&
110 git format-patch -1 --stdout >format-patch.output &&
111 calc_patch_id same <format-patch.output &&
112 test_cmp patch-id_main patch-id_same &&
113 set $(git patch-id <format-patch.output) &&
114 test "$2" = $(git rev-parse HEAD)
117 test_expect_success 'whitespace is irrelevant in footer' '
118 get_patch_id main &&
119 git checkout same &&
120 git format-patch -1 --stdout >format-patch.output &&
121 sed "s/ \$//" format-patch.output | calc_patch_id same &&
122 test_cmp patch-id_main patch-id_same
125 cmp_patch_id () {
127 test "$1" = "relevant"
128 then
129 ! test_cmp patch-id_"$2" patch-id_"$3"
130 else
131 test_cmp patch-id_"$2" patch-id_"$3"
135 test_patch_id_file_order () {
136 relevant="$1"
137 shift
138 name="order-${1}-$relevant"
139 shift
140 get_top_diff "main" >top-diff.output &&
141 calc_patch_id <top-diff.output "$name" "$@" &&
142 git checkout same &&
143 git format-patch -1 --stdout -O foo-then-bar >format-patch.output &&
144 calc_patch_id <format-patch.output "ordered-$name" "$@" &&
145 cmp_patch_id $relevant "$name" "ordered-$name"
148 test_patch_id_whitespace () {
149 relevant="$1"
150 shift
151 name="ws-${1}-$relevant"
152 shift
153 get_top_diff "main~" >top-diff.output &&
154 calc_patch_id <top-diff.output "$name" "$@" &&
155 get_top_diff "with_space" >top-diff.output &&
156 calc_patch_id <top-diff.output "ws-$name" "$@" &&
157 cmp_patch_id $relevant "$name" "ws-$name"
161 # combined test for options: add more tests here to make them
162 # run with all options
163 test_patch_id () {
164 test_patch_id_file_order "$@"
167 # small tests with detailed diagnostic for basic options.
168 test_expect_success 'file order is irrelevant with --stable' '
169 test_patch_id_file_order irrelevant --stable --stable
172 test_expect_success 'file order is relevant with --unstable' '
173 test_patch_id_file_order relevant --unstable --unstable
176 test_expect_success 'whitespace is relevant with --verbatim' '
177 test_patch_id_whitespace relevant --verbatim --verbatim
180 test_expect_success 'whitespace is irrelevant without --verbatim' '
181 test_patch_id_whitespace irrelevant --stable --stable
184 #Now test various option combinations.
185 test_expect_success 'default is unstable' '
186 test_patch_id relevant default
189 test_expect_success 'patchid.stable = true is stable' '
190 test_config patchid.stable true &&
191 test_patch_id irrelevant patchid.stable=true
194 test_expect_success 'patchid.stable = false is unstable' '
195 test_config patchid.stable false &&
196 test_patch_id relevant patchid.stable=false
199 test_expect_success 'patchid.verbatim = true is correct and stable' '
200 test_config patchid.verbatim true &&
201 test_patch_id_whitespace relevant patchid.verbatim=true &&
202 test_patch_id irrelevant patchid.verbatim=true
205 test_expect_success 'patchid.verbatim = false is unstable' '
206 test_config patchid.verbatim false &&
207 test_patch_id relevant patchid.verbatim=false
210 test_expect_success '--unstable overrides patchid.stable = true' '
211 test_config patchid.stable true &&
212 test_patch_id relevant patchid.stable=true--unstable --unstable
215 test_expect_success '--stable overrides patchid.stable = false' '
216 test_config patchid.stable false &&
217 test_patch_id irrelevant patchid.stable=false--stable --stable
220 test_expect_success '--verbatim overrides patchid.stable = false' '
221 test_config patchid.stable false &&
222 test_patch_id_whitespace relevant stable=false--verbatim --verbatim
225 test_expect_success 'patch-id supports git-format-patch MIME output' '
226 get_patch_id main &&
227 git checkout same &&
228 git format-patch -1 --attach --stdout >format-patch.output &&
229 calc_patch_id <format-patch.output same &&
230 test_cmp patch-id_main patch-id_same
233 test_expect_success 'patch-id respects config from subdir' '
234 test_config patchid.stable true &&
235 mkdir subdir &&
237 # copy these because test_patch_id() looks for them in
238 # the current directory
239 cp bar-then-foo foo-then-bar subdir &&
242 cd subdir &&
243 test_patch_id irrelevant patchid.stable=true
247 test_expect_success 'patch-id handles no-nl-at-eof markers' '
248 cat >nonl <<-\EOF &&
249 diff --git i/a w/a
250 index e69de29..2e65efe 100644
251 --- i/a
252 +++ w/a
253 @@ -0,0 +1 @@
255 \ No newline at end of file
256 diff --git i/b w/b
257 index e69de29..6178079 100644
258 --- i/b
259 +++ w/b
260 @@ -0,0 +1 @@
263 cat >withnl <<-\EOF &&
264 diff --git i/a w/a
265 index e69de29..7898192 100644
266 --- i/a
267 +++ w/a
268 @@ -0,0 +1 @@
270 diff --git i/b w/b
271 index e69de29..6178079 100644
272 --- i/b
273 +++ w/b
274 @@ -0,0 +1 @@
277 calc_patch_id nonl <nonl &&
278 calc_patch_id withnl <withnl &&
279 test_cmp patch-id_nonl patch-id_withnl &&
280 calc_patch_id nonl-inc-ws --verbatim <nonl &&
281 calc_patch_id withnl-inc-ws --verbatim <withnl &&
282 ! test_cmp patch-id_nonl-inc-ws patch-id_withnl-inc-ws
285 test_expect_success 'patch-id handles diffs with one line of before/after' '
286 cat >diffu1 <<-\EOF &&
287 diff --git a/bar b/bar
288 index bdaf90f..31051f6 100644
289 --- a/bar
290 +++ b/bar
291 @@ -2 +2,2 @@
294 diff --git a/car b/car
295 index 00750ed..2ae5e34 100644
296 --- a/car
297 +++ b/car
298 @@ -1 +1,2 @@
301 diff --git a/foo b/foo
302 index e439850..7146eb8 100644
303 --- a/foo
304 +++ b/foo
305 @@ -2 +2,2 @@
309 calc_patch_id diffu1 <diffu1 &&
310 test_config patchid.stable true &&
311 calc_patch_id diffu1stable <diffu1
313 test_done