1#!/bin/sh
2
3test_description='diff function context'
4
5. ./test-lib.sh
6
7dir="$TEST_DIRECTORY/t4051"
8
9commit_and_tag () {
10 tag=$1 &&
11 shift &&
12 git add "$@" &&
13 test_tick &&
14 git commit -m "$tag" &&
15 git tag "$tag"
16}
17
18first_context_line () {
19 awk '
20 found {print; exit}
21 /^@@/ {found = 1}
22 '
23}
24
25last_context_line () {
26 sed -ne \$p
27}
28
29check_diff () {
30 name=$1
31 desc=$2
32 options="-W $3"
33
34 test_expect_success "$desc" '
35 git diff $options "$name^" "$name" >"$name.diff"
36 '
37
38 test_expect_success ' diff applies' '
39 test_when_finished "git reset --hard" &&
40 git checkout --detach "$name^" &&
41 git apply --index "$name.diff" &&
42 git diff --exit-code "$name"
43 '
44}
45
46test_expect_success 'setup' '
47 cat "$dir/includes.c" "$dir/dummy.c" "$dir/dummy.c" "$dir/hello.c" \
48 "$dir/dummy.c" "$dir/dummy.c" >file.c &&
49 commit_and_tag initial file.c &&
50
51 grep -v "delete me from hello" <file.c >file.c.new &&
52 mv file.c.new file.c &&
53 commit_and_tag changed_hello file.c &&
54
55 grep -v "delete me from includes" <file.c >file.c.new &&
56 mv file.c.new file.c &&
57 commit_and_tag changed_includes file.c &&
58
59 cat "$dir/appended1.c" >>file.c &&
60 commit_and_tag appended file.c &&
61
62 cat "$dir/appended2.c" >>file.c &&
63 commit_and_tag extended file.c &&
64
65 grep -v "Begin of second part" <file.c >file.c.new &&
66 mv file.c.new file.c &&
67 commit_and_tag long_common_tail file.c &&
68
69 git checkout initial &&
70 grep -v "delete me from hello" <file.c >file.c.new &&
71 mv file.c.new file.c &&
72 cat "$dir/appended1.c" >>file.c &&
73 commit_and_tag changed_hello_appended file.c
74'
75
76check_diff changed_hello 'changed function'
77
78test_expect_success ' context includes begin' '
79 grep "^ .*Begin of hello" changed_hello.diff
80'
81
82test_expect_success ' context includes end' '
83 grep "^ .*End of hello" changed_hello.diff
84'
85
86test_expect_success ' context does not include other functions' '
87 test $(grep -c "^[ +-].*Begin" changed_hello.diff) -le 1
88'
89
90test_expect_success ' context does not include preceding empty lines' '
91 test "$(first_context_line <changed_hello.diff)" != " "
92'
93
94test_expect_success ' context does not include trailing empty lines' '
95 test "$(last_context_line <changed_hello.diff)" != " "
96'
97
98check_diff changed_includes 'changed includes'
99
100test_expect_success ' context includes begin' '
101 grep "^ .*Begin.h" changed_includes.diff
102'
103
104test_expect_success ' context includes end' '
105 grep "^ .*End.h" changed_includes.diff
106'
107
108test_expect_success ' context does not include other functions' '
109 test $(grep -c "^[ +-].*Begin" changed_includes.diff) -le 1
110'
111
112test_expect_success ' context does not include trailing empty lines' '
113 test "$(last_context_line <changed_includes.diff)" != " "
114'
115
116check_diff appended 'appended function'
117
118test_expect_success ' context includes begin' '
119 grep "^[+].*Begin of first part" appended.diff
120'
121
122test_expect_success ' context includes end' '
123 grep "^[+].*End of first part" appended.diff
124'
125
126test_expect_success ' context does not include other functions' '
127 test $(grep -c "^[ +-].*Begin" appended.diff) -le 1
128'
129
130check_diff extended 'appended function part'
131
132test_expect_success ' context includes begin' '
133 grep "^ .*Begin of first part" extended.diff
134'
135
136test_expect_success ' context includes end' '
137 grep "^[+].*End of second part" extended.diff
138'
139
140test_expect_success ' context does not include other functions' '
141 test $(grep -c "^[ +-].*Begin" extended.diff) -le 2
142'
143
144test_expect_success ' context does not include preceding empty lines' '
145 test "$(first_context_line <extended.diff)" != " "
146'
147
148check_diff long_common_tail 'change with long common tail and no context' -U0
149
150test_expect_success ' context includes begin' '
151 grep "^ .*Begin of first part" long_common_tail.diff
152'
153
154test_expect_success ' context includes end' '
155 grep "^ .*End of second part" long_common_tail.diff
156'
157
158test_expect_success ' context does not include other functions' '
159 test $(grep -c "^[ +-].*Begin" long_common_tail.diff) -le 2
160'
161
162test_expect_success ' context does not include preceding empty lines' '
163 test "$(first_context_line <long_common_tail.diff.diff)" != " "
164'
165
166check_diff changed_hello_appended 'changed function plus appended function'
167
168test_expect_success ' context includes begin' '
169 grep "^ .*Begin of hello" changed_hello_appended.diff &&
170 grep "^[+].*Begin of first part" changed_hello_appended.diff
171'
172
173test_expect_success ' context includes end' '
174 grep "^ .*End of hello" changed_hello_appended.diff &&
175 grep "^[+].*End of first part" changed_hello_appended.diff
176'
177
178test_expect_success ' context does not include other functions' '
179 test $(grep -c "^[ +-].*Begin" changed_hello_appended.diff) -le 2
180'
181
182test_done