]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4211-line-log.sh
The third batch
[thirdparty/git.git] / t / t4211-line-log.sh
CommitLineData
12da1d1f
TR
1#!/bin/sh
2
3test_description='test log -L'
8f37854b 4GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
5export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
12da1d1f
TR
7. ./test-lib.sh
8
9test_expect_success 'setup (import history)' '
10 git fast-import < "$TEST_DIRECTORY"/t4211/history.export &&
11 git reset --hard
12'
13
39664cb0
JH
14test_expect_success 'basic command line parsing' '
15 # This may fail due to "no such path a.c in commit", or
16 # "-L is incompatible with pathspec", depending on the
17 # order the error is checked. Either is acceptable.
18 test_must_fail git log -L1,1:a.c -- a.c &&
19
20 # -L requires there is no pathspec
21 test_must_fail git log -L1,1:b.c -- b.c 2>error &&
6789275d 22 test_grep "cannot be used with pathspec" error &&
39664cb0
JH
23
24 # This would fail because --follow wants a single path, but
25 # we may fail due to incompatibility between -L/--follow in
26 # the future. Either is acceptable.
27 test_must_fail git log -L1,1:b.c --follow &&
28 test_must_fail git log --follow -L1,1:b.c &&
29
30 # This would fail because -L wants no pathspec, but
31 # we may fail due to incompatibility between -L/--follow in
32 # the future. Either is acceptable.
33 test_must_fail git log --follow -L1,1:b.c -- b.c
34'
35
d51c5274
TR
36canned_test_1 () {
37 test_expect_$1 "$2" "
38 git log $2 >actual &&
dfa5f53e 39 test_cmp \"\$TEST_DIRECTORY\"/t4211/$(test_oid algo)/expect.$3 actual
12da1d1f
TR
40 "
41}
42
d51c5274
TR
43canned_test () {
44 canned_test_1 success "$@"
45}
46canned_test_failure () {
47 canned_test_1 failure "$@"
48}
49
12da1d1f
TR
50test_bad_opts () {
51 test_expect_success "invalid args: $1" "
52 test_must_fail git log $1 2>errors &&
6789275d 53 test_grep '$2' errors
12da1d1f
TR
54 "
55}
56
57canned_test "-L 4,12:a.c simple" simple-f
58canned_test "-L 4,+9:a.c simple" simple-f
59canned_test "-L '/long f/,/^}/:a.c' simple" simple-f
13b8f68c 60canned_test "-L :f:a.c simple" simple-f-to-main
12da1d1f
TR
61
62canned_test "-L '/main/,/^}/:a.c' simple" simple-main
13b8f68c 63canned_test "-L :main:a.c simple" simple-main-to-end
12da1d1f
TR
64
65canned_test "-L 1,+4:a.c simple" beginning-of-file
66
67canned_test "-L 20:a.c simple" end-of-file
68
69canned_test "-L '/long f/',/^}/:a.c -L /main/,/^}/:a.c simple" two-ranges
70canned_test "-L 24,+1:a.c simple" vanishes-early
71
035ff398 72canned_test "-M -L '/long f/,/^}/:b.c' move-support" move-support-f
31c61918 73canned_test "-M -L ':f:b.c' parallel-change" parallel-change-f-to-main
12da1d1f 74
20961886 75canned_test "-L 4,12:a.c -L :main:a.c simple" multiple
215e76c7 76canned_test "-L 4,18:a.c -L ^:main:a.c simple" multiple-overlapping
20961886 77canned_test "-L :main:a.c -L 4,18:a.c simple" multiple-overlapping
3755b53a
ES
78canned_test "-L 4:a.c -L 8,12:a.c simple" multiple-superset
79canned_test "-L 8,12:a.c -L 4:a.c simple" multiple-superset
20961886 80
12da1d1f 81test_bad_opts "-L" "switch.*requires a value"
0269f968
MM
82test_bad_opts "-L b.c" "argument not .start,end:file"
83test_bad_opts "-L 1:" "argument not .start,end:file"
12da1d1f
TR
84test_bad_opts "-L 1:nonexistent" "There is no path"
85test_bad_opts "-L 1:simple" "There is no path"
0269f968 86test_bad_opts "-L '/foo:b.c'" "argument not .start,end:file"
12da1d1f 87test_bad_opts "-L 1000:b.c" "has only.*lines"
0269f968 88test_bad_opts "-L :b.c" "argument not .start,end:file"
13b8f68c 89test_bad_opts "-L :foo:b.c" "no match"
12da1d1f 90
25fb8ee4
ES
91test_expect_success '-L X (X == nlines)' '
92 n=$(wc -l <b.c) &&
93 git log -L $n:b.c
94'
95
63828b84 96test_expect_success '-L X (X == nlines + 1)' '
25fb8ee4
ES
97 n=$(expr $(wc -l <b.c) + 1) &&
98 test_must_fail git log -L $n:b.c
99'
100
101test_expect_success '-L X (X == nlines + 2)' '
102 n=$(expr $(wc -l <b.c) + 2) &&
103 test_must_fail git log -L $n:b.c
104'
105
106test_expect_success '-L ,Y (Y == nlines)' '
107 n=$(printf "%d" $(wc -l <b.c)) &&
108 git log -L ,$n:b.c
109'
110
111test_expect_success '-L ,Y (Y == nlines + 1)' '
112 n=$(expr $(wc -l <b.c) + 1) &&
7f81c00f 113 git log -L ,$n:b.c
25fb8ee4
ES
114'
115
116test_expect_success '-L ,Y (Y == nlines + 2)' '
117 n=$(expr $(wc -l <b.c) + 2) &&
7f81c00f 118 git log -L ,$n:b.c
25fb8ee4
ES
119'
120
a8787c5c
TM
121test_expect_success '-L with --first-parent and a merge' '
122 git checkout parallel-change &&
123 git log --first-parent -L 1,1:b.c
124'
125
c1496934
JS
126test_expect_success '-L with --output' '
127 git checkout parallel-change &&
128 git log --output=log -L :main:b.c >output &&
f0dc593a 129 test_must_be_empty output &&
c1496934
JS
130 test_line_count = 70 log
131'
132
aaae0bf7
AX
133test_expect_success 'range_set_union' '
134 test_seq 500 > c.c &&
135 git add c.c &&
136 git commit -m "many lines" &&
137 test_seq 1000 > c.c &&
138 git add c.c &&
139 git commit -m "modify many lines" &&
cbe1d9d6 140 git log $(for x in $(test_seq 200); do echo -L $((2*x)),+1:c.c || return 1; done)
aaae0bf7
AX
141'
142
9f607cd0
JK
143test_expect_success '-s shows only line-log commits' '
144 git log --format="commit %s" -L1,24:b.c >expect.raw &&
145 grep ^commit expect.raw >expect &&
146 git log --format="commit %s" -L1,24:b.c -s >actual &&
147 test_cmp expect actual
148'
149
05314efa
JK
150test_expect_success '-p shows the default patch output' '
151 git log -L1,24:b.c >expect &&
152 git log -L1,24:b.c -p >actual &&
153 test_cmp expect actual
154'
155
156test_expect_success '--raw is forbidden' '
157 test_must_fail git log -L1,24:b.c --raw
158'
159
a2bb801f
SG
160test_expect_success 'setup for checking fancy rename following' '
161 git checkout --orphan moves-start &&
162 git reset --hard &&
163
164 printf "%s\n" 12 13 14 15 b c d e >file-1 &&
165 printf "%s\n" 22 23 24 25 B C D E >file-2 &&
166 git add file-1 file-2 &&
167 test_tick &&
168 git commit -m "Add file-1 and file-2" &&
169 oid_add_f1_f2=$(git rev-parse --short HEAD) &&
170
171 git checkout -b moves-main &&
172 printf "%s\n" 11 12 13 14 15 b c d e >file-1 &&
173 git commit -a -m "Modify file-1 on main" &&
174 oid_mod_f1_main=$(git rev-parse --short HEAD) &&
175
176 printf "%s\n" 21 22 23 24 25 B C D E >file-2 &&
177 git commit -a -m "Modify file-2 on main #1" &&
178 oid_mod_f2_main_1=$(git rev-parse --short HEAD) &&
179
180 git mv file-1 renamed-1 &&
181 git commit -m "Rename file-1 to renamed-1 on main" &&
182
183 printf "%s\n" 11 12 13 14 15 b c d e f >renamed-1 &&
184 git commit -a -m "Modify renamed-1 on main" &&
185 oid_mod_r1_main=$(git rev-parse --short HEAD) &&
186
187 printf "%s\n" 21 22 23 24 25 B C D E F >file-2 &&
188 git commit -a -m "Modify file-2 on main #2" &&
189 oid_mod_f2_main_2=$(git rev-parse --short HEAD) &&
190
191 git checkout -b moves-side moves-start &&
192 printf "%s\n" 12 13 14 15 16 b c d e >file-1 &&
193 git commit -a -m "Modify file-1 on side #1" &&
194 oid_mod_f1_side_1=$(git rev-parse --short HEAD) &&
195
196 printf "%s\n" 22 23 24 25 26 B C D E >file-2 &&
197 git commit -a -m "Modify file-2 on side" &&
198 oid_mod_f2_side=$(git rev-parse --short HEAD) &&
199
200 git mv file-2 renamed-2 &&
201 git commit -m "Rename file-2 to renamed-2 on side" &&
202
203 printf "%s\n" 12 13 14 15 16 a b c d e >file-1 &&
204 git commit -a -m "Modify file-1 on side #2" &&
205 oid_mod_f1_side_2=$(git rev-parse --short HEAD) &&
206
207 printf "%s\n" 22 23 24 25 26 A B C D E >renamed-2 &&
208 git commit -a -m "Modify renamed-2 on side" &&
209 oid_mod_r2_side=$(git rev-parse --short HEAD) &&
210
211 git checkout moves-main &&
212 git merge moves-side &&
213 oid_merge=$(git rev-parse --short HEAD)
214'
215
216test_expect_success 'fancy rename following #1' '
217 cat >expect <<-EOF &&
218 $oid_merge Merge branch '\''moves-side'\'' into moves-main
219 $oid_mod_f1_side_2 Modify file-1 on side #2
220 $oid_mod_f1_side_1 Modify file-1 on side #1
221 $oid_mod_r1_main Modify renamed-1 on main
222 $oid_mod_f1_main Modify file-1 on main
223 $oid_add_f1_f2 Add file-1 and file-2
224 EOF
225 git log -L1:renamed-1 --oneline --no-patch >actual &&
226 test_cmp expect actual
227'
228
229test_expect_success 'fancy rename following #2' '
230 cat >expect <<-EOF &&
231 $oid_merge Merge branch '\''moves-side'\'' into moves-main
232 $oid_mod_r2_side Modify renamed-2 on side
233 $oid_mod_f2_side Modify file-2 on side
234 $oid_mod_f2_main_2 Modify file-2 on main #2
235 $oid_mod_f2_main_1 Modify file-2 on main #1
236 $oid_add_f1_f2 Add file-1 and file-2
237 EOF
238 git log -L1:renamed-2 --oneline --no-patch >actual &&
239 test_cmp expect actual
240'
241
48da94ba
SG
242# Create the following linear history, where each commit does what its
243# subject line promises:
244#
245# * 66c6410 Modify func2() in file.c
246# * 50834e5 Modify other-file
247# * fe5851c Modify func1() in file.c
248# * 8c7c7dd Add other-file
249# * d5f4417 Add func1() and func2() in file.c
250test_expect_success 'setup for checking line-log and parent oids' '
251 git checkout --orphan parent-oids &&
252 git reset --hard &&
253
254 cat >file.c <<-\EOF &&
255 int func1()
256 {
257 return F1;
258 }
259
260 int func2()
261 {
262 return F2;
263 }
264 EOF
265 git add file.c &&
266 test_tick &&
01faa91c 267 first_tick=$test_tick &&
48da94ba
SG
268 git commit -m "Add func1() and func2() in file.c" &&
269
270 echo 1 >other-file &&
271 git add other-file &&
01faa91c 272 test_tick &&
48da94ba
SG
273 git commit -m "Add other-file" &&
274
275 sed -e "s/F1/F1 + 1/" file.c >tmp &&
276 mv tmp file.c &&
277 git commit -a -m "Modify func1() in file.c" &&
278
279 echo 2 >other-file &&
280 git commit -a -m "Modify other-file" &&
281
282 sed -e "s/F2/F2 + 2/" file.c >tmp &&
283 mv tmp file.c &&
284 git commit -a -m "Modify func2() in file.c" &&
285
286 head_oid=$(git rev-parse --short HEAD) &&
287 prev_oid=$(git rev-parse --short HEAD^) &&
288 root_oid=$(git rev-parse --short HEAD~4)
289'
290
291# Parent oid should be from immediate parent.
3cb9d2b6 292test_expect_success 'parent oids without parent rewriting' '
48da94ba
SG
293 cat >expect <<-EOF &&
294 $head_oid $prev_oid Modify func2() in file.c
295 $root_oid Add func1() and func2() in file.c
296 EOF
297 git log --format="%h %p %s" --no-patch -L:func2:file.c >actual &&
298 test_cmp expect actual
299'
300
301# Parent oid should be from the most recent ancestor touching func2(),
302# i.e. in this case from the root commit.
303test_expect_success 'parent oids with parent rewriting' '
304 cat >expect <<-EOF &&
305 $head_oid $root_oid Modify func2() in file.c
306 $root_oid Add func1() and func2() in file.c
307 EOF
308 git log --format="%h %p %s" --no-patch -L:func2:file.c --parents >actual &&
309 test_cmp expect actual
310'
311
01faa91c
RS
312test_expect_success 'line-log with --before' '
313 echo $root_oid >expect &&
314 git log --format=%h --no-patch -L:func2:file.c --before=$first_tick >actual &&
315 test_cmp expect actual
316'
317
4e57c88e
LKS
318test_expect_success 'setup tests for zero-width regular expressions' '
319 cat >expect <<-EOF
320 Modify func1() in file.c
321 Add func1() and func2() in file.c
322 EOF
323'
324
325test_expect_success 'zero-width regex $ matches any function name' '
326 git log --format="%s" --no-patch "-L:$:file.c" >actual &&
327 test_cmp expect actual
328'
329
330test_expect_success 'zero-width regex ^ matches any function name' '
331 git log --format="%s" --no-patch "-L:^:file.c" >actual &&
332 test_cmp expect actual
333'
334
335test_expect_success 'zero-width regex .* matches any function name' '
336 git log --format="%s" --no-patch "-L:.*:file.c" >actual &&
337 test_cmp expect actual
338'
339
12da1d1f 340test_done