]> git.ipfire.org Git - thirdparty/git.git/blob - t/lib-diff-alternative.sh
Merge branch 'js/t5563-portability-fix'
[thirdparty/git.git] / t / lib-diff-alternative.sh
1 # Helpers shared by the test scripts for diff algorithms (patience,
2 # histogram, etc).
3
4 test_diff_frobnitz() {
5 cat >file1 <<\EOF
6 #include <stdio.h>
7
8 // Frobs foo heartily
9 int frobnitz(int foo)
10 {
11 int i;
12 for(i = 0; i < 10; i++)
13 {
14 printf("Your answer is: ");
15 printf("%d\n", foo);
16 }
17 }
18
19 int fact(int n)
20 {
21 if(n > 1)
22 {
23 return fact(n-1) * n;
24 }
25 return 1;
26 }
27
28 int main(int argc, char **argv)
29 {
30 frobnitz(fact(10));
31 }
32 EOF
33
34 cat >file2 <<\EOF
35 #include <stdio.h>
36
37 int fib(int n)
38 {
39 if(n > 2)
40 {
41 return fib(n-1) + fib(n-2);
42 }
43 return 1;
44 }
45
46 // Frobs foo heartily
47 int frobnitz(int foo)
48 {
49 int i;
50 for(i = 0; i < 10; i++)
51 {
52 printf("%d\n", foo);
53 }
54 }
55
56 int main(int argc, char **argv)
57 {
58 frobnitz(fib(10));
59 }
60 EOF
61
62 file1=$(git rev-parse --short $(git hash-object file1))
63 file2=$(git rev-parse --short $(git hash-object file2))
64 cat >expect <<EOF
65 diff --git a/file1 b/file2
66 index $file1..$file2 100644
67 --- a/file1
68 +++ b/file2
69 @@ -1,26 +1,25 @@
70 #include <stdio.h>
71
72 +int fib(int n)
73 +{
74 + if(n > 2)
75 + {
76 + return fib(n-1) + fib(n-2);
77 + }
78 + return 1;
79 +}
80 +
81 // Frobs foo heartily
82 int frobnitz(int foo)
83 {
84 int i;
85 for(i = 0; i < 10; i++)
86 {
87 - printf("Your answer is: ");
88 printf("%d\n", foo);
89 }
90 }
91
92 -int fact(int n)
93 -{
94 - if(n > 1)
95 - {
96 - return fact(n-1) * n;
97 - }
98 - return 1;
99 -}
100 -
101 int main(int argc, char **argv)
102 {
103 - frobnitz(fact(10));
104 + frobnitz(fib(10));
105 }
106 EOF
107
108 cat >expect_diffstat <<EOF
109 file1 => file2 | 21 ++++++++++-----------
110 1 file changed, 10 insertions(+), 11 deletions(-)
111 EOF
112
113 STRATEGY=$1
114
115 test_expect_success "$STRATEGY diff from attributes" '
116 echo "file* diff=driver" >.gitattributes &&
117 git config diff.driver.algorithm "$STRATEGY" &&
118 test_must_fail git diff --no-index file1 file2 > output &&
119 cat expect &&
120 cat output &&
121 test_cmp expect output
122 '
123
124 test_expect_success "$STRATEGY diff from attributes has valid diffstat" '
125 echo "file* diff=driver" >.gitattributes &&
126 git config diff.driver.algorithm "$STRATEGY" &&
127 test_must_fail git diff --stat --no-index file1 file2 > output &&
128 test_cmp expect_diffstat output
129 '
130
131 test_expect_success "$STRATEGY diff" '
132 test_must_fail git diff --no-index "--diff-algorithm=$STRATEGY" file1 file2 > output &&
133 test_cmp expect output
134 '
135
136 test_expect_success "$STRATEGY diff command line precedence before attributes" '
137 echo "file* diff=driver" >.gitattributes &&
138 git config diff.driver.algorithm myers &&
139 test_must_fail git diff --no-index "--diff-algorithm=$STRATEGY" file1 file2 > output &&
140 test_cmp expect output
141 '
142
143 test_expect_success "$STRATEGY diff attributes precedence before config" '
144 git config diff.algorithm default &&
145 echo "file* diff=driver" >.gitattributes &&
146 git config diff.driver.algorithm "$STRATEGY" &&
147 test_must_fail git diff --no-index file1 file2 > output &&
148 test_cmp expect output
149 '
150
151 test_expect_success "$STRATEGY diff output is valid" '
152 mv file2 expect &&
153 git apply < output &&
154 test_cmp expect file2
155 '
156 }
157
158 test_diff_unique() {
159 cat >uniq1 <<\EOF
160 1
161 2
162 3
163 4
164 5
165 6
166 EOF
167
168 cat >uniq2 <<\EOF
169 a
170 b
171 c
172 d
173 e
174 f
175 EOF
176
177 uniq1=$(git rev-parse --short $(git hash-object uniq1))
178 uniq2=$(git rev-parse --short $(git hash-object uniq2))
179 cat >expect <<EOF
180 diff --git a/uniq1 b/uniq2
181 index $uniq1..$uniq2 100644
182 --- a/uniq1
183 +++ b/uniq2
184 @@ -1,6 +1,6 @@
185 -1
186 -2
187 -3
188 -4
189 -5
190 -6
191 +a
192 +b
193 +c
194 +d
195 +e
196 +f
197 EOF
198
199 STRATEGY=$1
200
201 test_expect_success 'completely different files' '
202 test_must_fail git diff --no-index "--$STRATEGY" uniq1 uniq2 > output &&
203 test_cmp expect output
204 '
205 }
206