Merge branch 'ja/doc-synopsis-markup'
[git/gitster.git] / t / t8003-blame-corner-cases.sh
blob6288352f5777b3eed84b891c79ded307502f8ed6
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_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
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 &&
16 echo ABC >mouse &&
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 &&
20 test_tick &&
21 GIT_AUTHOR_NAME=Initial git commit -m Initial &&
23 cat one >uno &&
24 mv two dos &&
25 cat one >>tres &&
26 echo DEF >>mouse &&
27 git add uno dos tres mouse &&
28 test_tick &&
29 GIT_AUTHOR_NAME=Second git commit -a -m Second &&
31 echo GHIJK >>mouse &&
32 git add mouse &&
33 test_tick &&
34 GIT_AUTHOR_NAME=Third git commit -m Third &&
36 cat mouse >cow &&
37 git add cow &&
38 test_tick &&
39 GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
41 cat >cow <<-\EOF &&
42 ABC
43 DEF
44 XXXX
45 GHIJK
46 EOF
47 git add cow &&
48 test_tick &&
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 &&
116 mouse-Initial
117 mouse-Second
118 mouse-Third
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 &&
128 mouse-Initial
129 mouse-Second
130 cow-Fifth
131 mouse-Third
133 test_cmp expected current
137 test_expect_success 'blame wholesale copy and more in the index' '
139 cat >horse <<-\EOF &&
142 XXXX
143 YYYY
144 GHIJK
146 git add horse &&
147 test_when_finished "git rm -f horse" &&
148 git blame -f -C -C1 -- horse | sed -e "$pick_fc" >current &&
149 cat >expected <<-\EOF &&
150 mouse-Initial
151 mouse-Second
152 cow-Fifth
153 horse-Not
154 mouse-Third
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 &&
166 git add 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 &&
171 git add rodent &&
172 git blame -f -C -C1 rodent | sed -e "$pick_fc" >current &&
173 cat >expected <<-\EOF &&
174 mouse-Initial
175 mouse-Second
176 rodent-Not
178 test_cmp expected current
181 test_expect_success 'blame path that used to be a directory' '
182 mkdir path &&
183 echo A A A A A >path/file &&
184 echo B B B B B >path/elif &&
185 git add path &&
186 test_tick &&
187 git commit -m "path was a directory" &&
188 rm -fr path &&
189 echo A A A A A >path &&
190 git add path &&
191 test_tick &&
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 &&
199 tree $TREE
200 author <noname> 1234567890 +0000
201 committer David Reiss <dreiss@facebook.com> 1234567890 +0000
203 some message
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 &&
242 git add 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 &&
263 >.gitattributes &&
264 git add crlfinrepo &&
265 git commit -m "add crlfinrepo" &&
266 git config core.autocrlf true &&
267 mv crlfinrepo tmp &&
268 git checkout crlfinrepo &&
269 rm tmp &&
270 git blame crlfinrepo >actual &&
271 grep "A U Thor" actual
274 test_expect_success 'setup coalesce tests' '
275 cat >giraffe <<-\EOF &&
279 git add giraffe &&
280 git commit -m "original file" &&
281 orig=$(git rev-parse HEAD) &&
283 cat >giraffe <<-\EOF &&
285 SPLIT
288 git add giraffe &&
289 git commit -m "interior SPLIT line" &&
290 split=$(git rev-parse HEAD) &&
292 cat >giraffe <<-\EOF &&
296 git add giraffe &&
297 git commit -m "same contents as original" &&
298 final=$(git rev-parse HEAD)
301 test_expect_success 'blame coalesce' '
302 cat >expect <<-EOF &&
303 $orig 1 1 2
304 $orig 2 2
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 &&
313 $orig 1) ABC
314 $orig 3) DEF
316 git blame --no-abbrev -s -L1,1 -L3,3 $split giraffe >actual &&
317 test_cmp expect actual
320 test_done