interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t4052-stat-output.sh

473 lines · 14851 bytes Raw Blame History
1#!/bin/sh
2#
3# Copyright (c) 2012 Zbigniew Jędrzejewski-Szmek
4#
5
6test_description='test --stat output of various commands'
7
8GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11. ./test-lib.sh
12. "$TEST_DIRECTORY"/lib-terminal.sh
13
14# 120-character name
15name=aaaaaaaaaa
16name=$name$name$name$name$name$name$name$name$name$name$name$name
17test_expect_success 'preparation' '
18 >"$name" &&
19 git add "$name" &&
20 git commit -m message &&
21 echo a >"$name" &&
22 git commit -m message "$name"
23'
24
25cat >expect72 <<-'EOF'
26 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
27EOF
28test_expect_success "format-patch: small change with long name gives more space to the name" '
29 git format-patch -1 --stdout >output &&
30 grep " | " output >actual &&
31 test_cmp expect72 actual
32'
33
34while read cmd args
35do
36 cat >expect80 <<-'EOF'
37 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
38 EOF
39 test_expect_success "$cmd: small change with long name gives more space to the name" '
40 git $cmd $args >output &&
41 grep " | " output >actual &&
42 test_cmp expect80 actual
43 '
44done <<\EOF
45diff HEAD^ HEAD --stat
46show --stat
47log -1 --stat
48EOF
49
50cat >expect.60 <<-'EOF'
51 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
52EOF
53cat >expect.6030 <<-'EOF'
54 ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
55EOF
56while read verb expect cmd args
57do
58 # No width limit applied when statNameWidth is ignored
59 case "$expect" in expect72|expect.6030)
60 test_expect_success "$cmd $verb diff.statNameWidth with long name" '
61 git -c diff.statNameWidth=30 $cmd $args >output &&
62 grep " | " output >actual &&
63 test_cmp $expect actual
64 ';;
65 esac
66 # Maximum width limit still applied when statNameWidth is ignored
67 case "$expect" in expect.60|expect.6030)
68 test_expect_success "$cmd --stat=width $verb diff.statNameWidth with long name" '
69 git -c diff.statNameWidth=30 $cmd $args --stat=60 >output &&
70 grep " | " output >actual &&
71 test_cmp $expect actual
72 ';;
73 esac
74done <<\EOF
75ignores expect72 format-patch -1 --stdout
76ignores expect.60 format-patch -1 --stdout
77respects expect.6030 diff HEAD^ HEAD --stat
78respects expect.6030 show --stat
79respects expect.6030 log -1 --stat
80EOF
81
82cat >expect.40 <<-'EOF'
83 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
84EOF
85cat >expect2.40 <<-'EOF'
86 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
87 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
88EOF
89cat >expect2.6030 <<-'EOF'
90 ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
91 ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
92EOF
93while read expect cmd args
94do
95 test_expect_success "$cmd --stat=width: a long name is given more room when the bar is short" '
96 git $cmd $args --stat=40 >output &&
97 grep " | " output >actual &&
98 test_cmp $expect.40 actual
99 '
100
101 test_expect_success "$cmd --stat-width=width with long name" '
102 git $cmd $args --stat-width=40 >output &&
103 grep " | " output >actual &&
104 test_cmp $expect.40 actual
105 '
106
107 test_expect_success "$cmd --stat=width,name-width with long name" '
108 git $cmd $args --stat=60,30 >output &&
109 grep " | " output >actual &&
110 test_cmp $expect.6030 actual
111 '
112
113 test_expect_success "$cmd --stat-name-width=width with long name" '
114 git $cmd $args --stat-name-width=30 >output &&
115 grep " | " output >actual &&
116 test_cmp $expect.6030 actual
117 '
118done <<\EOF
119expect2 format-patch --cover-letter -1 --stdout
120expect diff HEAD^ HEAD --stat
121expect show --stat
122expect log -1 --stat
123EOF
124
125test_expect_success 'preparation for big-change tests' '
126 >abcd &&
127 git add abcd &&
128 git commit -m message &&
129 i=0 &&
130 while test $i -lt 1000
131 do
132 echo $i && i=$(($i + 1)) || return 1
133 done >abcd &&
134 git commit -m message abcd
135'
136
137cat >expect72 <<'EOF'
138 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
139 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
140EOF
141test_expect_success "format-patch --cover-letter ignores COLUMNS with big change" '
142 COLUMNS=200 git format-patch -1 --stdout --cover-letter >output &&
143 grep " | " output >actual &&
144 test_cmp expect72 actual
145'
146
147cat >expect72 <<'EOF'
148 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
149EOF
150cat >expect72-graph <<'EOF'
151| abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
152EOF
153cat >expect200 <<'EOF'
154 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
155EOF
156cat >expect200-graph <<'EOF'
157| abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
158EOF
159while read verb expect cmd args
160do
161 test_expect_success "$cmd $verb COLUMNS with big change" '
162 COLUMNS=200 git $cmd $args >output &&
163 grep " | " output >actual &&
164 test_cmp "$expect" actual
165 '
166
167 case "$cmd" in diff|show) continue;; esac
168
169 test_expect_success "$cmd --graph $verb COLUMNS with big change" '
170 COLUMNS=200 git $cmd $args --graph >output &&
171 grep " | " output >actual &&
172 test_cmp "$expect-graph" actual
173 '
174done <<\EOF
175ignores expect72 format-patch -1 --stdout
176respects expect200 diff HEAD^ HEAD --stat
177respects expect200 show --stat
178respects expect200 log -1 --stat
179EOF
180
181cat >expect40 <<'EOF'
182 abcd | 1000 ++++++++++++++++++++++++++
183EOF
184cat >expect40-graph <<'EOF'
185| abcd | 1000 ++++++++++++++++++++++++
186EOF
187while read verb expect cmd args
188do
189 test_expect_success "$cmd $verb not enough COLUMNS with big change" '
190 COLUMNS=40 git $cmd $args >output &&
191 grep " | " output >actual &&
192 test_cmp "$expect" actual
193 '
194
195 case "$cmd" in diff|show) continue;; esac
196
197 test_expect_success "$cmd --graph $verb not enough COLUMNS with big change" '
198 COLUMNS=40 git $cmd $args --graph >output &&
199 grep " | " output >actual &&
200 test_cmp "$expect-graph" actual
201 '
202done <<\EOF
203ignores expect72 format-patch -1 --stdout
204respects expect40 diff HEAD^ HEAD --stat
205respects expect40 show --stat
206respects expect40 log -1 --stat
207EOF
208
209cat >expect40 <<'EOF'
210 abcd | 1000 ++++++++++++++++++++++++++
211EOF
212cat >expect40-graph <<'EOF'
213| abcd | 1000 ++++++++++++++++++++++++++
214EOF
215while read verb expect cmd args
216do
217 test_expect_success "$cmd $verb diff.statGraphWidth" '
218 git -c diff.statGraphWidth=26 $cmd $args >output &&
219 grep " | " output >actual &&
220 test_cmp "$expect" actual
221 '
222
223 case "$cmd" in diff|show) continue;; esac
224
225 test_expect_success "$cmd --graph $verb diff.statGraphWidth" '
226 git -c diff.statGraphWidth=26 $cmd $args --graph >output &&
227 grep " | " output >actual &&
228 test_cmp "$expect-graph" actual
229 '
230done <<\EOF
231ignores expect72 format-patch -1 --stdout
232respects expect40 diff HEAD^ HEAD --stat
233respects expect40 show --stat
234respects expect40 log -1 --stat
235EOF
236
237cat >expect <<'EOF'
238 abcd | 1000 ++++++++++++++++++++++++++
239EOF
240cat >expect-graph <<'EOF'
241| abcd | 1000 ++++++++++++++++++++++++++
242EOF
243while read cmd args
244do
245 test_expect_success "$cmd --stat=width with big change" '
246 git $cmd $args --stat=40 >output &&
247 grep " | " output >actual &&
248 test_cmp expect actual
249 '
250
251 test_expect_success "$cmd --stat-width=width with big change" '
252 git $cmd $args --stat-width=40 >output &&
253 grep " | " output >actual &&
254 test_cmp expect actual
255 '
256
257 test_expect_success "$cmd --stat-graph-width=width with big change" '
258 git $cmd $args --stat-graph-width=26 >output &&
259 grep " | " output >actual &&
260 test_cmp expect actual
261 '
262
263 case "$cmd" in diff|show) continue;; esac
264
265 test_expect_success "$cmd --stat-width=width --graph with big change" '
266 git $cmd $args --stat-width=40 --graph >output &&
267 grep " | " output >actual &&
268 test_cmp expect-graph actual
269 '
270
271 test_expect_success "$cmd --stat-graph-width=width --graph with big change" '
272 git $cmd $args --stat-graph-width=26 --graph >output &&
273 grep " | " output >actual &&
274 test_cmp expect-graph actual
275 '
276done <<\EOF
277format-patch -1 --stdout
278diff HEAD^ HEAD --stat
279show --stat
280log -1 --stat
281EOF
282
283test_expect_success 'preparation for long-name tests' '
284 cp abcd aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
285 git add aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
286 git commit -m message
287'
288
289cat >expect <<'EOF'
290 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++
291EOF
292cat >expect-graph <<'EOF'
293| ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++
294EOF
295while read cmd args
296do
297 test_expect_success "$cmd --stat=width with big change is more balanced" '
298 git $cmd $args --stat-width=60 >output &&
299 grep " | " output >actual &&
300 test_cmp expect actual
301 '
302
303 case "$cmd" in diff|show) continue;; esac
304
305 test_expect_success "$cmd --stat=width --graph with big change is balanced" '
306 git $cmd $args --stat-width=60 --graph >output &&
307 grep " | " output >actual &&
308 test_cmp expect-graph actual
309 '
310done <<\EOF
311format-patch -1 --stdout
312diff HEAD^ HEAD --stat
313show --stat
314log -1 --stat
315EOF
316
317cat >expect72 <<'EOF'
318 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++
319EOF
320cat >expect72-graph <<'EOF'
321| ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++
322EOF
323cat >expect200 <<'EOF'
324 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
325EOF
326cat >expect200-graph <<'EOF'
327| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
328EOF
329while read verb expect cmd args
330do
331 test_expect_success "$cmd $verb COLUMNS with long name" '
332 COLUMNS=200 git $cmd $args >output &&
333 grep " | " output >actual &&
334 test_cmp "$expect" actual
335 '
336
337 case "$cmd" in diff|show) continue;; esac
338
339 test_expect_success "$cmd --graph $verb COLUMNS with long name" '
340 COLUMNS=200 git $cmd $args --graph >output &&
341 grep " | " output >actual &&
342 test_cmp "$expect-graph" actual
343 '
344done <<\EOF
345ignores expect72 format-patch -1 --stdout
346respects expect200 diff HEAD^ HEAD --stat
347respects expect200 show --stat
348respects expect200 log -1 --stat
349EOF
350
351cat >expect1 <<'EOF'
352 ...aaaaaaa | 1000 ++++++
353EOF
354cat >expect1-graph <<'EOF'
355| ...aaaaaaa | 1000 ++++++
356EOF
357while read verb expect cmd args
358do
359 test_expect_success COLUMNS_CAN_BE_1 \
360 "$cmd $verb prefix greater than COLUMNS with big change" '
361 COLUMNS=1 git $cmd $args >output &&
362 grep " | " output >actual &&
363 test_cmp "$expect" actual
364 '
365
366 case "$cmd" in diff|show) continue;; esac
367
368 test_expect_success COLUMNS_CAN_BE_1 \
369 "$cmd --graph $verb prefix greater than COLUMNS with big change" '
370 COLUMNS=1 git $cmd $args --graph >output &&
371 grep " | " output >actual &&
372 test_cmp "$expect-graph" actual
373 '
374done <<\EOF
375ignores expect72 format-patch -1 --stdout
376respects expect1 diff HEAD^ HEAD --stat
377respects expect1 show --stat
378respects expect1 log -1 --stat
379EOF
380
381cat >expect <<'EOF'
382 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
383EOF
384test_expect_success 'merge --stat respects diff.statGraphWidth with big change' '
385 git checkout -b branch1 HEAD^^ &&
386 git -c diff.statGraphWidth=26 merge --stat --no-ff main^ >output &&
387 grep " | " output >actual &&
388 test_cmp expect40 actual
389'
390test_expect_success 'merge --stat respects COLUMNS with big change' '
391 git checkout -b branch2 HEAD^^ &&
392 COLUMNS=100 git merge --stat --no-ff main^ >output &&
393 grep " | " output >actual &&
394 test_cmp expect actual
395'
396
397cat >expect <<'EOF'
398 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++
399EOF
400cat >expect.30 <<'EOF'
401 ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++++++++++++++++++++++++++++++
402EOF
403test_expect_success 'merge --stat respects diff.statNameWidth with long name' '
404 git switch branch1 &&
405 git -c diff.statNameWidth=30 merge --stat --no-ff main >output &&
406 grep " | " output >actual &&
407 test_cmp expect.30 actual
408'
409test_expect_success 'merge --stat respects COLUMNS with long name' '
410 git switch branch2 &&
411 COLUMNS=100 git merge --stat --no-ff main >output &&
412 grep " | " output >actual &&
413 test_cmp expect actual
414'
415
416# We want git-log to print only 1 commit containing a single branch graph and a
417# diffstat (the diffstat display width, when not manually set through the
418# option "--stat-width", will be automatically calculated).
419# The diffstat will be only one file, with a placeholder FILENAME, that, with
420# enough terminal display width, will contain the following line:
421# "<RED>|<RESET> ${FILENAME} | 0"
422# where "<RED>" and "<RESET>" are ANSI escape codes to color the text.
423# To calculate the minimum terminal display width MIN_TERM_WIDTH so that the
424# FILENAME in the diffstat will not be shortened, we take the FILENAME length
425# and add 9 to it.
426# To check if the diffstat width, when the line_prefix (the "<RED>|<RESET>" of
427# the graph) contains ANSI escape codes (the ANSI escape codes to color the
428# text), is calculated correctly, we:
429# 1. check if it contains the line defined before when using MIN_TERM_WIDTH
430# 2. check if it contains the line defined before, but with the FILENAME
431# shortened by only one character, when using MIN_TERM_WIDTH - 1
432
433test_expect_success 'diffstat where line_prefix contains ANSI escape codes is correct width' '
434 FILENAME="placeholder-text-placeholder-text" &&
435 FILENAME_TRIMMED="...eholder-text-placeholder-text" &&
436 MIN_TERM_WIDTH=$((${#FILENAME} + 9)) &&
437 test_config color.diff always &&
438 git commit --allow-empty --allow-empty-message &&
439 >${FILENAME} &&
440 git add ${FILENAME} &&
441 git commit --allow-empty-message &&
442 COLUMNS=$((MIN_TERM_WIDTH)) git log --graph --stat -n1 | test_decode_color >out &&
443 test_grep "<RED>|<RESET> ${FILENAME} | 0" out &&
444 COLUMNS=$((MIN_TERM_WIDTH - 1)) git log --graph --stat -n1 | test_decode_color >out &&
445 test_grep "<RED>|<RESET> ${FILENAME_TRIMMED} | 0" out
446'
447
448test_expect_success 'diffstat truncation with invalid UTF-8 does not crash' '
449 empty_blob=$(git hash-object -w --stdin </dev/null) &&
450 printf "100644 blob $empty_blob\taaa-\300-aaa\n" |
451 git mktree >tree_file &&
452 tree=$(cat tree_file) &&
453 empty_tree=$(git mktree </dev/null) &&
454 c1=$(git commit-tree -m before $empty_tree) &&
455 c2=$(git commit-tree -m after -p $c1 $tree) &&
456 git -c core.quotepath=false diff --stat --stat-name-width=5 $c1..$c2 >output &&
457 test_grep "| 0" output
458'
459
460test_expect_success FUNNYNAMES 'diffstat truncation with control chars does not read out of bounds' '
461 FNAME=$(printf "aaa-\302\237\302\237\302\237-aaa") &&
462 git commit --allow-empty -m setup &&
463 >$FNAME &&
464 git add -- $FNAME &&
465 git commit -m "add file with control char name" &&
466 git -c core.quotepath=false diff --stat --stat-name-width=5 HEAD~1..HEAD >output &&
467 test_grep "| 0" output &&
468 rm -- $FNAME &&
469 git rm -- $FNAME &&
470 git commit -m "remove test file"
471'
472
473test_done