3 test_description
='git blame corner cases'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 TEST_PASSES_SANITIZE_LEAK
=true
10 pick_fc
='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/'
12 test_expect_success setup
'
13 echo A A A A A >one &&
14 echo B B B B B >two &&
15 echo C C C C C >tres &&
17 test_write_lines 1 2 3 4 5 6 7 8 9 >nine_lines &&
18 test_write_lines 1 2 3 4 5 6 7 8 9 a >ten_lines &&
19 git add one two tres mouse nine_lines ten_lines &&
21 GIT_AUTHOR_NAME=Initial git commit -m Initial &&
27 git add uno dos tres mouse &&
29 GIT_AUTHOR_NAME=Second git commit -a -m Second &&
34 GIT_AUTHOR_NAME=Third git commit -m Third &&
39 GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
49 GIT_AUTHOR_NAME=Fifth git commit -m Fifth
52 test_expect_success
'straight copy without -C' '
54 git blame uno | grep Second
58 test_expect_success
'straight move without -C' '
60 git blame dos | grep Initial
64 test_expect_success
'straight copy with -C' '
66 git blame -C1 uno | grep Second
70 test_expect_success
'straight move with -C' '
72 git blame -C1 dos | grep Initial
76 test_expect_success
'straight copy with -C -C' '
78 git blame -C -C1 uno | grep Initial
82 test_expect_success
'straight move with -C -C' '
84 git blame -C -C1 dos | grep Initial
88 test_expect_success
'append without -C' '
90 git blame -L2 tres | grep Second
94 test_expect_success
'append with -C' '
96 git blame -L2 -C1 tres | grep Second
100 test_expect_success
'append with -C -C' '
102 git blame -L2 -C -C1 tres | grep Second
106 test_expect_success
'append with -C -C -C' '
108 git blame -L2 -C -C -C1 tres | grep Initial
112 test_expect_success
'blame wholesale copy' '
114 git blame -f -C -C1 HEAD^ -- cow | sed -e "$pick_fc" >current &&
115 cat >expected <<-\EOF &&
120 test_cmp expected current
124 test_expect_success
'blame wholesale copy and more' '
126 git blame -f -C -C1 HEAD -- cow | sed -e "$pick_fc" >current &&
127 cat >expected <<-\EOF &&
133 test_cmp expected current
137 test_expect_success
'blame wholesale copy and more in the index' '
139 cat >horse <<-\EOF &&
147 test_when_finished "git rm -f horse" &&
148 git blame -f -C -C1 -- horse | sed -e "$pick_fc" >current &&
149 cat >expected <<-\EOF &&
156 test_cmp expected current
160 test_expect_success
'blame during cherry-pick with file rename conflict' '
162 test_when_finished "git reset --hard && git checkout main" &&
163 git checkout HEAD~3 &&
164 echo MOUSE >> mouse &&
165 git mv mouse rodent &&
167 GIT_AUTHOR_NAME=Rodent git commit -m "rodent" &&
168 git checkout --detach main &&
169 (git cherry-pick HEAD@{1} || test $? -eq 1) &&
170 git show HEAD@{1}:rodent > rodent &&
172 git blame -f -C -C1 rodent | sed -e "$pick_fc" >current &&
173 cat >expected <<-\EOF &&
178 test_cmp expected current
181 test_expect_success
'blame path that used to be a directory' '
183 echo A A A A A >path/file &&
184 echo B B B B B >path/elif &&
187 git commit -m "path was a directory" &&
189 echo A A A A A >path &&
192 git commit -m "path is a regular file" &&
193 git blame HEAD^.. -- path
196 test_expect_success
'blame to a commit with no author name' '
197 TREE=$(git rev-parse HEAD:) &&
198 cat >badcommit <<EOF &&
200 author <noname> 1234567890 +0000
201 committer David Reiss <dreiss@facebook.com> 1234567890 +0000
205 COMMIT=$(git hash-object --literally -t commit -w badcommit) &&
206 git --no-pager blame $COMMIT -- uno >/dev/null
209 test_expect_success
'blame -L with invalid start' '
210 test_must_fail git blame -L5 tres 2>errors &&
211 test_grep "has only 2 lines" errors
214 test_expect_success
'blame -L with invalid end' '
215 git blame -L1,5 tres >out &&
216 test_line_count = 2 out
219 test_expect_success
'blame parses <end> part of -L' '
220 git blame -L1,1 tres >out &&
221 test_line_count = 1 out
224 test_expect_success
'blame -Ln,-(n+1)' '
225 git blame -L3,-4 nine_lines >out &&
226 test_line_count = 3 out
229 test_expect_success
'indent of line numbers, nine lines' '
230 git blame nine_lines >actual &&
231 test $(grep -c " " actual) = 0
234 test_expect_success
'indent of line numbers, ten lines' '
235 git blame ten_lines >actual &&
236 test $(grep -c " " actual) = 9
239 test_expect_success
'setup file with CRLF newlines' '
240 git config core.autocrlf false &&
241 printf "testcase\n" >crlffile &&
243 git commit -m testcase &&
244 printf "testcase\r\n" >crlffile
247 test_expect_success
'blame file with CRLF core.autocrlf true' '
248 git config core.autocrlf true &&
249 git blame crlffile >actual &&
250 grep "A U Thor" actual
253 test_expect_success
'blame file with CRLF attributes text' '
254 git config core.autocrlf false &&
255 echo "crlffile text" >.gitattributes &&
256 git blame crlffile >actual &&
257 grep "A U Thor" actual
260 test_expect_success
'blame file with CRLF core.autocrlf=true' '
261 git config core.autocrlf false &&
262 printf "testcase\r\n" >crlfinrepo &&
264 git add crlfinrepo &&
265 git commit -m "add crlfinrepo" &&
266 git config core.autocrlf true &&
268 git checkout crlfinrepo &&
270 git blame crlfinrepo >actual &&
271 grep "A U Thor" actual
274 test_expect_success
'setup coalesce tests' '
275 cat >giraffe <<-\EOF &&
280 git commit -m "original file" &&
281 orig=$(git rev-parse HEAD) &&
283 cat >giraffe <<-\EOF &&
289 git commit -m "interior SPLIT line" &&
290 split=$(git rev-parse HEAD) &&
292 cat >giraffe <<-\EOF &&
297 git commit -m "same contents as original" &&
298 final=$(git rev-parse HEAD)
301 test_expect_success
'blame coalesce' '
302 cat >expect <<-EOF &&
306 git blame --porcelain $final giraffe >actual.raw &&
307 grep "^$orig" actual.raw >actual &&
308 test_cmp expect actual
311 test_expect_success
'blame does not coalesce non-adjacent result lines' '
312 cat >expect <<-EOF &&
316 git blame --no-abbrev -s -L1,1 -L3,3 $split giraffe >actual &&
317 test_cmp expect actual