Merge branch 'mm/push-default-warning'
[git] / t / t6302-for-each-ref-filter.sh
1 #!/bin/sh
2
3 test_description='test for-each-refs usage of ref-filter APIs'
4
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-gpg.sh
7
8 if ! test_have_prereq GPG
9 then
10         skip_all="skipping for-each-ref tests, GPG not available"
11         test_done
12 fi
13
14 test_expect_success 'setup some history and refs' '
15         test_commit one &&
16         test_commit two &&
17         test_commit three &&
18         git checkout -b side &&
19         test_commit four &&
20         git tag -s -m "A signed tag message" signed-tag &&
21         git tag -s -m "Annonated doubly" double-tag signed-tag &&
22         git checkout master &&
23         git update-ref refs/odd/spot master
24 '
25
26 test_expect_success 'filtering with --points-at' '
27         cat >expect <<-\EOF &&
28         refs/heads/master
29         refs/odd/spot
30         refs/tags/three
31         EOF
32         git for-each-ref --format="%(refname)" --points-at=master >actual &&
33         test_cmp expect actual
34 '
35
36 test_expect_success 'check signed tags with --points-at' '
37         sed -e "s/Z$//" >expect <<-\EOF &&
38         refs/heads/side Z
39         refs/tags/four Z
40         refs/tags/signed-tag four
41         EOF
42         git for-each-ref --format="%(refname) %(*subject)" --points-at=side >actual &&
43         test_cmp expect actual
44 '
45
46 test_expect_success 'filtering with --merged' '
47         cat >expect <<-\EOF &&
48         refs/heads/master
49         refs/odd/spot
50         refs/tags/one
51         refs/tags/three
52         refs/tags/two
53         EOF
54         git for-each-ref --format="%(refname)" --merged=master >actual &&
55         test_cmp expect actual
56 '
57
58 test_expect_success 'filtering with --no-merged' '
59         cat >expect <<-\EOF &&
60         refs/heads/side
61         refs/tags/double-tag
62         refs/tags/four
63         refs/tags/signed-tag
64         EOF
65         git for-each-ref --format="%(refname)" --no-merged=master >actual &&
66         test_cmp expect actual
67 '
68
69 test_expect_success 'filtering with --contains' '
70         cat >expect <<-\EOF &&
71         refs/heads/master
72         refs/heads/side
73         refs/odd/spot
74         refs/tags/double-tag
75         refs/tags/four
76         refs/tags/signed-tag
77         refs/tags/three
78         refs/tags/two
79         EOF
80         git for-each-ref --format="%(refname)" --contains=two >actual &&
81         test_cmp expect actual
82 '
83
84 test_expect_success '%(color) must fail' '
85         test_must_fail git for-each-ref --format="%(color)%(refname)"
86 '
87
88 test_expect_success 'left alignment is default' '
89         cat >expect <<-\EOF &&
90         refname is refs/heads/master  |refs/heads/master
91         refname is refs/heads/side    |refs/heads/side
92         refname is refs/odd/spot      |refs/odd/spot
93         refname is refs/tags/double-tag|refs/tags/double-tag
94         refname is refs/tags/four     |refs/tags/four
95         refname is refs/tags/one      |refs/tags/one
96         refname is refs/tags/signed-tag|refs/tags/signed-tag
97         refname is refs/tags/three    |refs/tags/three
98         refname is refs/tags/two      |refs/tags/two
99         EOF
100         git for-each-ref --format="%(align:30)refname is %(refname)%(end)|%(refname)" >actual &&
101         test_cmp expect actual
102 '
103
104 test_expect_success 'middle alignment' '
105         cat >expect <<-\EOF &&
106         | refname is refs/heads/master |refs/heads/master
107         |  refname is refs/heads/side  |refs/heads/side
108         |   refname is refs/odd/spot   |refs/odd/spot
109         |refname is refs/tags/double-tag|refs/tags/double-tag
110         |  refname is refs/tags/four   |refs/tags/four
111         |   refname is refs/tags/one   |refs/tags/one
112         |refname is refs/tags/signed-tag|refs/tags/signed-tag
113         |  refname is refs/tags/three  |refs/tags/three
114         |   refname is refs/tags/two   |refs/tags/two
115         EOF
116         git for-each-ref --format="|%(align:middle,30)refname is %(refname)%(end)|%(refname)" >actual &&
117         test_cmp expect actual
118 '
119
120 test_expect_success 'right alignment' '
121         cat >expect <<-\EOF &&
122         |  refname is refs/heads/master|refs/heads/master
123         |    refname is refs/heads/side|refs/heads/side
124         |      refname is refs/odd/spot|refs/odd/spot
125         |refname is refs/tags/double-tag|refs/tags/double-tag
126         |     refname is refs/tags/four|refs/tags/four
127         |      refname is refs/tags/one|refs/tags/one
128         |refname is refs/tags/signed-tag|refs/tags/signed-tag
129         |    refname is refs/tags/three|refs/tags/three
130         |      refname is refs/tags/two|refs/tags/two
131         EOF
132         git for-each-ref --format="|%(align:30,right)refname is %(refname)%(end)|%(refname)" >actual &&
133         test_cmp expect actual
134 '
135
136 cat >expect <<-\EOF
137 |       refname is refs/heads/master       |refs/heads/master
138 |        refname is refs/heads/side        |refs/heads/side
139 |         refname is refs/odd/spot         |refs/odd/spot
140 |     refname is refs/tags/double-tag      |refs/tags/double-tag
141 |        refname is refs/tags/four         |refs/tags/four
142 |         refname is refs/tags/one         |refs/tags/one
143 |     refname is refs/tags/signed-tag      |refs/tags/signed-tag
144 |        refname is refs/tags/three        |refs/tags/three
145 |         refname is refs/tags/two         |refs/tags/two
146 EOF
147
148 test_align_permutations() {
149         while read -r option
150         do
151                 test_expect_success "align:$option" '
152                         git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
153                         test_cmp expect actual
154                 '
155         done
156 }
157
158 test_align_permutations <<-\EOF
159         middle,42
160         42,middle
161         position=middle,42
162         42,position=middle
163         middle,width=42
164         width=42,middle
165         position=middle,width=42
166         width=42,position=middle
167 EOF
168
169 # Last one wins (silently) when multiple arguments of the same type are given
170
171 test_align_permutations <<-\EOF
172         32,width=42,middle
173         width=30,42,middle
174         width=42,position=right,middle
175         42,right,position=middle
176 EOF
177
178 # Individual atoms inside %(align:...) and %(end) must not be quoted.
179
180 test_expect_success 'alignment with format quote' "
181         cat >expect <<-\EOF &&
182         |'      '\''master| A U Thor'\''      '|
183         |'       '\''side| A U Thor'\''       '|
184         |'     '\''odd/spot| A U Thor'\''     '|
185         |'        '\''double-tag| '\''        '|
186         |'       '\''four| A U Thor'\''       '|
187         |'       '\''one| A U Thor'\''        '|
188         |'        '\''signed-tag| '\''        '|
189         |'      '\''three| A U Thor'\''       '|
190         |'       '\''two| A U Thor'\''        '|
191         EOF
192         git for-each-ref --shell --format=\"|%(align:30,middle)'%(refname:short)| %(authorname)'%(end)|\" >actual &&
193         test_cmp expect actual
194 "
195
196 test_expect_success 'nested alignment with quote formatting' "
197         cat >expect <<-\EOF &&
198         |'         master               '|
199         |'           side               '|
200         |'       odd/spot               '|
201         |'     double-tag               '|
202         |'           four               '|
203         |'            one               '|
204         |'     signed-tag               '|
205         |'          three               '|
206         |'            two               '|
207         EOF
208         git for-each-ref --shell --format='|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|' >actual &&
209         test_cmp expect actual
210 "
211
212 test_expect_success 'check `%(contents:lines=1)`' '
213         cat >expect <<-\EOF &&
214         master |three
215         side |four
216         odd/spot |three
217         double-tag |Annonated doubly
218         four |four
219         one |one
220         signed-tag |A signed tag message
221         three |three
222         two |two
223         EOF
224         git for-each-ref --format="%(refname:short) |%(contents:lines=1)" >actual &&
225         test_cmp expect actual
226 '
227
228 test_expect_success 'check `%(contents:lines=0)`' '
229         cat >expect <<-\EOF &&
230         master |
231         side |
232         odd/spot |
233         double-tag |
234         four |
235         one |
236         signed-tag |
237         three |
238         two |
239         EOF
240         git for-each-ref --format="%(refname:short) |%(contents:lines=0)" >actual &&
241         test_cmp expect actual
242 '
243
244 test_expect_success 'check `%(contents:lines=99999)`' '
245         cat >expect <<-\EOF &&
246         master |three
247         side |four
248         odd/spot |three
249         double-tag |Annonated doubly
250         four |four
251         one |one
252         signed-tag |A signed tag message
253         three |three
254         two |two
255         EOF
256         git for-each-ref --format="%(refname:short) |%(contents:lines=99999)" >actual &&
257         test_cmp expect actual
258 '
259
260 test_expect_success '`%(contents:lines=-1)` should fail' '
261         test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
262 '
263
264 test_expect_success 'setup for version sort' '
265         test_commit foo1.3 &&
266         test_commit foo1.6 &&
267         test_commit foo1.10
268 '
269
270 test_expect_success 'version sort' '
271         git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
272         cat >expect <<-\EOF &&
273         foo1.3
274         foo1.6
275         foo1.10
276         EOF
277         test_cmp expect actual
278 '
279
280 test_expect_success 'version sort (shortened)' '
281         git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
282         cat >expect <<-\EOF &&
283         foo1.3
284         foo1.6
285         foo1.10
286         EOF
287         test_cmp expect actual
288 '
289
290 test_expect_success 'reverse version sort' '
291         git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
292         cat >expect <<-\EOF &&
293         foo1.10
294         foo1.6
295         foo1.3
296         EOF
297         test_cmp expect actual
298 '
299
300 test_done