3 test_description='git blame corner cases'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9 pick_fc='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/'
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 &&
16 for i in 1 2 3 4 5 6 7 8 9
20 for i in 1 2 3 4 5 6 7 8 9 a
24 git add one two tres mouse nine_lines ten_lines &&
26 GIT_AUTHOR_NAME=Initial git commit -m Initial &&
32 git add uno dos tres mouse &&
34 GIT_AUTHOR_NAME=Second git commit -a -m Second &&
39 GIT_AUTHOR_NAME=Third git commit -m Third &&
44 GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
54 GIT_AUTHOR_NAME=Fifth git commit -m Fifth
57 test_expect_success 'straight copy without -C' '
59 git blame uno | grep Second
63 test_expect_success 'straight move without -C' '
65 git blame dos | grep Initial
69 test_expect_success 'straight copy with -C' '
71 git blame -C1 uno | grep Second
75 test_expect_success 'straight move with -C' '
77 git blame -C1 dos | grep Initial
81 test_expect_success 'straight copy with -C -C' '
83 git blame -C -C1 uno | grep Initial
87 test_expect_success 'straight move with -C -C' '
89 git blame -C -C1 dos | grep Initial
93 test_expect_success 'append without -C' '
95 git blame -L2 tres | grep Second
99 test_expect_success 'append with -C' '
101 git blame -L2 -C1 tres | grep Second
105 test_expect_success 'append with -C -C' '
107 git blame -L2 -C -C1 tres | grep Second
111 test_expect_success 'append with -C -C -C' '
113 git blame -L2 -C -C -C1 tres | grep Initial
117 test_expect_success 'blame wholesale copy' '
119 git blame -f -C -C1 HEAD^ -- cow | sed -e "$pick_fc" >current &&
120 cat >expected <<-\EOF &&
125 test_cmp expected current
129 test_expect_success 'blame wholesale copy and more' '
131 git blame -f -C -C1 HEAD -- cow | sed -e "$pick_fc" >current &&
132 cat >expected <<-\EOF &&
138 test_cmp expected current
142 test_expect_success 'blame wholesale copy and more in the index' '
144 cat >horse <<-\EOF &&
152 test_when_finished "git rm -f horse" &&
153 git blame -f -C -C1 -- horse | sed -e "$pick_fc" >current &&
154 cat >expected <<-\EOF &&
161 test_cmp expected current
165 test_expect_success 'blame during cherry-pick with file rename conflict' '
167 test_when_finished "git reset --hard && git checkout main" &&
168 git checkout HEAD~3 &&
169 echo MOUSE >> mouse &&
170 git mv mouse 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 &&
177 git blame -f -C -C1 rodent | sed -e "$pick_fc" >current &&
178 cat >expected <<-\EOF &&
183 test_cmp expected current
186 test_expect_success 'blame path that used to be a directory' '
188 echo A A A A A >path/file &&
189 echo B B B B B >path/elif &&
192 git commit -m "path was a directory" &&
194 echo A A A A A >path &&
197 git commit -m "path is a regular file" &&
198 git blame HEAD^.. -- path
201 test_expect_success 'blame to a commit with no author name' '
202 TREE=$(git rev-parse HEAD:) &&
203 cat >badcommit <<EOF &&
205 author <noname> 1234567890 +0000
206 committer David Reiss <dreiss@facebook.com> 1234567890 +0000
210 COMMIT=$(git hash-object -t commit -w badcommit) &&
211 git --no-pager blame $COMMIT -- uno >/dev/null
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
219 test_expect_success 'blame -L with invalid end' '
220 git blame -L1,5 tres >out &&
221 test_line_count = 2 out
224 test_expect_success 'blame parses <end> part of -L' '
225 git blame -L1,1 tres >out &&
226 test_line_count = 1 out
229 test_expect_success 'blame -Ln,-(n+1)' '
230 git blame -L3,-4 nine_lines >out &&
231 test_line_count = 3 out
234 test_expect_success 'indent of line numbers, nine lines' '
235 git blame nine_lines >actual &&
236 test $(grep -c " " actual) = 0
239 test_expect_success 'indent of line numbers, ten lines' '
240 git blame ten_lines >actual &&
241 test $(grep -c " " actual) = 9
244 test_expect_success 'setup file with CRLF newlines' '
245 git config core.autocrlf false &&
246 printf "testcase\n" >crlffile &&
248 git commit -m testcase &&
249 printf "testcase\r\n" >crlffile
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
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
265 test_expect_success 'blame file with CRLF core.autocrlf=true' '
266 git config core.autocrlf false &&
267 printf "testcase\r\n" >crlfinrepo &&
269 git add crlfinrepo &&
270 git commit -m "add crlfinrepo" &&
271 git config core.autocrlf true &&
273 git checkout crlfinrepo &&
275 git blame crlfinrepo >actual &&
276 grep "A U Thor" actual
279 test_expect_success 'setup coalesce tests' '
280 cat >giraffe <<-\EOF &&
285 git commit -m "original file" &&
286 orig=$(git rev-parse HEAD) &&
288 cat >giraffe <<-\EOF &&
294 git commit -m "interior SPLIT line" &&
295 split=$(git rev-parse HEAD) &&
297 cat >giraffe <<-\EOF &&
302 git commit -m "same contents as original" &&
303 final=$(git rev-parse HEAD)
306 test_expect_success 'blame coalesce' '
307 cat >expect <<-EOF &&
311 git blame --porcelain $final giraffe >actual.raw &&
312 grep "^$orig" actual.raw >actual &&
313 test_cmp expect actual
316 test_expect_success 'blame does not coalesce non-adjacent result lines' '
317 cat >expect <<-EOF &&
321 git blame --no-abbrev -s -L1,1 -L3,3 $split giraffe >actual &&
322 test_cmp expect actual