pickaxe tests: refactor to use test_commit --append --printf
[git/debian.git] / t / t8003-blame-corner-cases.sh
blobda80f815ce9d38f109df07a8dcbe803f8a794309
1 #!/bin/sh
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-lib.sh
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 &&
15 echo ABC >mouse &&
16 for i in 1 2 3 4 5 6 7 8 9
18 echo $i
19 done >nine_lines &&
20 for i in 1 2 3 4 5 6 7 8 9 a
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 &&
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 &&
36 echo GHIJK >>mouse &&
37 git add mouse &&
38 test_tick &&
39 GIT_AUTHOR_NAME=Third git commit -m Third &&
41 cat mouse >cow &&
42 git add cow &&
43 test_tick &&
44 GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
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
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 &&
121 mouse-Initial
122 mouse-Second
123 mouse-Third
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 &&
133 mouse-Initial
134 mouse-Second
135 cow-Fifth
136 mouse-Third
138 test_cmp expected current
142 test_expect_success 'blame wholesale copy and more in the index' '
144 cat >horse <<-\EOF &&
147 XXXX
148 YYYY
149 GHIJK
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
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 &&
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
183 test_cmp expected current
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
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
208 some message
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 &&
247 git add 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 &&
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
279 test_expect_success 'setup coalesce tests' '
280 cat >giraffe <<-\EOF &&
284 git add giraffe &&
285 git commit -m "original file" &&
286 orig=$(git rev-parse HEAD) &&
288 cat >giraffe <<-\EOF &&
290 SPLIT
293 git add giraffe &&
294 git commit -m "interior SPLIT line" &&
295 split=$(git rev-parse HEAD) &&
297 cat >giraffe <<-\EOF &&
301 git add giraffe &&
302 git commit -m "same contents as original" &&
303 final=$(git rev-parse HEAD)
306 test_expect_success 'blame coalesce' '
307 cat >expect <<-EOF &&
308 $orig 1 1 2
309 $orig 2 2
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 &&
318 $orig 1) ABC
319 $orig 3) DEF
321 git blame --no-abbrev -s -L1,1 -L3,3 $split giraffe >actual &&
322 test_cmp expect actual
325 test_done