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