Merge branch 'tb/pack-revindex-on-disk-cleanup'
[git/debian.git] / t / t4204-patch-id.sh
blobe78d8097f39690ee094f601344d104a6e3a4ce09
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 as="a a a a a a a a" && # eight a
13 test_write_lines $as >foo &&
14 test_write_lines $as >bar &&
15 git add foo bar &&
16 git commit -a -m initial &&
17 test_write_lines $as b >foo &&
18 test_write_lines $as 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 test_write_lines bar foo >bar-then-foo &&
27 test_write_lines foo bar >foo-then-bar
30 test_expect_success 'patch-id output is well-formed' '
31 git log -p -1 | git patch-id >output &&
32 grep "^$OID_REGEX $(git rev-parse HEAD)$" output
35 #calculate patch id. Make sure output is not empty.
36 calc_patch_id () {
37 patch_name="$1"
38 shift
39 git patch-id "$@" |
40 sed "s/ .*//" >patch-id_"$patch_name" &&
41 test_line_count -gt 0 patch-id_"$patch_name"
44 get_top_diff () {
45 git log -p -1 "$@" -O bar-then-foo --
48 get_patch_id () {
49 get_top_diff "$1" | calc_patch_id "$@"
52 test_expect_success 'patch-id detects equality' '
53 get_patch_id main &&
54 get_patch_id same &&
55 test_cmp patch-id_main patch-id_same
58 test_expect_success 'patch-id detects inequality' '
59 get_patch_id main &&
60 get_patch_id notsame &&
61 ! test_cmp patch-id_main patch-id_notsame
64 test_expect_success 'patch-id supports git-format-patch output' '
65 get_patch_id main &&
66 git checkout same &&
67 git format-patch -1 --stdout | calc_patch_id same &&
68 test_cmp patch-id_main patch-id_same &&
69 set $(git format-patch -1 --stdout | git patch-id) &&
70 test "$2" = $(git rev-parse HEAD)
73 test_expect_success 'whitespace is irrelevant in footer' '
74 get_patch_id main &&
75 git checkout same &&
76 git format-patch -1 --stdout | sed "s/ \$//" | calc_patch_id same &&
77 test_cmp patch-id_main patch-id_same
80 cmp_patch_id () {
82 test "$1" = "relevant"
83 then
84 ! test_cmp patch-id_"$2" patch-id_"$3"
85 else
86 test_cmp patch-id_"$2" patch-id_"$3"
90 test_patch_id_file_order () {
91 relevant="$1"
92 shift
93 name="order-${1}-$relevant"
94 shift
95 get_top_diff "main" | calc_patch_id "$name" "$@" &&
96 git checkout same &&
97 git format-patch -1 --stdout -O foo-then-bar |
98 calc_patch_id "ordered-$name" "$@" &&
99 cmp_patch_id $relevant "$name" "ordered-$name"
103 # combined test for options: add more tests here to make them
104 # run with all options
105 test_patch_id () {
106 test_patch_id_file_order "$@"
109 # small tests with detailed diagnostic for basic options.
110 test_expect_success 'file order is irrelevant with --stable' '
111 test_patch_id_file_order irrelevant --stable --stable
114 test_expect_success 'file order is relevant with --unstable' '
115 test_patch_id_file_order relevant --unstable --unstable
118 #Now test various option combinations.
119 test_expect_success 'default is unstable' '
120 test_patch_id relevant default
123 test_expect_success 'patchid.stable = true is stable' '
124 test_config patchid.stable true &&
125 test_patch_id irrelevant patchid.stable=true
128 test_expect_success 'patchid.stable = false is unstable' '
129 test_config patchid.stable false &&
130 test_patch_id relevant patchid.stable=false
133 test_expect_success '--unstable overrides patchid.stable = true' '
134 test_config patchid.stable true &&
135 test_patch_id relevant patchid.stable=true--unstable --unstable
138 test_expect_success '--stable overrides patchid.stable = false' '
139 test_config patchid.stable false &&
140 test_patch_id irrelevant patchid.stable=false--stable --stable
143 test_expect_success 'patch-id supports git-format-patch MIME output' '
144 get_patch_id main &&
145 git checkout same &&
146 git format-patch -1 --attach --stdout | calc_patch_id same &&
147 test_cmp patch-id_main patch-id_same
150 test_expect_success 'patch-id respects config from subdir' '
151 test_config patchid.stable true &&
152 mkdir subdir &&
154 # copy these because test_patch_id() looks for them in
155 # the current directory
156 cp bar-then-foo foo-then-bar subdir &&
159 cd subdir &&
160 test_patch_id irrelevant patchid.stable=true
164 cat >nonl <<\EOF
165 diff --git i/a w/a
166 index e69de29..2e65efe 100644
167 --- i/a
168 +++ w/a
169 @@ -0,0 +1 @@
171 \ No newline at end of file
172 diff --git i/b w/b
173 index e69de29..6178079 100644
174 --- i/b
175 +++ w/b
176 @@ -0,0 +1 @@
180 cat >withnl <<\EOF
181 diff --git i/a w/a
182 index e69de29..7898192 100644
183 --- i/a
184 +++ w/a
185 @@ -0,0 +1 @@
187 diff --git i/b w/b
188 index e69de29..6178079 100644
189 --- i/b
190 +++ w/b
191 @@ -0,0 +1 @@
195 test_expect_success 'patch-id handles no-nl-at-eof markers' '
196 cat nonl | calc_patch_id nonl &&
197 cat withnl | calc_patch_id withnl &&
198 test_cmp patch-id_nonl patch-id_withnl
200 test_done