]>
Commit | Line | Data |
---|---|---|
c2a06369 JH |
1 | #!/bin/sh |
2 | ||
3 | test_description='git blame corner cases' | |
747f6c68 | 4 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
5 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
6 | ||
c2a06369 JH |
7 | . ./test-lib.sh |
8 | ||
9 | pick_fc='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/' | |
10 | ||
11 | test_expect_success setup ' | |
c2a06369 JH |
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 && | |
08495412 ES |
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 && | |
00fb3d21 | 18 | git add one two tres mouse nine_lines ten_lines && |
c2a06369 JH |
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 && | |
99094a7a | 25 | echo DEF >>mouse && |
c2a06369 JH |
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 | ||
c66b4700 MH |
40 | cat >cow <<-\EOF && |
41 | ABC | |
42 | DEF | |
43 | XXXX | |
44 | GHIJK | |
45 | EOF | |
c2a06369 JH |
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 && | |
c66b4700 MH |
114 | cat >expected <<-\EOF && |
115 | mouse-Initial | |
116 | mouse-Second | |
117 | mouse-Third | |
118 | EOF | |
82ebb0b6 | 119 | test_cmp expected current |
c2a06369 JH |
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 && | |
c66b4700 MH |
126 | cat >expected <<-\EOF && |
127 | mouse-Initial | |
128 | mouse-Second | |
129 | cow-Fifth | |
130 | mouse-Third | |
131 | EOF | |
82ebb0b6 | 132 | test_cmp expected current |
c2a06369 JH |
133 | |
134 | ' | |
135 | ||
3b75ee93 MH |
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 | ||
747f6c68 | 161 | test_when_finished "git reset --hard && git checkout main" && |
3b75ee93 MH |
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" && | |
747f6c68 | 167 | git checkout --detach main && |
3b75ee93 MH |
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 && | |
3b75ee93 MH |
172 | cat >expected <<-\EOF && |
173 | mouse-Initial | |
174 | mouse-Second | |
175 | rodent-Not | |
176 | EOF | |
177 | test_cmp expected current | |
178 | ' | |
179 | ||
a9b2d424 JH |
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 | ||
c8cba791 | 195 | test_expect_success 'blame to a commit with no author name' ' |
844116d9 | 196 | TREE=$(git rev-parse HEAD:) && |
99094a7a | 197 | cat >badcommit <<EOF && |
c8cba791 DR |
198 | tree $TREE |
199 | author <noname> 1234567890 +0000 | |
200 | committer David Reiss <dreiss@facebook.com> 1234567890 +0000 | |
201 | ||
202 | some message | |
203 | EOF | |
34959d80 | 204 | COMMIT=$(git hash-object --literally -t commit -w badcommit) && |
c8cba791 DR |
205 | git --no-pager blame $COMMIT -- uno >/dev/null |
206 | ' | |
207 | ||
92f9e273 | 208 | test_expect_success 'blame -L with invalid start' ' |
33f0ea42 | 209 | test_must_fail git blame -L5 tres 2>errors && |
6789275d | 210 | test_grep "has only 2 lines" errors |
92f9e273 JS |
211 | ' |
212 | ||
213 | test_expect_success 'blame -L with invalid end' ' | |
96cfa94e IS |
214 | git blame -L1,5 tres >out && |
215 | test_line_count = 2 out | |
92f9e273 JS |
216 | ' |
217 | ||
25ed3412 BY |
218 | test_expect_success 'blame parses <end> part of -L' ' |
219 | git blame -L1,1 tres >out && | |
96cfa94e IS |
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 | |
25ed3412 BY |
226 | ' |
227 | ||
00fb3d21 RS |
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 | ||
4bf256d6 | 238 | test_expect_success 'setup file with CRLF newlines' ' |
4d4813a5 | 239 | git config core.autocrlf false && |
4bf256d6 | 240 | printf "testcase\n" >crlffile && |
4d4813a5 | 241 | git add crlffile && |
242 | git commit -m testcase && | |
4bf256d6 TB |
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 && | |
4d4813a5 | 256 | grep "A U Thor" actual |
257 | ' | |
258 | ||
a08feb8e TB |
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 | ||
dd7c6111 | 273 | test_expect_success 'setup coalesce tests' ' |
f0cbe742 BR |
274 | cat >giraffe <<-\EOF && |
275 | ABC | |
276 | DEF | |
277 | EOF | |
278 | git add giraffe && | |
279 | git commit -m "original file" && | |
dd7c6111 | 280 | orig=$(git rev-parse HEAD) && |
f0cbe742 BR |
281 | |
282 | cat >giraffe <<-\EOF && | |
283 | ABC | |
284 | SPLIT | |
285 | DEF | |
286 | EOF | |
287 | git add giraffe && | |
288 | git commit -m "interior SPLIT line" && | |
dd7c6111 | 289 | split=$(git rev-parse HEAD) && |
f0cbe742 BR |
290 | |
291 | cat >giraffe <<-\EOF && | |
292 | ABC | |
293 | DEF | |
294 | EOF | |
295 | git add giraffe && | |
296 | git commit -m "same contents as original" && | |
dd7c6111 JK |
297 | final=$(git rev-parse HEAD) |
298 | ' | |
f0cbe742 | 299 | |
dd7c6111 | 300 | test_expect_success 'blame coalesce' ' |
f0cbe742 | 301 | cat >expect <<-EOF && |
dd7c6111 JK |
302 | $orig 1 1 2 |
303 | $orig 2 2 | |
f0cbe742 | 304 | EOF |
dd7c6111 JK |
305 | git blame --porcelain $final giraffe >actual.raw && |
306 | grep "^$orig" actual.raw >actual && | |
f0cbe742 BR |
307 | test_cmp expect actual |
308 | ' | |
f0cbe742 | 309 | |
c2ebaa27 | 310 | test_expect_success 'blame does not coalesce non-adjacent result lines' ' |
f0cbe742 | 311 | cat >expect <<-EOF && |
c2ebaa27 JK |
312 | $orig 1) ABC |
313 | $orig 3) DEF | |
f0cbe742 | 314 | EOF |
c2ebaa27 | 315 | git blame --no-abbrev -s -L1,1 -L3,3 $split giraffe >actual && |
f0cbe742 BR |
316 | test_cmp expect actual |
317 | ' | |
318 | ||
c2a06369 | 319 | test_done |