Merge branch 'ds/t1092-fix-flake-from-progress'
[git] / t / t8003-blame-corner-cases.sh
1 #!/bin/sh
2
3 test_description='git blame corner cases'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 . ./test-lib.sh
8
9 pick_fc='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/'
10
11 test_expect_success setup '
12         echo A A A A A >one &&
13         echo B B B B B >two &&
14         echo C C C C C >tres &&
15         echo ABC >mouse &&
16         for i in 1 2 3 4 5 6 7 8 9
17         do
18                 echo $i
19         done >nine_lines &&
20         for i in 1 2 3 4 5 6 7 8 9 a
21         do
22                 echo $i
23         done >ten_lines &&
24         git add one two tres mouse nine_lines ten_lines &&
25         test_tick &&
26         GIT_AUTHOR_NAME=Initial git commit -m Initial &&
27
28         cat one >uno &&
29         mv two dos &&
30         cat one >>tres &&
31         echo DEF >>mouse &&
32         git add uno dos tres mouse &&
33         test_tick &&
34         GIT_AUTHOR_NAME=Second git commit -a -m Second &&
35
36         echo GHIJK >>mouse &&
37         git add mouse &&
38         test_tick &&
39         GIT_AUTHOR_NAME=Third git commit -m Third &&
40
41         cat mouse >cow &&
42         git add cow &&
43         test_tick &&
44         GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
45
46         cat >cow <<-\EOF &&
47         ABC
48         DEF
49         XXXX
50         GHIJK
51         EOF
52         git add cow &&
53         test_tick &&
54         GIT_AUTHOR_NAME=Fifth git commit -m Fifth
55 '
56
57 test_expect_success 'straight copy without -C' '
58
59         git blame uno | grep Second
60
61 '
62
63 test_expect_success 'straight move without -C' '
64
65         git blame dos | grep Initial
66
67 '
68
69 test_expect_success 'straight copy with -C' '
70
71         git blame -C1 uno | grep Second
72
73 '
74
75 test_expect_success 'straight move with -C' '
76
77         git blame -C1 dos | grep Initial
78
79 '
80
81 test_expect_success 'straight copy with -C -C' '
82
83         git blame -C -C1 uno | grep Initial
84
85 '
86
87 test_expect_success 'straight move with -C -C' '
88
89         git blame -C -C1 dos | grep Initial
90
91 '
92
93 test_expect_success 'append without -C' '
94
95         git blame -L2 tres | grep Second
96
97 '
98
99 test_expect_success 'append with -C' '
100
101         git blame -L2 -C1 tres | grep Second
102
103 '
104
105 test_expect_success 'append with -C -C' '
106
107         git blame -L2 -C -C1 tres | grep Second
108
109 '
110
111 test_expect_success 'append with -C -C -C' '
112
113         git blame -L2 -C -C -C1 tres | grep Initial
114
115 '
116
117 test_expect_success 'blame wholesale copy' '
118
119         git blame -f -C -C1 HEAD^ -- cow | sed -e "$pick_fc" >current &&
120         cat >expected <<-\EOF &&
121         mouse-Initial
122         mouse-Second
123         mouse-Third
124         EOF
125         test_cmp expected current
126
127 '
128
129 test_expect_success 'blame wholesale copy and more' '
130
131         git blame -f -C -C1 HEAD -- cow | sed -e "$pick_fc" >current &&
132         cat >expected <<-\EOF &&
133         mouse-Initial
134         mouse-Second
135         cow-Fifth
136         mouse-Third
137         EOF
138         test_cmp expected current
139
140 '
141
142 test_expect_success 'blame wholesale copy and more in the index' '
143
144         cat >horse <<-\EOF &&
145         ABC
146         DEF
147         XXXX
148         YYYY
149         GHIJK
150         EOF
151         git add horse &&
152         test_when_finished "git rm -f horse" &&
153         git blame -f -C -C1 -- horse | sed -e "$pick_fc" >current &&
154         cat >expected <<-\EOF &&
155         mouse-Initial
156         mouse-Second
157         cow-Fifth
158         horse-Not
159         mouse-Third
160         EOF
161         test_cmp expected current
162
163 '
164
165 test_expect_success 'blame during cherry-pick with file rename conflict' '
166
167         test_when_finished "git reset --hard && git checkout main" &&
168         git checkout HEAD~3 &&
169         echo MOUSE >> mouse &&
170         git mv mouse rodent &&
171         git add rodent &&
172         GIT_AUTHOR_NAME=Rodent git commit -m "rodent" &&
173         git checkout --detach main &&
174         (git cherry-pick HEAD@{1} || test $? -eq 1) &&
175         git show HEAD@{1}:rodent > rodent &&
176         git add rodent &&
177         git blame -f -C -C1 rodent | sed -e "$pick_fc" >current &&
178         cat >expected <<-\EOF &&
179         mouse-Initial
180         mouse-Second
181         rodent-Not
182         EOF
183         test_cmp expected current
184 '
185
186 test_expect_success 'blame path that used to be a directory' '
187         mkdir path &&
188         echo A A A A A >path/file &&
189         echo B B B B B >path/elif &&
190         git add path &&
191         test_tick &&
192         git commit -m "path was a directory" &&
193         rm -fr path &&
194         echo A A A A A >path &&
195         git add path &&
196         test_tick &&
197         git commit -m "path is a regular file" &&
198         git blame HEAD^.. -- path
199 '
200
201 test_expect_success 'blame to a commit with no author name' '
202   TREE=$(git rev-parse HEAD:) &&
203   cat >badcommit <<EOF &&
204 tree $TREE
205 author <noname> 1234567890 +0000
206 committer David Reiss <dreiss@facebook.com> 1234567890 +0000
207
208 some message
209 EOF
210   COMMIT=$(git hash-object -t commit -w badcommit) &&
211   git --no-pager blame $COMMIT -- uno >/dev/null
212 '
213
214 test_expect_success 'blame -L with invalid start' '
215         test_must_fail git blame -L5 tres 2>errors &&
216         test_i18ngrep "has only 2 lines" errors
217 '
218
219 test_expect_success 'blame -L with invalid end' '
220         git blame -L1,5 tres >out &&
221         test_line_count = 2 out
222 '
223
224 test_expect_success 'blame parses <end> part of -L' '
225         git blame -L1,1 tres >out &&
226         test_line_count = 1 out
227 '
228
229 test_expect_success 'blame -Ln,-(n+1)' '
230         git blame -L3,-4 nine_lines >out &&
231         test_line_count = 3 out
232 '
233
234 test_expect_success 'indent of line numbers, nine lines' '
235         git blame nine_lines >actual &&
236         test $(grep -c "  " actual) = 0
237 '
238
239 test_expect_success 'indent of line numbers, ten lines' '
240         git blame ten_lines >actual &&
241         test $(grep -c "  " actual) = 9
242 '
243
244 test_expect_success 'setup file with CRLF newlines' '
245         git config core.autocrlf false &&
246         printf "testcase\n" >crlffile &&
247         git add crlffile &&
248         git commit -m testcase &&
249         printf "testcase\r\n" >crlffile
250 '
251
252 test_expect_success 'blame file with CRLF core.autocrlf true' '
253         git config core.autocrlf true &&
254         git blame crlffile >actual &&
255         grep "A U Thor" actual
256 '
257
258 test_expect_success 'blame file with CRLF attributes text' '
259         git config core.autocrlf false &&
260         echo "crlffile text" >.gitattributes &&
261         git blame crlffile >actual &&
262         grep "A U Thor" actual
263 '
264
265 test_expect_success 'blame file with CRLF core.autocrlf=true' '
266         git config core.autocrlf false &&
267         printf "testcase\r\n" >crlfinrepo &&
268         >.gitattributes &&
269         git add crlfinrepo &&
270         git commit -m "add crlfinrepo" &&
271         git config core.autocrlf true &&
272         mv crlfinrepo tmp &&
273         git checkout crlfinrepo &&
274         rm tmp &&
275         git blame crlfinrepo >actual &&
276         grep "A U Thor" actual
277 '
278
279 test_expect_success 'setup coalesce tests' '
280         cat >giraffe <<-\EOF &&
281         ABC
282         DEF
283         EOF
284         git add giraffe &&
285         git commit -m "original file" &&
286         orig=$(git rev-parse HEAD) &&
287
288         cat >giraffe <<-\EOF &&
289         ABC
290         SPLIT
291         DEF
292         EOF
293         git add giraffe &&
294         git commit -m "interior SPLIT line" &&
295         split=$(git rev-parse HEAD) &&
296
297         cat >giraffe <<-\EOF &&
298         ABC
299         DEF
300         EOF
301         git add giraffe &&
302         git commit -m "same contents as original" &&
303         final=$(git rev-parse HEAD)
304 '
305
306 test_expect_success 'blame coalesce' '
307         cat >expect <<-EOF &&
308         $orig 1 1 2
309         $orig 2 2
310         EOF
311         git blame --porcelain $final giraffe >actual.raw &&
312         grep "^$orig" actual.raw >actual &&
313         test_cmp expect actual
314 '
315
316 test_expect_success 'blame does not coalesce non-adjacent result lines' '
317         cat >expect <<-EOF &&
318         $orig 1) ABC
319         $orig 3) DEF
320         EOF
321         git blame --no-abbrev -s -L1,1 -L3,3 $split giraffe >actual &&
322         test_cmp expect actual
323 '
324
325 test_done