Raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2012 Zbigniew Jędrzejewski-Szmek
4 #
5
6 test_description='test --stat output of various commands'
7
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11 . ./test-lib.sh
12 . "$TEST_DIRECTORY"/lib-terminal.sh
13
14 # 120-character name
15 name=aaaaaaaaaa
16 name=$name$name$name$name$name$name$name$name$name$name$name$name
17 test_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
25 cat >expect72 <<-'EOF'
26 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
27 EOF
28 test_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
34 while read cmd args
35 do
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 '
44 done <<\EOF
45 diff HEAD^ HEAD --stat
46 show --stat
47 log -1 --stat
48 EOF
49
50 cat >expect.60 <<-'EOF'
51 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
52 EOF
53 cat >expect.6030 <<-'EOF'
54 ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
55 EOF
56 while read verb expect cmd args
57 do
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
74 done <<\EOF
75 ignores expect72 format-patch -1 --stdout
76 ignores expect.60 format-patch -1 --stdout
77 respects expect.6030 diff HEAD^ HEAD --stat
78 respects expect.6030 show --stat
79 respects expect.6030 log -1 --stat
80 EOF
81
82 cat >expect.40 <<-'EOF'
83 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
84 EOF
85 cat >expect2.40 <<-'EOF'
86 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
87 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
88 EOF
89 cat >expect2.6030 <<-'EOF'
90 ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
91 ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
92 EOF
93 while read expect cmd args
94 do
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 '
118 done <<\EOF
119 expect2 format-patch --cover-letter -1 --stdout
120 expect diff HEAD^ HEAD --stat
121 expect show --stat
122 expect log -1 --stat
123 EOF
124
125 test_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
137 cat >expect72 <<'EOF'
138 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
139 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
140 EOF
141 test_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
147 cat >expect72 <<'EOF'
148 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
149 EOF
150 cat >expect72-graph <<'EOF'
151 | abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
152 EOF
153 cat >expect200 <<'EOF'
154 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
155 EOF
156 cat >expect200-graph <<'EOF'
157 | abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
158 EOF
159 while read verb expect cmd args
160 do
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 '
174 done <<\EOF
175 ignores expect72 format-patch -1 --stdout
176 respects expect200 diff HEAD^ HEAD --stat
177 respects expect200 show --stat
178 respects expect200 log -1 --stat
179 EOF
180
181 cat >expect40 <<'EOF'
182 abcd | 1000 ++++++++++++++++++++++++++
183 EOF
184 cat >expect40-graph <<'EOF'
185 | abcd | 1000 ++++++++++++++++++++++++
186 EOF
187 while read verb expect cmd args
188 do
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 '
202 done <<\EOF
203 ignores expect72 format-patch -1 --stdout
204 respects expect40 diff HEAD^ HEAD --stat
205 respects expect40 show --stat
206 respects expect40 log -1 --stat
207 EOF
208
209 cat >expect40 <<'EOF'
210 abcd | 1000 ++++++++++++++++++++++++++
211 EOF
212 cat >expect40-graph <<'EOF'
213 | abcd | 1000 ++++++++++++++++++++++++++
214 EOF
215 while read verb expect cmd args
216 do
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 '
230 done <<\EOF
231 ignores expect72 format-patch -1 --stdout
232 respects expect40 diff HEAD^ HEAD --stat
233 respects expect40 show --stat
234 respects expect40 log -1 --stat
235 EOF
236
237 cat >expect <<'EOF'
238 abcd | 1000 ++++++++++++++++++++++++++
239 EOF
240 cat >expect-graph <<'EOF'
241 | abcd | 1000 ++++++++++++++++++++++++++
242 EOF
243 while read cmd args
244 do
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 '
276 done <<\EOF
277 format-patch -1 --stdout
278 diff HEAD^ HEAD --stat
279 show --stat
280 log -1 --stat
281 EOF
282
283 test_expect_success 'preparation for long-name tests' '
284 cp abcd aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
285 git add aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
286 git commit -m message
287 '
288
289 cat >expect <<'EOF'
290 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++
291 EOF
292 cat >expect-graph <<'EOF'
293 | ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++
294 EOF
295 while read cmd args
296 do
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 '
310 done <<\EOF
311 format-patch -1 --stdout
312 diff HEAD^ HEAD --stat
313 show --stat
314 log -1 --stat
315 EOF
316
317 cat >expect72 <<'EOF'
318 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++
319 EOF
320 cat >expect72-graph <<'EOF'
321 | ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++
322 EOF
323 cat >expect200 <<'EOF'
324 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
325 EOF
326 cat >expect200-graph <<'EOF'
327 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
328 EOF
329 while read verb expect cmd args
330 do
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 '
344 done <<\EOF
345 ignores expect72 format-patch -1 --stdout
346 respects expect200 diff HEAD^ HEAD --stat
347 respects expect200 show --stat
348 respects expect200 log -1 --stat
349 EOF
350
351 cat >expect1 <<'EOF'
352 ...aaaaaaa | 1000 ++++++
353 EOF
354 cat >expect1-graph <<'EOF'
355 | ...aaaaaaa | 1000 ++++++
356 EOF
357 while read verb expect cmd args
358 do
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 '
374 done <<\EOF
375 ignores expect72 format-patch -1 --stdout
376 respects expect1 diff HEAD^ HEAD --stat
377 respects expect1 show --stat
378 respects expect1 log -1 --stat
379 EOF
380
381 cat >expect <<'EOF'
382 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
383 EOF
384 test_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 '
390 test_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
397 cat >expect <<'EOF'
398 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++
399 EOF
400 cat >expect.30 <<'EOF'
401 ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++++++++++++++++++++++++++++++
402 EOF
403 test_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 '
409 test_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
433 test_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
448 test_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
460 test_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
473 test_done