summaryrefslogtreecommitdiff
path: root/t/t8013-blame-ignore-revs.sh
blob: 36dc31eb391361e002ac83da679a5be6c5b23b35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/bin/sh

test_description='ignore revisions when blaming'
. ./test-lib.sh

# Creates:
# 	A--B--X
# A added line 1 and B added line 2.  X makes changes to those lines.  Sanity
# check that X is blamed for both lines.
test_expect_success setup '
	test_commit A file line1 &&

	echo line2 >>file &&
	git add file &&
	test_tick &&
	git commit -m B &&
	git tag B &&

	test_write_lines line-one line-two >file &&
	git add file &&
	test_tick &&
	git commit -m X &&
	git tag X &&

	git blame --line-porcelain file >blame_raw &&

	grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
	git rev-parse X >expect &&
	test_cmp expect actual &&

	grep -E "^[0-9a-f]+ [0-9]+ 2" blame_raw | sed -e "s/ .*//" >actual &&
	git rev-parse X >expect &&
	test_cmp expect actual
	'

# Ignore X, make sure A is blamed for line 1 and B for line 2.
test_expect_success ignore_rev_changing_lines '
	git blame --line-porcelain --ignore-rev X file >blame_raw &&

	grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
	git rev-parse A >expect &&
	test_cmp expect actual &&

	grep -E "^[0-9a-f]+ [0-9]+ 2" blame_raw | sed -e "s/ .*//" >actual &&
	git rev-parse B >expect &&
	test_cmp expect actual
	'

# For ignored revs that have added 'unblamable' lines, attribute those to the
# ignored commit.
# 	A--B--X--Y
# Where Y changes lines 1 and 2, and adds lines 3 and 4.  The added lines ought
# to have nothing in common with "line-one" or "line-two", to keep any
# heuristics from matching them with any lines in the parent.
test_expect_success ignore_rev_adding_unblamable_lines '
	test_write_lines line-one-change line-two-changed y3 y4 >file &&
	git add file &&
	test_tick &&
	git commit -m Y &&
	git tag Y &&

	git rev-parse Y >expect &&
	git blame --line-porcelain file --ignore-rev Y >blame_raw &&

	grep -E "^[0-9a-f]+ [0-9]+ 3" blame_raw | sed -e "s/ .*//" >actual &&
	test_cmp expect actual &&

	grep -E "^[0-9a-f]+ [0-9]+ 4" blame_raw | sed -e "s/ .*//" >actual &&
	test_cmp expect actual
	'

# Ignore X and Y, both in separate files.  Lines 1 == A, 2 == B.
test_expect_success ignore_revs_from_files '
	git rev-parse X >ignore_x &&
	git rev-parse Y >ignore_y &&
	git blame --line-porcelain file --ignore-revs-file ignore_x --ignore-revs-file ignore_y >blame_raw &&

	grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
	git rev-parse A >expect &&
	test_cmp expect actual &&

	grep -E "^[0-9a-f]+ [0-9]+ 2" blame_raw | sed -e "s/ .*//" >actual &&
	git rev-parse B >expect &&
	test_cmp expect actual
	'

# Ignore X from the config option, Y from a file.
test_expect_success ignore_revs_from_configs_and_files '
	git config --add blame.ignoreRevsFile ignore_x &&
	git blame --line-porcelain file --ignore-revs-file ignore_y >blame_raw &&

	grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
	git rev-parse A >expect &&
	test_cmp expect actual &&

	grep -E "^[0-9a-f]+ [0-9]+ 2" blame_raw | sed -e "s/ .*//" >actual &&
	git rev-parse B >expect &&
	test_cmp expect actual
	'

# Override blame.ignoreRevsFile (ignore_x) with an empty string.  X should be
# blamed now for lines 1 and 2, since we are no longer ignoring X.
test_expect_success override_ignore_revs_file '
	git blame --line-porcelain file --ignore-revs-file "" --ignore-revs-file ignore_y >blame_raw &&
	git rev-parse X >expect &&

	grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
	test_cmp expect actual &&

	grep -E "^[0-9a-f]+ [0-9]+ 2" blame_raw | sed -e "s/ .*//" >actual &&
	test_cmp expect actual
	'
test_expect_success bad_files_and_revs '
	test_must_fail git blame file --ignore-rev NOREV 2>err &&
	test_i18ngrep "cannot find revision NOREV to ignore" err &&

	test_must_fail git blame file --ignore-revs-file NOFILE 2>err &&
	test_i18ngrep "could not open.*: NOFILE" err &&

	echo NOREV >ignore_norev &&
	test_must_fail git blame file --ignore-revs-file ignore_norev 2>err &&
	test_i18ngrep "invalid object name: NOREV" err
	'

