1#!/bin/sh
2#
3# Copyright (c) 2007 Andy Parkins
4#
5
6test_description='for-each-ref test'
7
8. ./test-lib.sh
9
10# Mon Jul 3 15:18:43 2006 +0000
11datestamp=1151939923
12setdate_and_increment () {
13 GIT_COMMITTER_DATE="$datestamp +0200"
14 datestamp=$(expr "$datestamp" + 1)
15 GIT_AUTHOR_DATE="$datestamp +0200"
16 datestamp=$(expr "$datestamp" + 1)
17 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
18}
19
20test_expect_success 'Create sample commit with known timestamp' '
21 setdate_and_increment &&
22 echo "Using $datestamp" > one &&
23 git add one &&
24 git commit -m "Initial" &&
25 setdate_and_increment &&
26 git tag -a -m "Tagging at $datestamp" testtag
27'
28
29test_expect_success 'Check atom names are valid' '
30 bad=
31 for token in \
32 refname objecttype objectsize objectname tree parent \
33 numparent object type author authorname authoremail \
34 authordate committer committername committeremail \
35 committerdate tag tagger taggername taggeremail \
36 taggerdate creator creatordate subject body contents
37 do
38 git for-each-ref --format="$token=%($token)" refs/heads || {
39 bad=$token
40 break
41 }
42 done
43 test -z "$bad"
44'
45
46test_expect_failure 'Check invalid atoms names are errors' '
47 git-for-each-ref --format="%(INVALID)" refs/heads
48'
49
50test_expect_success 'Check format specifiers are ignored in naming date atoms' '
51 git-for-each-ref --format="%(authordate)" refs/heads &&
52 git-for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
53 git-for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
54 git-for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
55'
56
57test_expect_success 'Check valid format specifiers for date fields' '
58 git-for-each-ref --format="%(authordate:default)" refs/heads &&
59 git-for-each-ref --format="%(authordate:relative)" refs/heads &&
60 git-for-each-ref --format="%(authordate:short)" refs/heads &&
61 git-for-each-ref --format="%(authordate:local)" refs/heads &&
62 git-for-each-ref --format="%(authordate:iso8601)" refs/heads &&
63 git-for-each-ref --format="%(authordate:rfc2822)" refs/heads
64'
65
66test_expect_failure 'Check invalid format specifiers are errors' '
67 git-for-each-ref --format="%(authordate:INVALID)" refs/heads
68'
69
70cat >expected <<\EOF
71'refs/heads/master' 'Mon Jul 3 17:18:43 2006 +0200' 'Mon Jul 3 17:18:44 2006 +0200'
72'refs/tags/testtag' 'Mon Jul 3 17:18:45 2006 +0200'
73EOF
74
75test_expect_success 'Check unformatted date fields output' '
76 (git for-each-ref --shell --format="%(refname) %(committerdate) %(authordate)" refs/heads &&
77 git for-each-ref --shell --format="%(refname) %(taggerdate)" refs/tags) >actual &&
78 git diff expected actual
79'
80
81test_expect_success 'Check format "default" formatted date fields output' '
82 f=default &&
83 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
84 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
85 git diff expected actual
86'
87
88# Don't know how to do relative check because I can't know when this script
89# is going to be run and can't fake the current time to git, and hence can't
90# provide expected output. Instead, I'll just make sure that "relative"
91# doesn't exit in error
92#
93#cat >expected <<\EOF
94#
95#EOF
96#
97test_expect_success 'Check format "relative" date fields output' '
98 f=relative &&
99 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
100 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
101'
102
103cat >expected <<\EOF
104'refs/heads/master' '2006-07-03' '2006-07-03'
105'refs/tags/testtag' '2006-07-03'
106EOF
107
108test_expect_success 'Check format "short" date fields output' '
109 f=short &&
110 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
111 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
112 git diff expected actual
113'
114
115cat >expected <<\EOF
116'refs/heads/master' 'Mon Jul 3 15:18:43 2006' 'Mon Jul 3 15:18:44 2006'
117'refs/tags/testtag' 'Mon Jul 3 15:18:45 2006'
118EOF
119
120test_expect_success 'Check format "local" date fields output' '
121 f=local &&
122 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
123 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
124 git diff expected actual
125'
126
127cat >expected <<\EOF
128'refs/heads/master' '2006-07-03 17:18:43 +0200' '2006-07-03 17:18:44 +0200'
129'refs/tags/testtag' '2006-07-03 17:18:45 +0200'
130EOF
131
132test_expect_success 'Check format "iso8601" date fields output' '
133 f=iso8601 &&
134 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
135 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
136 git diff expected actual
137'
138
139cat >expected <<\EOF
140'refs/heads/master' 'Mon, 3 Jul 2006 17:18:43 +0200' 'Mon, 3 Jul 2006 17:18:44 +0200'
141'refs/tags/testtag' 'Mon, 3 Jul 2006 17:18:45 +0200'
142EOF
143
144test_expect_success 'Check format "rfc2822" date fields output' '
145 f=rfc2822 &&
146 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
147 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
148 git diff expected actual
149'
150
151cat >expected <<\EOF
152refs/heads/master
153refs/tags/testtag
154EOF
155
156test_expect_success 'Verify ascending sort' '
157 git-for-each-ref --format="%(refname)" --sort=refname >actual &&
158 git diff expected actual
159'
160
161
162cat >expected <<\EOF
163refs/tags/testtag
164refs/heads/master
165EOF
166
167test_expect_success 'Verify descending sort' '
168 git-for-each-ref --format="%(refname)" --sort=-refname >actual &&
169 git diff expected actual
170'
171
172cat >expected <<\EOF
173'refs/heads/master'
174'refs/tags/testtag'
175EOF
176
177test_expect_success 'Quoting style: shell' '
178 git for-each-ref --shell --format="%(refname)" >actual &&
179 git diff expected actual
180'
181
182test_expect_success 'Quoting style: perl' '
183 git for-each-ref --perl --format="%(refname)" >actual &&
184 git diff expected actual
185'
186
187test_expect_success 'Quoting style: python' '
188 git for-each-ref --python --format="%(refname)" >actual &&
189 git diff expected actual
190'
191
192cat >expected <<\EOF
193"refs/heads/master"
194"refs/tags/testtag"
195EOF
196
197test_expect_success 'Quoting style: tcl' '
198 git for-each-ref --tcl --format="%(refname)" >actual &&
199 git diff expected actual
200'
201
202for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
203 test_expect_success "more than one quoting style: $i" "
204 git for-each-ref $i 2>&1 | (read line &&
205 case \$line in
206 \"error: more than one quoting style\"*) : happy;;
207 *) false
208 esac)
209 "
210done
211
212test_done