Makefile: make script-related rules usable from subdirectories
[git.git] / t / t6006-rev-list-format.sh
blob3fc3b74c8efa4e5092e36e6a6fbd2a15bc862d77
1 #!/bin/sh
3 test_description='git rev-list --pretty=format test'
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-terminal.sh
8 test_tick
9 test_expect_success 'setup' '
10 touch foo && git add foo && git commit -m "added foo" &&
11 echo changed >foo && git commit -a -m "changed foo"
14 # usage: test_format name format_string <expected_output
15 test_format () {
16 cat >expect.$1
17 test_expect_success "format $1" "
18 git rev-list --pretty=format:'$2' master >output.$1 &&
19 test_cmp expect.$1 output.$1
23 # Feed to --format to provide predictable colored sequences.
24 AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
25 has_color () {
26 printf '\033[31mfoo\033[m\n' >expect &&
27 test_cmp expect "$1"
30 has_no_color () {
31 echo foo >expect &&
32 test_cmp expect "$1"
35 test_format percent %%h <<'EOF'
36 commit 131a310eb913d107dd3c09a65d1651175898735d
38 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
40 EOF
42 test_format hash %H%n%h <<'EOF'
43 commit 131a310eb913d107dd3c09a65d1651175898735d
44 131a310eb913d107dd3c09a65d1651175898735d
45 131a310
46 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
47 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
48 86c75cf
49 EOF
51 test_format tree %T%n%t <<'EOF'
52 commit 131a310eb913d107dd3c09a65d1651175898735d
53 fe722612f26da5064c32ca3843aa154bdb0b08a0
54 fe72261
55 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
56 4d5fcadc293a348e88f777dc0920f11e7d71441c
57 4d5fcad
58 EOF
60 test_format parents %P%n%p <<'EOF'
61 commit 131a310eb913d107dd3c09a65d1651175898735d
62 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
63 86c75cf
64 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
67 EOF
69 # we don't test relative here
70 test_format author %an%n%ae%n%ad%n%aD%n%at <<'EOF'
71 commit 131a310eb913d107dd3c09a65d1651175898735d
72 A U Thor
73 author@example.com
74 Thu Apr 7 15:13:13 2005 -0700
75 Thu, 7 Apr 2005 15:13:13 -0700
76 1112911993
77 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
78 A U Thor
79 author@example.com
80 Thu Apr 7 15:13:13 2005 -0700
81 Thu, 7 Apr 2005 15:13:13 -0700
82 1112911993
83 EOF
85 test_format committer %cn%n%ce%n%cd%n%cD%n%ct <<'EOF'
86 commit 131a310eb913d107dd3c09a65d1651175898735d
87 C O Mitter
88 committer@example.com
89 Thu Apr 7 15:13:13 2005 -0700
90 Thu, 7 Apr 2005 15:13:13 -0700
91 1112911993
92 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
93 C O Mitter
94 committer@example.com
95 Thu Apr 7 15:13:13 2005 -0700
96 Thu, 7 Apr 2005 15:13:13 -0700
97 1112911993
98 EOF
100 test_format encoding %e <<'EOF'
101 commit 131a310eb913d107dd3c09a65d1651175898735d
102 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
105 test_format subject %s <<'EOF'
106 commit 131a310eb913d107dd3c09a65d1651175898735d
107 changed foo
108 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
109 added foo
112 test_format body %b <<'EOF'
113 commit 131a310eb913d107dd3c09a65d1651175898735d
114 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
117 test_format raw-body %B <<'EOF'
118 commit 131a310eb913d107dd3c09a65d1651175898735d
119 changed foo
121 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
122 added foo
126 test_format colors %Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy <<'EOF'
127 commit 131a310eb913d107dd3c09a65d1651175898735d
128 \e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
129 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
130 \e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
133 test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<'EOF'
134 commit 131a310eb913d107dd3c09a65d1651175898735d
135 \e[1;31;43mfoo\e[m
136 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
137 \e[1;31;43mfoo\e[m
140 test_expect_success '%C(auto) does not enable color by default' '
141 git log --format=$AUTO_COLOR -1 >actual &&
142 has_no_color actual
145 test_expect_success '%C(auto) enables colors for color.diff' '
146 git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
147 has_color actual
150 test_expect_success '%C(auto) enables colors for color.ui' '
151 git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
152 has_color actual
155 test_expect_success '%C(auto) respects --color' '
156 git log --format=$AUTO_COLOR -1 --color >actual &&
157 has_color actual
160 test_expect_success '%C(auto) respects --no-color' '
161 git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
162 has_no_color actual
165 test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
167 TERM=vt100 && export TERM &&
168 test_terminal \
169 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
170 has_color actual
174 test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
176 TERM=vt100 && export TERM &&
177 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
178 has_no_color actual
182 cat >commit-msg <<'EOF'
183 Test printing of complex bodies
185 This commit message is much longer than the others,
186 and it will be encoded in iso8859-1. We should therefore
187 include an iso8859 character: ¡bueno!
189 test_expect_success 'setup complex body' '
190 git config i18n.commitencoding iso8859-1 &&
191 echo change2 >foo && git commit -a -F commit-msg
194 test_format complex-encoding %e <<'EOF'
195 commit f58db70b055c5718631e5c61528b28b12090cdea
196 iso8859-1
197 commit 131a310eb913d107dd3c09a65d1651175898735d
198 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
201 test_format complex-subject %s <<'EOF'
202 commit f58db70b055c5718631e5c61528b28b12090cdea
203 Test printing of complex bodies
204 commit 131a310eb913d107dd3c09a65d1651175898735d
205 changed foo
206 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
207 added foo
210 test_format complex-body %b <<'EOF'
211 commit f58db70b055c5718631e5c61528b28b12090cdea
212 This commit message is much longer than the others,
213 and it will be encoded in iso8859-1. We should therefore
214 include an iso8859 character: ¡bueno!
216 commit 131a310eb913d107dd3c09a65d1651175898735d
217 commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
220 test_expect_success '%x00 shows NUL' '
221 echo >expect commit f58db70b055c5718631e5c61528b28b12090cdea &&
222 echo >>expect fooQbar &&
223 git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
224 nul_to_q <actual.nul >actual &&
225 test_cmp expect actual
228 test_expect_success '%ad respects --date=' '
229 echo 2005-04-07 >expect.ad-short &&
230 git log -1 --date=short --pretty=tformat:%ad >output.ad-short master &&
231 test_cmp expect.ad-short output.ad-short
234 test_expect_success 'empty email' '
235 test_tick &&
236 C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
237 A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
238 test "$A" = "A U Thor,,Thu Apr 7 15:14:13 2005 -0700" || {
239 echo "Eh? $A" >failure
240 false
244 test_expect_success 'del LF before empty (1)' '
245 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
246 test_line_count = 2 actual
249 test_expect_success 'del LF before empty (2)' '
250 git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
251 test_line_count = 6 actual &&
252 grep "^$" actual
255 test_expect_success 'add LF before non-empty (1)' '
256 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
257 test_line_count = 2 actual
260 test_expect_success 'add LF before non-empty (2)' '
261 git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
262 test_line_count = 6 actual &&
263 grep "^$" actual
266 test_expect_success 'add SP before non-empty (1)' '
267 git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
268 test $(wc -w <actual) = 2
271 test_expect_success 'add SP before non-empty (2)' '
272 git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
273 test $(wc -w <actual) = 4
276 test_expect_success '--abbrev' '
277 echo SHORT SHORT SHORT >expect2 &&
278 echo LONG LONG LONG >expect3 &&
279 git log -1 --format="%h %h %h" HEAD >actual1 &&
280 git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
281 git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
282 sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
283 sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
284 test_cmp expect2 fuzzy2 &&
285 test_cmp expect3 fuzzy3 &&
286 ! test_cmp actual1 actual2
289 test_expect_success '%H is not affected by --abbrev-commit' '
290 git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
291 len=$(wc -c <actual) &&
292 test $len = 41
295 test_expect_success '%h is not affected by --abbrev-commit' '
296 git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
297 len=$(wc -c <actual) &&
298 test $len = 21
301 test_expect_success '"%h %gD: %gs" is same as git-reflog' '
302 git reflog >expect &&
303 git log -g --format="%h %gD: %gs" >actual &&
304 test_cmp expect actual
307 test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
308 git reflog --date=raw >expect &&
309 git log -g --format="%h %gD: %gs" --date=raw >actual &&
310 test_cmp expect actual
313 test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
314 git reflog --abbrev=13 --date=raw >expect &&
315 git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
316 test_cmp expect actual
319 test_expect_success '%gd shortens ref name' '
320 echo "master@{0}" >expect.gd-short &&
321 git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&
322 test_cmp expect.gd-short actual.gd-short
325 test_expect_success 'reflog identity' '
326 echo "C O Mitter:committer@example.com" >expect &&
327 git log -g -1 --format="%gn:%ge" >actual &&
328 test_cmp expect actual
331 test_expect_success 'oneline with empty message' '
332 git commit -m "dummy" --allow-empty &&
333 git commit -m "dummy" --allow-empty &&
334 git filter-branch --msg-filter "sed -e s/dummy//" HEAD^^.. &&
335 git rev-list --oneline HEAD >test.txt &&
336 test_line_count = 5 test.txt &&
337 git rev-list --oneline --graph HEAD >testg.txt &&
338 test_line_count = 5 testg.txt
341 test_expect_success 'single-character name is parsed correctly' '
342 git commit --author="a <a@example.com>" --allow-empty -m foo &&
343 echo "a <a@example.com>" >expect &&
344 git log -1 --format="%an <%ae>" >actual &&
345 test_cmp expect actual
348 test_done