format-patch: ensure that --rfc and -k are mutually exclusive
[alt-git.git] / t / t4051-diff-function-context.sh
blob725278ad19c720468113659dbcc63aa013ac7de0
1 #!/bin/sh
3 test_description='diff function context'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 dir="$TEST_DIRECTORY/t4051"
10 commit_and_tag () {
11 tag=$1 &&
12 shift &&
13 git add "$@" &&
14 test_tick &&
15 git commit -m "$tag" &&
16 git tag "$tag"
19 first_context_line () {
20 awk '
21 found {print; exit}
22 /^@@/ {found = 1}
26 last_context_line () {
27 sed -ne \$p
30 check_diff () {
31 name=$1
32 desc=$2
33 options="-W $3"
35 test_expect_success "$desc" '
36 git diff $options "$name^" "$name" >"$name.diff"
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"
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 &&
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 &&
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 &&
60 cat "$dir/appended1.c" >>file.c &&
61 commit_and_tag appended file.c &&
63 cat "$dir/appended2.c" >>file.c &&
64 commit_and_tag extended file.c &&
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 &&
70 git checkout initial &&
71 cat "$dir/hello.c" "$dir/dummy.c" >file.c &&
72 commit_and_tag hello_dummy file.c &&
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 &&
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
87 check_diff changed_hello 'changed function'
89 test_expect_success ' context includes comment' '
90 grep "^ .*Hello comment" changed_hello.diff
93 test_expect_success ' context includes begin' '
94 grep "^ .*Begin of hello" changed_hello.diff
97 test_expect_success ' context includes end' '
98 grep "^ .*End of hello" changed_hello.diff
101 test_expect_success ' context does not include other functions' '
102 test $(grep -c "^[ +-].*Begin" changed_hello.diff) -le 1
105 test_expect_success ' context does not include preceding empty lines' '
106 test "$(first_context_line <changed_hello.diff)" != " "
109 test_expect_success ' context does not include trailing empty lines' '
110 test "$(last_context_line <changed_hello.diff)" != " "
113 check_diff changed_includes 'changed includes'
115 test_expect_success ' context includes begin' '
116 grep "^ .*Begin.h" changed_includes.diff
119 test_expect_success ' context includes end' '
120 grep "^ .*End.h" changed_includes.diff
123 test_expect_success ' context does not include other functions' '
124 test $(grep -c "^[ +-].*Begin" changed_includes.diff) -le 1
127 test_expect_success ' context does not include trailing empty lines' '
128 test "$(last_context_line <changed_includes.diff)" != " "
131 check_diff appended 'appended function'
133 test_expect_success ' context includes begin' '
134 grep "^[+].*Begin of first part" appended.diff
137 test_expect_success ' context includes end' '
138 grep "^[+].*End of first part" appended.diff
141 test_expect_success ' context does not include other functions' '
142 test $(grep -c "^[ +-].*Begin" appended.diff) -le 1
145 check_diff extended 'appended function part'
147 test_expect_success ' context includes begin' '
148 grep "^ .*Begin of first part" extended.diff
151 test_expect_success ' context includes end' '
152 grep "^[+].*End of second part" extended.diff
155 test_expect_success ' context does not include other functions' '
156 test $(grep -c "^[ +-].*Begin" extended.diff) -le 2
159 test_expect_success ' context does not include preceding empty lines' '
160 test "$(first_context_line <extended.diff)" != " "
163 check_diff long_common_tail 'change with long common tail and no context' -U0
165 test_expect_success ' context includes begin' '
166 grep "^ .*Begin of first part" long_common_tail.diff
169 test_expect_success ' context includes end' '
170 grep "^ .*End of second part" long_common_tail.diff
173 test_expect_success ' context does not include other functions' '
174 test $(grep -c "^[ +-].*Begin" long_common_tail.diff) -le 2
177 test_expect_success ' context does not include preceding empty lines' '
178 test "$(first_context_line <long_common_tail.diff)" != " "
181 check_diff changed_hello_appended 'changed function plus appended function'
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
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
193 test_expect_success ' context does not include other functions' '
194 test $(grep -c "^[ +-].*Begin" changed_hello_appended.diff) -le 2
197 check_diff changed_hello_dummy 'changed two consecutive functions'
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
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
209 test_expect_success ' overlapping hunks are merged' '
210 test $(grep -c "^@@" changed_hello_dummy.diff) -eq 1
213 test_done