1#!/bin/sh
2
3test_description='Test diff indent heuristic.
4
5'
6. ./test-lib.sh
7. "$TEST_DIRECTORY"/diff-lib.sh
8
9# Compare two diff outputs. Ignore "index" lines, because we don't
10# care about SHA-1s or file modes.
11compare_diff () {
12 sed -e "/^index /d" <"$1" >.tmp-1
13 sed -e "/^index /d" <"$2" >.tmp-2
14 test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
15}
16
17# Compare blame output using the expectation for a diff as reference.
18# Only look for the lines coming from non-boundary commits.
19compare_blame () {
20 sed -n -e "1,4d" -e "s/^\+//p" <"$1" >.tmp-1
21 sed -ne "s/^[^^][^)]*) *//p" <"$2" >.tmp-2
22 test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
23}
24
25test_expect_success 'prepare' '
26 cat <<-\EOF >spaces.txt &&
27 1
28 2
29 a
30
31 b
32 3
33 4
34 EOF
35
36 cat <<-\EOF >functions.c &&
37 1
38 2
39 /* function */
40 foo() {
41 foo
42 }
43
44 3
45 4
46 EOF
47
48 git add spaces.txt functions.c &&
49 test_tick &&
50 git commit -m initial &&
51 git branch old &&
52
53 cat <<-\EOF >spaces.txt &&
54 1
55 2
56 a
57
58 b
59 a
60
61 b
62 3
63 4
64 EOF
65
66 cat <<-\EOF >functions.c &&
67 1
68 2
69 /* function */
70 bar() {
71 foo
72 }
73
74 /* function */
75 foo() {
76 foo
77 }
78
79 3
80 4
81 EOF
82
83 git add spaces.txt functions.c &&
84 test_tick &&
85 git commit -m initial &&
86 git branch new &&
87
88 tr "_" " " <<-\EOF >spaces-expect &&
89 diff --git a/spaces.txt b/spaces.txt
90 --- a/spaces.txt
91 +++ b/spaces.txt
92 @@ -3,5 +3,8 @@
93 a
94 _
95 b
96 +a
97 +
98 +b
99 3
100 4
101 EOF
102
103 tr "_" " " <<-\EOF >spaces-compacted-expect &&
104 diff --git a/spaces.txt b/spaces.txt
105 --- a/spaces.txt
106 +++ b/spaces.txt
107 @@ -2,6 +2,9 @@
108 2
109 a
110 _
111 +b
112 +a
113 +
114 b
115 3
116 4
117 EOF
118
119 tr "_" " " <<-\EOF >functions-expect &&
120 diff --git a/functions.c b/functions.c
121 --- a/functions.c
122 +++ b/functions.c
123 @@ -1,6 +1,11 @@
124 1
125 2
126 /* function */
127 +bar() {
128 + foo
129 +}
130 +
131 +/* function */
132 foo() {
133 foo
134 }
135 EOF
136
137 tr "_" " " <<-\EOF >functions-compacted-expect
138 diff --git a/functions.c b/functions.c
139 --- a/functions.c
140 +++ b/functions.c
141 @@ -1,5 +1,10 @@
142 1
143 2
144 +/* function */
145 +bar() {
146 + foo
147 +}
148 +
149 /* function */
150 foo() {
151 foo
152 EOF
153'
154
155test_expect_success 'diff: ugly spaces' '
156 git diff old new -- spaces.txt >out &&
157 compare_diff spaces-expect out
158'
159
160test_expect_success 'diff: nice spaces with --indent-heuristic' '
161 git diff --indent-heuristic old new -- spaces.txt >out-compacted &&
162 compare_diff spaces-compacted-expect out-compacted
163'
164
165test_expect_success 'diff: nice spaces with diff.indentHeuristic' '
166 git -c diff.indentHeuristic=true diff old new -- spaces.txt >out-compacted2 &&
167 compare_diff spaces-compacted-expect out-compacted2
168'
169
170test_expect_success 'diff: --no-indent-heuristic overrides config' '
171 git -c diff.indentHeuristic=true diff --no-indent-heuristic old new -- spaces.txt >out2 &&
172 compare_diff spaces-expect out2
173'
174
175test_expect_success 'diff: --indent-heuristic with --patience' '
176 git diff --indent-heuristic --patience old new -- spaces.txt >out-compacted3 &&
177 compare_diff spaces-compacted-expect out-compacted3
178'
179
180test_expect_success 'diff: --indent-heuristic with --histogram' '
181 git diff --indent-heuristic --histogram old new -- spaces.txt >out-compacted4 &&
182 compare_diff spaces-compacted-expect out-compacted4
183'
184
185test_expect_success 'diff: ugly functions' '
186 git diff old new -- functions.c >out &&
187 compare_diff functions-expect out
188'
189
190test_expect_success 'diff: nice functions with --indent-heuristic' '
191 git diff --indent-heuristic old new -- functions.c >out-compacted &&
192 compare_diff functions-compacted-expect out-compacted
193'
194
195test_expect_success 'blame: ugly spaces' '
196 git blame old..new -- spaces.txt >out-blame &&
197 compare_blame spaces-expect out-blame
198'
199
200test_expect_success 'blame: nice spaces with --indent-heuristic' '
201 git blame --indent-heuristic old..new -- spaces.txt >out-blame-compacted &&
202 compare_blame spaces-compacted-expect out-blame-compacted
203'
204
205test_expect_success 'blame: nice spaces with diff.indentHeuristic' '
206 git -c diff.indentHeuristic=true blame old..new -- spaces.txt >out-blame-compacted2 &&
207 compare_blame spaces-compacted-expect out-blame-compacted2
208'
209
210test_expect_success 'blame: --no-indent-heuristic overrides config' '
211 git -c diff.indentHeuristic=true blame --no-indent-heuristic old..new -- spaces.txt >out-blame2 &&
212 git blame old..new -- spaces.txt >out-blame &&
213 compare_blame spaces-expect out-blame2
214'
215
216test_done