]> git.ipfire.org Git - thirdparty/git.git/blob - t/t4204-patch-id.sh
Merge branch 'es/doc-stdout-vs-stderr'
[thirdparty/git.git] / t / t4204-patch-id.sh
1 #!/bin/sh
2
3 test_description='git patch-id'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
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
28 '
29
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
33 '
34
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"
42 }
43
44 get_top_diff () {
45 git log -p -1 "$@" -O bar-then-foo --
46 }
47
48 get_patch_id () {
49 get_top_diff "$1" | calc_patch_id "$@"
50 }
51
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
56 '
57
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
62 '
63
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)
71 '
72
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
78 '
79
80 cmp_patch_id () {
81 if
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"
87 fi
88 }
89
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"
100
101 }
102
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 "$@"
107 }
108
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
112 '
113
114 test_expect_success 'file order is relevant with --unstable' '
115 test_patch_id_file_order relevant --unstable --unstable
116 '
117
118 #Now test various option combinations.
119 test_expect_success 'default is unstable' '
120 test_patch_id relevant default
121 '
122
123 test_expect_success 'patchid.stable = true is stable' '
124 test_config patchid.stable true &&
125 test_patch_id irrelevant patchid.stable=true
126 '
127
128 test_expect_success 'patchid.stable = false is unstable' '
129 test_config patchid.stable false &&
130 test_patch_id relevant patchid.stable=false
131 '
132
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
136 '
137
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
141 '
142
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
148 '
149
150 test_expect_success 'patch-id respects config from subdir' '
151 test_config patchid.stable true &&
152 mkdir subdir &&
153
154 # copy these because test_patch_id() looks for them in
155 # the current directory
156 cp bar-then-foo foo-then-bar subdir &&
157
158 (
159 cd subdir &&
160 test_patch_id irrelevant patchid.stable=true
161 )
162 '
163
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 @@
170 +a
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 @@
177 +b
178 EOF
179
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 @@
186 +a
187 diff --git i/b w/b
188 index e69de29..6178079 100644
189 --- i/b
190 +++ w/b
191 @@ -0,0 +1 @@
192 +b
193 EOF
194
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
199 '
200 test_done