3 test_description
='git patch-id'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
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 &&
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 &&
24 git commit --amend -a -m notsame-msg &&
25 git checkout -b with_space main~ &&
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.
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"
60 git log
-p -1 "$@" -O bar-then-foo
--full-index --
64 get_top_diff
"$1" >top-diff.output
&&
65 calc_patch_id
<top-diff.output
"$@"
68 test_expect_success
'patch-id detects equality' '
71 test_cmp patch-id_main patch-id_same
74 test_expect_success
'patch-id detects inequality' '
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 &&
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 &&
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' '
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' '
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
127 test "$1" = "relevant"
129 ! test_cmp patch-id_
"$2" patch-id_
"$3"
131 test_cmp patch-id_
"$2" patch-id_
"$3"
135 test_patch_id_file_order
() {
138 name
="order-${1}-$relevant"
140 get_top_diff
"main" >top-diff.output
&&
141 calc_patch_id
<top-diff.output
"$name" "$@" &&
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
() {
151 name
="ws-${1}-$relevant"
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
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' '
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 &&
237 # copy these because test_patch_id() looks for them in
238 # the current directory
239 cp bar-then-foo foo-then-bar subdir &&
243 test_patch_id irrelevant patchid.stable=true
247 test_expect_success
'patch-id handles no-nl-at-eof markers' '
250 index e69de29..2e65efe 100644
255 \ No newline at end of file
257 index e69de29..6178079 100644
263 cat >withnl <<-\EOF &&
265 index e69de29..7898192 100644
271 index e69de29..6178079 100644
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
294 diff --git a/car b/car
295 index 00750ed..2ae5e34 100644
301 diff --git a/foo b/foo
302 index e439850..7146eb8 100644
309 calc_patch_id diffu1 <diffu1 &&
310 test_config patchid.stable true &&
311 calc_patch_id diffu1stable <diffu1
314 test_expect_failure
'patch-id computes same ID with different object hashes' '
315 test_when_finished "rm -rf repo-sha1 repo-sha256" &&
318 diff --git a/bar b/bar
319 index bdaf90f..31051f6 100644
327 git init --object-format=sha1 repo-sha1 &&
328 git -C repo-sha1 patch-id <diff >patch-id-sha1 &&
329 git init --object-format=sha256 repo-sha256 &&
330 git -C repo-sha256 patch-id <diff >patch-id-sha256 &&
331 test_cmp patch-id-sha1 patch-id-sha256
334 test_expect_success
'patch-id without repository' '
336 diff --git a/bar b/bar
337 index bdaf90f..31051f6 100644
344 nongit git patch-id <diff