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 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
52EOF
53cat >expect.6030 <<-'EOF'
54 ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
55EOF
56cat >expect2.60 <<-'EOF'
57 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
58 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
59EOF
60cat >expect2.6030 <<-'EOF'
61 ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
62 ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
63EOF
64while read expect cmd args
65do
66	test_expect_success "$cmd --stat=width: a long name is given more room when the bar is short" '
67		git $cmd $args --stat=40 >output &&
68		grep " | " output >actual &&
69		test_cmp $expect.60 actual
70	'
71
72	test_expect_success "$cmd --stat-width=width with long name" '
73		git $cmd $args --stat-width=40 >output &&
74		grep " | " output >actual &&
75		test_cmp $expect.60 actual
76	'
77
78	test_expect_success "$cmd --stat=...,name-width with long name" '
79		git $cmd $args --stat=60,30 >output &&
80		grep " | " output >actual &&
81		test_cmp $expect.6030 actual
82	'
83
84	test_expect_success "$cmd --stat-name-width with long name" '
85		git $cmd $args --stat-name-width=30 >output &&
86		grep " | " output >actual &&
87		test_cmp $expect.6030 actual
88	'
89done <<\EOF
90expect2 format-patch --cover-letter -1 --stdout
91expect diff HEAD^ HEAD --stat
92expect show --stat
93expect log -1 --stat
94EOF
95
96
97test_expect_success 'preparation for big change tests' '
98	>abcd &&
99	git add abcd &&
100	git commit -m message &&
101	i=0 &&
102	while test $i -lt 1000
103	do
104		echo $i && i=$(($i + 1))
105	done >abcd &&
106	git commit -m message abcd
107'
108
109cat >expect72 <<'EOF'
110 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
111 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
112EOF
113test_expect_success "format-patch --cover-letter ignores COLUMNS (big change)" '
114	COLUMNS=200 git format-patch -1 --stdout --cover-letter >output &&
115	grep " | " output >actual &&
116	test_cmp expect72 actual
117'
118
119cat >expect72 <<'EOF'
120 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
121EOF
122cat >expect72-graph <<'EOF'
123|  abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
124EOF
125cat >expect200 <<'EOF'
126 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
127EOF
128cat >expect200-graph <<'EOF'
129|  abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
130EOF
131while read verb expect cmd args
132do
133	test_expect_success "$cmd $verb COLUMNS (big change)" '
134		COLUMNS=200 git $cmd $args >output &&
135		grep " | " output >actual &&
136		test_cmp "$expect" actual
137	'
138
139	case "$cmd" in diff|show) continue;; esac
140
141	test_expect_success "$cmd --graph $verb COLUMNS (big change)" '
142		COLUMNS=200 git $cmd $args --graph >output &&
143		grep " | " output >actual &&
144		test_cmp "$expect-graph" actual
145	'
146done <<\EOF
147ignores expect72 format-patch -1 --stdout
148respects expect200 diff HEAD^ HEAD --stat
149respects expect200 show --stat
150respects expect200 log -1 --stat
151EOF
152
153cat >expect40 <<'EOF'
154 abcd | 1000 ++++++++++++++++++++++++++
155EOF
156cat >expect40-graph <<'EOF'
157|  abcd | 1000 ++++++++++++++++++++++++
158EOF
159while read verb expect cmd args
160do
161	test_expect_success "$cmd $verb not enough COLUMNS (big change)" '
162		COLUMNS=40 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 not enough COLUMNS (big change)" '
170		COLUMNS=40 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 expect40 diff HEAD^ HEAD --stat
177respects expect40 show --stat
178respects expect40 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 statGraphWidth config" '
190		git -c diff.statGraphWidth=26 $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 statGraphWidth config" '
198		git -c diff.statGraphWidth=26 $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
209
210cat >expect <<'EOF'
211 abcd | 1000 ++++++++++++++++++++++++++
212EOF
213cat >expect-graph <<'EOF'
214|  abcd | 1000 ++++++++++++++++++++++++++
215EOF
216while read cmd args
217do
218	test_expect_success "$cmd --stat=width with big change" '
219		git $cmd $args --stat=40 >output &&
220		grep " | " output >actual &&
221		test_cmp expect actual
222	'
223
224	test_expect_success "$cmd --stat-width=width with big change" '
225		git $cmd $args --stat-width=40 >output &&
226		grep " | " output >actual &&
227		test_cmp expect actual
228	'
229
230	test_expect_success "$cmd --stat-graph-width with big change" '
231		git $cmd $args --stat-graph-width=26 >output &&
232		grep " | " output >actual &&
233		test_cmp expect actual
234	'
235
236	case "$cmd" in diff|show) continue;; esac
237
238	test_expect_success "$cmd --stat-width=width --graph with big change" '
239		git $cmd $args --stat-width=40 --graph >output &&
240		grep " | " output >actual &&
241		test_cmp expect-graph actual
242	'
243
244	test_expect_success "$cmd --stat-graph-width --graph with big change" '
245		git $cmd $args --stat-graph-width=26 --graph >output &&
246		grep " | " output >actual &&
247		test_cmp expect-graph actual
248	'
249done <<\EOF
250format-patch -1 --stdout
251diff HEAD^ HEAD --stat
252show --stat
253log -1 --stat
254EOF
255
256test_expect_success 'preparation for long filename tests' '
257	cp abcd aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
258	git add aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
259	git commit -m message
260'
261
262cat >expect <<'EOF'
263 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++
264EOF
265cat >expect-graph <<'EOF'
266|  ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++
267EOF
268while read cmd args
269do
270	test_expect_success "$cmd --stat=width with big change is more balanced" '
271		git $cmd $args --stat-width=60 >output &&
272		grep " | " output >actual &&
273		test_cmp expect actual
274	'
275
276	case "$cmd" in diff|show) continue;; esac
277
278	test_expect_success "$cmd --stat=width --graph with big change is balanced" '
279		git $cmd $args --stat-width=60 --graph >output &&
280		grep " | " output >actual &&
281		test_cmp expect-graph actual
282	'
283done <<\EOF
284format-patch -1 --stdout
285diff HEAD^ HEAD --stat
286show --stat
287log -1 --stat
288EOF
289
290cat >expect72 <<'EOF'
291 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++
292EOF
293cat >expect72-graph <<'EOF'
294|  ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++
295EOF
296cat >expect200 <<'EOF'
297 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
298EOF
299cat >expect200-graph <<'EOF'
300|  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
301EOF
302while read verb expect cmd args
303do
304	test_expect_success "$cmd $verb COLUMNS (long filename)" '
305		COLUMNS=200 git $cmd $args >output &&
306		grep " | " output >actual &&
307		test_cmp "$expect" actual
308	'
309
310	case "$cmd" in diff|show) continue;; esac
311
312	test_expect_success "$cmd --graph $verb COLUMNS (long filename)" '
313		COLUMNS=200 git $cmd $args --graph >output &&
314		grep " | " output >actual &&
315		test_cmp "$expect-graph" actual
316	'
317done <<\EOF
318ignores expect72 format-patch -1 --stdout
319respects expect200 diff HEAD^ HEAD --stat
320respects expect200 show --stat
321respects expect200 log -1 --stat
322EOF
323
324cat >expect1 <<'EOF'
325 ...aaaaaaa | 1000 ++++++
326EOF
327cat >expect1-graph <<'EOF'
328|  ...aaaaaaa | 1000 ++++++
329EOF
330while read verb expect cmd args
331do
332	test_expect_success COLUMNS_CAN_BE_1 \
333		"$cmd $verb prefix greater than COLUMNS (big change)" '
334		COLUMNS=1 git $cmd $args >output &&
335		grep " | " output >actual &&
336		test_cmp "$expect" actual
337	'
338
339	case "$cmd" in diff|show) continue;; esac
340
341	test_expect_success COLUMNS_CAN_BE_1 \
342		"$cmd --graph $verb prefix greater than COLUMNS (big change)" '
343		COLUMNS=1 git $cmd $args --graph >output &&
344		grep " | " output >actual &&
345		test_cmp "$expect-graph" actual
346	'
347done <<\EOF
348ignores expect72 format-patch -1 --stdout
349respects expect1 diff HEAD^ HEAD --stat
350respects expect1 show --stat
351respects expect1 log -1 --stat
352EOF
353
354cat >expect <<'EOF'
355 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
356EOF
357test_expect_success 'merge --stat respects COLUMNS (big change)' '
358	git checkout -b branch HEAD^^ &&
359	COLUMNS=100 git merge --stat --no-ff main^ >output &&
360	grep " | " output >actual &&
361	test_cmp expect actual
362'
363
364cat >expect <<'EOF'
365 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++
366EOF
367test_expect_success 'merge --stat respects COLUMNS (long filename)' '
368	COLUMNS=100 git merge --stat --no-ff main >output &&
369	grep " | " output >actual &&
370	test_cmp expect actual
371'
372
373test_done
374