# For ignored revs that have added 'unblamable' lines, mark those lines with a
# '*'
# 	A--B--X--Y
# Lines 3 and 4 are from Y and unblamable.  This was set up in
# ignore_rev_adding_unblamable_lines.
test_expect_success mark_unblamable_lines '
	git config --add blame.markUnblamableLines true &&

	git blame --ignore-rev Y file >blame_raw &&
	echo "*" >expect &&

	sed -n "3p" blame_raw | cut -c1 >actual &&
	test_cmp expect actual &&

	sed -n "4p" blame_raw | cut -c1 >actual &&
	test_cmp expect actual
	'

# Commit Z will touch the first two lines.  Y touched all four.
# 	A--B--X--Y--Z
# The blame output when ignoring Z should be:
# ?Y ... 1)
# ?Y ... 2)
# Y  ... 3)
# Y  ... 4)
# We're checking only the first character
test_expect_success mark_ignored_lines '
	git config --add blame.markIgnoredLines true &&

	test_write_lines line-one-Z line-two-Z y3 y4 >file &&
	git add file &&
	test_tick &&
	git commit -m Z &&
	git tag Z &&

	git blame --ignore-rev Z file >blame_raw &&
	echo "?" >expect &&

	sed -n "1p" blame_raw | cut -c1 >actual &&
	test_cmp expect actual &&

	sed -n "2p" blame_raw | cut -c1 >actual &&
	test_cmp expect actual &&

	sed -n "3p" blame_raw | cut -c1 >actual &&
	! test_cmp expect actual &&

	sed -n "4p" blame_raw | cut -c1 >actual &&
	! test_cmp expect actual
	'

# For ignored revs that added 'unblamable' lines and more recent commits changed
# the blamable lines, mark the unblamable lines with a
# '*'
# 	A--B--X--Y--Z
# Lines 3 and 4 are from Y and unblamable, as set up in
# ignore_rev_adding_unblamable_lines.  Z changed lines 1 and 2.
test_expect_success mark_unblamable_lines_intermediate '
	git config --add blame.markUnblamableLines true &&

	git blame --ignore-rev Y file >blame_raw 2>stderr &&
	echo "*" >expect &&

	sed -n "3p" blame_raw | cut -c1 >actual &&
	test_cmp expect actual &&

	sed -n "4p" blame_raw | cut -c1 >actual &&
	test_cmp expect actual
	'

# The heuristic called by guess_line_blames() tries to find the size of a
# blame_entry 'e' in the parent's address space.  Those calculations need to
# check for negative or zero values for when a blame entry is completely outside
# the window of the parent's version of a file.
#
# This happens when one commit adds several lines (commit B below).  A later
# commit (C) changes one line in the middle of B's change.  Commit C gets blamed
# for its change, and that breaks up B's change into multiple blame entries.
# When processing B, one of the blame_entries is outside A's window (which was
# zero - it had no lines added on its side of the diff).
#
# A--B--C, ignore B to test the ignore heuristic's boundary checks.
test_expect_success ignored_chunk_negative_parent_size '
	rm -rf .git/ &&
	git init &&

	test_write_lines L1 L2 L7 L8 L9 >file &&
	git add file &&
	test_tick &&
	git commit -m A &&
	git tag A &&

	test_write_lines L1 L2 L3 L4 L5 L6 L7 L8 L9 >file &&
	git add file &&
	test_tick &&
	git commit -m B &&
	git tag B &&

	test_write_lines L1 L2 L3 L4 xxx L6 L7 L8 L9 >file &&
	git add file &&
	test_tick &&
	git commit -m C &&
	git tag C &&

	git blame file --ignore-rev B >blame_raw
	'

# Resetting the repo and creating:
#
# A--B--M
#  \   /
#   C-+
#
# 'A' creates a file.  B changes line 1, and C changes line 9.  M merges.
test_expect_success ignore_merge '
	rm -rf .git/ &&
	git init &&

	test_write_lines L1 L2 L3 L4 L5 L6 L7 L8 L9 >file &&
	git add file &&
	test_tick &&
	git commit -m A &&
	git tag A &&

	test_write_lines BB L2 L3 L4 L5 L6 L7 L8 L9 >file &&
	git add file &&
	test_tick &&
	git commit -m B &&
	git tag B &&

	git reset --hard A &&
	test_write_lines L1 L2 L3 L4 L5 L6 L7 L8 CC >file &&
	git add file &&
	test_tick &&
	git commit -m C &&
	git tag C &&

	test_merge M B &&
	git blame --line-porcelain file --ignore-rev M >blame_raw &&

	grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
	git rev-parse B >expect &&
	test_cmp expect actual &&

	grep -E "^[0-9a-f]+ [0-9]+ 9" blame_raw | sed -e "s/ .*//" >actual &&
	git rev-parse C >expect &&
	test_cmp expect actual
	'

test_done