t/t0015-hash.sh: remove unnecessary '\' at line end
[git/debian.git] / t / t4204-patch-id.sh
blob80f4a65b285c55cfc7dce38fb115fb0190de97a2
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 as="a a a a a a a a" && # eight a
12 test_write_lines $as >foo &&
13 test_write_lines $as >bar &&
14 git add foo bar &&
15 git commit -a -m initial &&
16 test_write_lines $as b >foo &&
17 test_write_lines $as 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 test_write_lines bar foo >bar-then-foo &&
26 test_write_lines foo bar >foo-then-bar
29 test_expect_success 'patch-id output is well-formed' '
30 git log -p -1 >log.output &&
31 git patch-id <log.output >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 "$@" >patch-id.output &&
40 sed "s/ .*//" patch-id.output >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" >top-diff.output &&
50 calc_patch_id <top-diff.output "$@"
53 test_expect_success 'patch-id detects equality' '
54 get_patch_id main &&
55 get_patch_id same &&
56 test_cmp patch-id_main patch-id_same
59 test_expect_success 'patch-id detects inequality' '
60 get_patch_id main &&
61 get_patch_id notsame &&
62 ! test_cmp patch-id_main patch-id_notsame
65 test_expect_success 'patch-id supports git-format-patch output' '
66 get_patch_id main &&
67 git checkout same &&
68 git format-patch -1 --stdout >format-patch.output &&
69 calc_patch_id same <format-patch.output &&
70 test_cmp patch-id_main patch-id_same &&
71 set $(git patch-id <format-patch.output) &&
72 test "$2" = $(git rev-parse HEAD)
75 test_expect_success 'whitespace is irrelevant in footer' '
76 get_patch_id main &&
77 git checkout same &&
78 git format-patch -1 --stdout >format-patch.output &&
79 sed "s/ \$//" format-patch.output | calc_patch_id same &&
80 test_cmp patch-id_main patch-id_same
83 cmp_patch_id () {
85 test "$1" = "relevant"
86 then
87 ! test_cmp patch-id_"$2" patch-id_"$3"
88 else
89 test_cmp patch-id_"$2" patch-id_"$3"
93 test_patch_id_file_order () {
94 relevant="$1"
95 shift
96 name="order-${1}-$relevant"
97 shift
98 get_top_diff "main" >top-diff.output &&
99 calc_patch_id <top-diff.output "$name" "$@" &&
100 git checkout same &&
101 git format-patch -1 --stdout -O foo-then-bar >format-patch.output &&
102 calc_patch_id <format-patch.output "ordered-$name" "$@" &&
103 cmp_patch_id $relevant "$name" "ordered-$name"
107 # combined test for options: add more tests here to make them
108 # run with all options
109 test_patch_id () {
110 test_patch_id_file_order "$@"
113 # small tests with detailed diagnostic for basic options.
114 test_expect_success 'file order is irrelevant with --stable' '
115 test_patch_id_file_order irrelevant --stable --stable
118 test_expect_success 'file order is relevant with --unstable' '
119 test_patch_id_file_order relevant --unstable --unstable
122 #Now test various option combinations.
123 test_expect_success 'default is unstable' '
124 test_patch_id relevant default
127 test_expect_success 'patchid.stable = true is stable' '
128 test_config patchid.stable true &&
129 test_patch_id irrelevant patchid.stable=true
132 test_expect_success 'patchid.stable = false is unstable' '
133 test_config patchid.stable false &&
134 test_patch_id relevant patchid.stable=false
137 test_expect_success '--unstable overrides patchid.stable = true' '
138 test_config patchid.stable true &&
139 test_patch_id relevant patchid.stable=true--unstable --unstable
142 test_expect_success '--stable overrides patchid.stable = false' '
143 test_config patchid.stable false &&
144 test_patch_id irrelevant patchid.stable=false--stable --stable
147 test_expect_success 'patch-id supports git-format-patch MIME output' '
148 get_patch_id main &&
149 git checkout same &&
150 git format-patch -1 --attach --stdout >format-patch.output &&
151 calc_patch_id <format-patch.output same &&
152 test_cmp patch-id_main patch-id_same
155 test_expect_success 'patch-id respects config from subdir' '
156 test_config patchid.stable true &&
157 mkdir subdir &&
159 # copy these because test_patch_id() looks for them in
160 # the current directory
161 cp bar-then-foo foo-then-bar subdir &&
164 cd subdir &&
165 test_patch_id irrelevant patchid.stable=true
169 cat >nonl <<\EOF
170 diff --git i/a w/a
171 index e69de29..2e65efe 100644
172 --- i/a
173 +++ w/a
174 @@ -0,0 +1 @@
176 \ No newline at end of file
177 diff --git i/b w/b
178 index e69de29..6178079 100644
179 --- i/b
180 +++ w/b
181 @@ -0,0 +1 @@
185 cat >withnl <<\EOF
186 diff --git i/a w/a
187 index e69de29..7898192 100644
188 --- i/a
189 +++ w/a
190 @@ -0,0 +1 @@
192 diff --git i/b w/b
193 index e69de29..6178079 100644
194 --- i/b
195 +++ w/b
196 @@ -0,0 +1 @@
200 test_expect_success 'patch-id handles no-nl-at-eof markers' '
201 cat nonl | calc_patch_id nonl &&
202 cat withnl | calc_patch_id withnl &&
203 test_cmp patch-id_nonl patch-id_withnl
205 test_done