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
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 &&
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 &&
25 git commit --amend -a -m notsame-msg &&
26 git checkout -b with_space main~ &&
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.
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"
61 git log
-p -1 "$@" -O bar-then-foo
--full-index --
65 get_top_diff
"$1" >top-diff.output
&&
66 calc_patch_id
<top-diff.output
"$@"
69 test_expect_success
'patch-id detects equality' '
72 test_cmp patch-id_main patch-id_same
75 test_expect_success
'patch-id detects inequality' '
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 &&
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 &&
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' '
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' '
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
128 test "$1" = "relevant"
130 ! test_cmp patch-id_
"$2" patch-id_
"$3"
132 test_cmp patch-id_
"$2" patch-id_
"$3"
136 test_patch_id_file_order
() {
139 name
="order-${1}-$relevant"
141 get_top_diff
"main" >top-diff.output
&&
142 calc_patch_id
<top-diff.output
"$name" "$@" &&
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
() {
152 name
="ws-${1}-$relevant"
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
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' '
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 &&
238 # copy these because test_patch_id() looks for them in
239 # the current directory
240 cp bar-then-foo foo-then-bar subdir &&
244 test_patch_id irrelevant patchid.stable=true
248 test_expect_success
'patch-id handles no-nl-at-eof markers' '
251 index e69de29..2e65efe 100644
256 \ No newline at end of file
258 index e69de29..6178079 100644
264 cat >withnl <<-\EOF &&
266 index e69de29..7898192 100644
272 index e69de29..6178079 100644
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
295 diff --git a/car b/car
296 index 00750ed..2ae5e34 100644
302 diff --git a/foo b/foo
303 index e439850..7146eb8 100644
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" &&
319 diff --git a/bar b/bar
320 index bdaf90f..31051f6 100644
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' '
337 diff --git a/bar b/bar
338 index bdaf90f..31051f6 100644
345 nongit git patch-id <diff