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