]> git.ipfire.org Git - thirdparty/git.git/blob - t/t4051-diff-function-context.sh
Sync with Git 2.45.1
[thirdparty/git.git] / t / t4051-diff-function-context.sh
1 #!/bin/sh
2
3 test_description='diff function context'
4
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 dir="$TEST_DIRECTORY/t4051"
9
10 commit_and_tag () {
11 tag=$1 &&
12 shift &&
13 git add "$@" &&
14 test_tick &&
15 git commit -m "$tag" &&
16 git tag "$tag"
17 }
18
19 first_context_line () {
20 awk '
21 found {print; exit}
22 /^@@/ {found = 1}
23 '
24 }
25
26 last_context_line () {
27 sed -ne \$p
28 }
29
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 '
45 }
46
47 test_expect_success 'setup' '
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 &&
68 commit_and_tag long_common_tail file.c &&
69
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 &&
76 sed "2a\\
77 extra line" <"$dir/dummy.c" >>file.c &&
78 commit_and_tag changed_hello_dummy file.c &&
79
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
85 '
86
87 check_diff changed_hello 'changed function'
88
89 test_expect_success ' context includes comment' '
90 grep "^ .*Hello comment" changed_hello.diff
91 '
92
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
109 test_expect_success ' context does not include trailing empty lines' '
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
127 test_expect_success ' context does not include trailing empty lines' '
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
141 test_expect_success ' context does not include other functions' '
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
155 test_expect_success ' context does not include other functions' '
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
169 test_expect_success ' context includes end' '
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' '
178 test "$(first_context_line <long_common_tail.diff)" != " "
179 '
180
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
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
213 test_done