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 test_write_lines 1 2 3 4 5 6 7 8 9 >nine_lines &&
17 test_write_lines 1 2 3 4 5 6 7 8 9 a >ten_lines &&
18 git add one two tres mouse nine_lines ten_lines &&
20 GIT_AUTHOR_NAME=Initial git commit -m Initial &&
26 git add uno dos tres mouse &&
28 GIT_AUTHOR_NAME=Second git commit -a -m Second &&
33 GIT_AUTHOR_NAME=Third git commit -m Third &&
38 GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
48 GIT_AUTHOR_NAME=Fifth git commit -m Fifth
51 test_expect_success
'straight copy without -C' '
53 git blame uno | grep Second
57 test_expect_success
'straight move without -C' '
59 git blame dos | grep Initial
63 test_expect_success
'straight copy with -C' '
65 git blame -C1 uno | grep Second
69 test_expect_success
'straight move with -C' '
71 git blame -C1 dos | grep Initial
75 test_expect_success
'straight copy with -C -C' '
77 git blame -C -C1 uno | grep Initial
81 test_expect_success
'straight move with -C -C' '
83 git blame -C -C1 dos | grep Initial
87 test_expect_success
'append without -C' '
89 git blame -L2 tres | grep Second
93 test_expect_success
'append with -C' '
95 git blame -L2 -C1 tres | grep Second
99 test_expect_success
'append with -C -C' '
101 git blame -L2 -C -C1 tres | grep Second
105 test_expect_success
'append with -C -C -C' '
107 git blame -L2 -C -C -C1 tres | grep Initial
111 test_expect_success
'blame wholesale copy' '
113 git blame -f -C -C1 HEAD^ -- cow | sed -e "$pick_fc" >current &&
114 cat >expected <<-\EOF &&
119 test_cmp expected current
123 test_expect_success
'blame wholesale copy and more' '
125 git blame -f -C -C1 HEAD -- cow | sed -e "$pick_fc" >current &&
126 cat >expected <<-\EOF &&
132 test_cmp expected current
136 test_expect_success
'blame wholesale copy and more in the index' '
138 cat >horse <<-\EOF &&
146 test_when_finished "git rm -f horse" &&
147 git blame -f -C -C1 -- horse | sed -e "$pick_fc" >current &&
148 cat >expected <<-\EOF &&
155 test_cmp expected current
159 test_expect_success
'blame during cherry-pick with file rename conflict' '
161 test_when_finished "git reset --hard && git checkout main" &&
162 git checkout HEAD~3 &&
163 echo MOUSE >> mouse &&
164 git mv mouse rodent &&
166 GIT_AUTHOR_NAME=Rodent git commit -m "rodent" &&
167 git checkout --detach main &&
168 (git cherry-pick HEAD@{1} || test $? -eq 1) &&
169 git show HEAD@{1}:rodent > rodent &&
171 git blame -f -C -C1 rodent | sed -e "$pick_fc" >current &&
172 cat >expected <<-\EOF &&
177 test_cmp expected current
180 test_expect_success
'blame path that used to be a directory' '
182 echo A A A A A >path/file &&
183 echo B B B B B >path/elif &&
186 git commit -m "path was a directory" &&
188 echo A A A A A >path &&
191 git commit -m "path is a regular file" &&
192 git blame HEAD^.. -- path
195 test_expect_success
'blame to a commit with no author name' '
196 TREE=$(git rev-parse HEAD:) &&
197 cat >badcommit <<EOF &&
199 author <noname> 1234567890 +0000
200 committer David Reiss <dreiss@facebook.com> 1234567890 +0000
204 COMMIT=$(git hash-object --literally -t commit -w badcommit) &&
205 git --no-pager blame $COMMIT -- uno >/dev/null
208 test_expect_success
'blame -L with invalid start' '
209 test_must_fail git blame -L5 tres 2>errors &&
210 test_grep "has only 2 lines" errors
213 test_expect_success
'blame -L with invalid end' '
214 git blame -L1,5 tres >out &&
215 test_line_count = 2 out
218 test_expect_success
'blame parses <end> part of -L' '
219 git blame -L1,1 tres >out &&
220 test_line_count = 1 out
223 test_expect_success
'blame -Ln,-(n+1)' '
224 git blame -L3,-4 nine_lines >out &&
225 test_line_count = 3 out
228 test_expect_success
'indent of line numbers, nine lines' '
229 git blame nine_lines >actual &&
230 test $(grep -c " " actual) = 0
233 test_expect_success
'indent of line numbers, ten lines' '
234 git blame ten_lines >actual &&
235 test $(grep -c " " actual) = 9
238 test_expect_success
'setup file with CRLF newlines' '
239 git config core.autocrlf false &&
240 printf "testcase\n" >crlffile &&
242 git commit -m testcase &&
243 printf "testcase\r\n" >crlffile
246 test_expect_success
'blame file with CRLF core.autocrlf true' '
247 git config core.autocrlf true &&
248 git blame crlffile >actual &&
249 grep "A U Thor" actual
252 test_expect_success
'blame file with CRLF attributes text' '
253 git config core.autocrlf false &&
254 echo "crlffile text" >.gitattributes &&
255 git blame crlffile >actual &&
256 grep "A U Thor" actual
259 test_expect_success
'blame file with CRLF core.autocrlf=true' '
260 git config core.autocrlf false &&
261 printf "testcase\r\n" >crlfinrepo &&
263 git add crlfinrepo &&
264 git commit -m "add crlfinrepo" &&
265 git config core.autocrlf true &&
267 git checkout crlfinrepo &&
269 git blame crlfinrepo >actual &&
270 grep "A U Thor" actual
273 test_expect_success
'setup coalesce tests' '
274 cat >giraffe <<-\EOF &&
279 git commit -m "original file" &&
280 orig=$(git rev-parse HEAD) &&
282 cat >giraffe <<-\EOF &&
288 git commit -m "interior SPLIT line" &&
289 split=$(git rev-parse HEAD) &&
291 cat >giraffe <<-\EOF &&
296 git commit -m "same contents as original" &&
297 final=$(git rev-parse HEAD)
300 test_expect_success
'blame coalesce' '
301 cat >expect <<-EOF &&
305 git blame --porcelain $final giraffe >actual.raw &&
306 grep "^$orig" actual.raw >actual &&
307 test_cmp expect actual
310 test_expect_success
'blame does not coalesce non-adjacent result lines' '
311 cat >expect <<-EOF &&
315 git blame --no-abbrev -s -L1,1 -L3,3 $split giraffe >actual &&
316 test_cmp expect actual