]> git.ipfire.org Git - thirdparty/git.git/blob - t/t8003-blame-corner-cases.sh
Merge branch 'jc/add-i-use-builtin-experimental'
[thirdparty/git.git] / t / t8003-blame-corner-cases.sh
1 #!/bin/sh
2
3 test_description='git blame corner cases'
4 . ./test-lib.sh
5
6 pick_fc='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/'
7
8 test_expect_success setup '
9 echo A A A A A >one &&
10 echo B B B B B >two &&
11 echo C C C C C >tres &&
12 echo ABC >mouse &&
13 for i in 1 2 3 4 5 6 7 8 9
14 do
15 echo $i
16 done >nine_lines &&
17 for i in 1 2 3 4 5 6 7 8 9 a
18 do
19 echo $i
20 done >ten_lines &&
21 git add one two tres mouse nine_lines ten_lines &&
22 test_tick &&
23 GIT_AUTHOR_NAME=Initial git commit -m Initial &&
24
25 cat one >uno &&
26 mv two dos &&
27 cat one >>tres &&
28 echo DEF >>mouse &&
29 git add uno dos tres mouse &&
30 test_tick &&
31 GIT_AUTHOR_NAME=Second git commit -a -m Second &&
32
33 echo GHIJK >>mouse &&
34 git add mouse &&
35 test_tick &&
36 GIT_AUTHOR_NAME=Third git commit -m Third &&
37
38 cat mouse >cow &&
39 git add cow &&
40 test_tick &&
41 GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
42
43 cat >cow <<-\EOF &&
44 ABC
45 DEF
46 XXXX
47 GHIJK
48 EOF
49 git add cow &&
50 test_tick &&
51 GIT_AUTHOR_NAME=Fifth git commit -m Fifth
52 '
53
54 test_expect_success 'straight copy without -C' '
55
56 git blame uno | grep Second
57
58 '
59
60 test_expect_success 'straight move without -C' '
61
62 git blame dos | grep Initial
63
64 '
65
66 test_expect_success 'straight copy with -C' '
67
68 git blame -C1 uno | grep Second
69
70 '
71
72 test_expect_success 'straight move with -C' '
73
74 git blame -C1 dos | grep Initial
75
76 '
77
78 test_expect_success 'straight copy with -C -C' '
79
80 git blame -C -C1 uno | grep Initial
81
82 '
83
84 test_expect_success 'straight move with -C -C' '
85
86 git blame -C -C1 dos | grep Initial
87
88 '
89
90 test_expect_success 'append without -C' '
91
92 git blame -L2 tres | grep Second
93
94 '
95
96 test_expect_success 'append with -C' '
97
98 git blame -L2 -C1 tres | grep Second
99
100 '
101
102 test_expect_success 'append with -C -C' '
103
104 git blame -L2 -C -C1 tres | grep Second
105
106 '
107
108 test_expect_success 'append with -C -C -C' '
109
110 git blame -L2 -C -C -C1 tres | grep Initial
111
112 '
113
114 test_expect_success 'blame wholesale copy' '
115
116 git blame -f -C -C1 HEAD^ -- cow | sed -e "$pick_fc" >current &&
117 cat >expected <<-\EOF &&
118 mouse-Initial
119 mouse-Second
120 mouse-Third
121 EOF
122 test_cmp expected current
123
124 '
125
126 test_expect_success 'blame wholesale copy and more' '
127
128 git blame -f -C -C1 HEAD -- cow | sed -e "$pick_fc" >current &&
129 cat >expected <<-\EOF &&
130 mouse-Initial
131 mouse-Second
132 cow-Fifth
133 mouse-Third
134 EOF
135 test_cmp expected current
136
137 '
138
139 test_expect_success 'blame wholesale copy and more in the index' '
140
141 cat >horse <<-\EOF &&
142 ABC
143 DEF
144 XXXX
145 YYYY
146 GHIJK
147 EOF
148 git add horse &&
149 test_when_finished "git rm -f horse" &&
150 git blame -f -C -C1 -- horse | sed -e "$pick_fc" >current &&
151 cat >expected <<-\EOF &&
152 mouse-Initial
153 mouse-Second
154 cow-Fifth
155 horse-Not
156 mouse-Third
157 EOF
158 test_cmp expected current
159
160 '
161
162 test_expect_success 'blame during cherry-pick with file rename conflict' '
163
164 test_when_finished "git reset --hard && git checkout master" &&
165 git checkout HEAD~3 &&
166 echo MOUSE >> mouse &&
167 git mv mouse rodent &&
168 git add rodent &&
169 GIT_AUTHOR_NAME=Rodent git commit -m "rodent" &&
170 git checkout --detach master &&
171 (git cherry-pick HEAD@{1} || test $? -eq 1) &&
172 git show HEAD@{1}:rodent > rodent &&
173 git add rodent &&
174 git blame -f -C -C1 rodent | sed -e "$pick_fc" >current &&
175 cat >expected <<-\EOF &&
176 mouse-Initial
177 mouse-Second
178 rodent-Not
179 EOF
180 test_cmp expected current
181 '
182
183 test_expect_success 'blame path that used to be a directory' '
184 mkdir path &&
185 echo A A A A A >path/file &&
186 echo B B B B B >path/elif &&
187 git add path &&
188 test_tick &&
189 git commit -m "path was a directory" &&
190 rm -fr path &&
191 echo A A A A A >path &&
192 git add path &&
193 test_tick &&
194 git commit -m "path is a regular file" &&
195 git blame HEAD^.. -- path
196 '
197
198 test_expect_success 'blame to a commit with no author name' '
199 TREE=$(git rev-parse HEAD:) &&
200 cat >badcommit <<EOF &&
201 tree $TREE
202 author <noname> 1234567890 +0000
203 committer David Reiss <dreiss@facebook.com> 1234567890 +0000
204
205 some message
206 EOF
207 COMMIT=$(git hash-object -t commit -w badcommit) &&
208 git --no-pager blame $COMMIT -- uno >/dev/null
209 '
210
211 test_expect_success 'blame -L with invalid start' '
212 test_must_fail git blame -L5 tres 2>errors &&
213 test_i18ngrep "has only 2 lines" errors
214 '
215
216 test_expect_success 'blame -L with invalid end' '
217 git blame -L1,5 tres >out &&
218 test_line_count = 2 out
219 '
220
221 test_expect_success 'blame parses <end> part of -L' '
222 git blame -L1,1 tres >out &&
223 test_line_count = 1 out
224 '
225
226 test_expect_success 'blame -Ln,-(n+1)' '
227 git blame -L3,-4 nine_lines >out &&
228 test_line_count = 3 out
229 '
230
231 test_expect_success 'indent of line numbers, nine lines' '
232 git blame nine_lines >actual &&
233 test $(grep -c " " actual) = 0
234 '
235
236 test_expect_success 'indent of line numbers, ten lines' '
237 git blame ten_lines >actual &&
238 test $(grep -c " " actual) = 9
239 '
240
241 test_expect_success 'setup file with CRLF newlines' '
242 git config core.autocrlf false &&
243 printf "testcase\n" >crlffile &&
244 git add crlffile &&
245 git commit -m testcase &&
246 printf "testcase\r\n" >crlffile
247 '
248
249 test_expect_success 'blame file with CRLF core.autocrlf true' '
250 git config core.autocrlf true &&
251 git blame crlffile >actual &&
252 grep "A U Thor" actual
253 '
254
255 test_expect_success 'blame file with CRLF attributes text' '
256 git config core.autocrlf false &&
257 echo "crlffile text" >.gitattributes &&
258 git blame crlffile >actual &&
259 grep "A U Thor" actual
260 '
261
262 test_expect_success 'blame file with CRLF core.autocrlf=true' '
263 git config core.autocrlf false &&
264 printf "testcase\r\n" >crlfinrepo &&
265 >.gitattributes &&
266 git add crlfinrepo &&
267 git commit -m "add crlfinrepo" &&
268 git config core.autocrlf true &&
269 mv crlfinrepo tmp &&
270 git checkout crlfinrepo &&
271 rm tmp &&
272 git blame crlfinrepo >actual &&
273 grep "A U Thor" actual
274 '
275
276 test_expect_success 'setup coalesce tests' '
277 cat >giraffe <<-\EOF &&
278 ABC
279 DEF
280 EOF
281 git add giraffe &&
282 git commit -m "original file" &&
283 orig=$(git rev-parse HEAD) &&
284
285 cat >giraffe <<-\EOF &&
286 ABC
287 SPLIT
288 DEF
289 EOF
290 git add giraffe &&
291 git commit -m "interior SPLIT line" &&
292 split=$(git rev-parse HEAD) &&
293
294 cat >giraffe <<-\EOF &&
295 ABC
296 DEF
297 EOF
298 git add giraffe &&
299 git commit -m "same contents as original" &&
300 final=$(git rev-parse HEAD)
301 '
302
303 test_expect_success 'blame coalesce' '
304 cat >expect <<-EOF &&
305 $orig 1 1 2
306 $orig 2 2
307 EOF
308 git blame --porcelain $final giraffe >actual.raw &&
309 grep "^$orig" actual.raw >actual &&
310 test_cmp expect actual
311 '
312
313 test_expect_success 'blame does not coalesce non-adjacent result lines' '
314 cat >expect <<-EOF &&
315 $orig 1) ABC
316 $orig 3) DEF
317 EOF
318 git blame --no-abbrev -s -L1,1 -L3,3 $split giraffe >actual &&
319 test_cmp expect actual
320 '
321
322 test_done