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