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