Git 2.45
[git/gitster.git] / t / lib-diff-alternative.sh
blobc4dc2d46dc1ef4d3c95bda858d208045ded864c2
1 # Helpers shared by the test scripts for diff algorithms (patience,
2 # histogram, etc).
4 test_diff_frobnitz() {
5 cat >file1 <<\EOF
6 #include <stdio.h>
8 // Frobs foo heartily
9 int frobnitz(int foo)
11 int i;
12 for(i = 0; i < 10; i++)
14 printf("Your answer is: ");
15 printf("%d\n", foo);
19 int fact(int n)
21 if(n > 1)
23 return fact(n-1) * n;
25 return 1;
28 int main(int argc, char **argv)
30 frobnitz(fact(10));
32 EOF
34 cat >file2 <<\EOF
35 #include <stdio.h>
37 int fib(int n)
39 if(n > 2)
41 return fib(n-1) + fib(n-2);
43 return 1;
46 // Frobs foo heartily
47 int frobnitz(int foo)
49 int i;
50 for(i = 0; i < 10; i++)
52 printf("%d\n", foo);
56 int main(int argc, char **argv)
58 frobnitz(fib(10));
60 EOF
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>
72 +int fib(int n)
74 + if(n > 2)
75 + {
76 + return fib(n-1) + fib(n-2);
77 + }
78 + return 1;
81 // Frobs foo heartily
82 int frobnitz(int foo)
84 int i;
85 for(i = 0; i < 10; i++)
87 - printf("Your answer is: ");
88 printf("%d\n", foo);
92 -int fact(int n)
94 - if(n > 1)
95 - {
96 - return fact(n-1) * n;
97 - }
98 - return 1;
101 int main(int argc, char **argv)
103 - frobnitz(fact(10));
104 + frobnitz(fib(10));
108 cat >expect_diffstat <<EOF
109 file1 => file2 | 21 ++++++++++-----------
110 1 file changed, 10 insertions(+), 11 deletions(-)
113 STRATEGY=$1
115 test_expect_success "setup attributes files for tests with $STRATEGY" '
116 git checkout -b master &&
117 echo "file* diff=driver" >.gitattributes &&
118 git add file1 file2 .gitattributes &&
119 git commit -m "adding files" &&
120 git checkout -b branchA &&
121 echo "file* diff=driverA" >.gitattributes &&
122 git add .gitattributes &&
123 git commit -m "adding driverA as diff driver" &&
124 git checkout master &&
125 git clone --bare --no-local . bare.git
128 test_expect_success "$STRATEGY diff from attributes" '
129 test_must_fail git -c diff.driver.algorithm=$STRATEGY diff --no-index file1 file2 > output &&
130 test_cmp expect output
133 test_expect_success "diff from attributes with bare repo with source" '
134 git -C bare.git --attr-source=branchA -c diff.driver.algorithm=myers \
135 -c diff.driverA.algorithm=$STRATEGY \
136 diff HEAD:file1 HEAD:file2 >output &&
137 test_cmp expect output
140 test_expect_success "diff from attributes with bare repo with invalid source" '
141 test_must_fail git -C bare.git --attr-source=invalid-branch diff \
142 HEAD:file1 HEAD:file2
145 test_expect_success "$STRATEGY diff from attributes has valid diffstat" '
146 echo "file* diff=driver" >.gitattributes &&
147 git config diff.driver.algorithm "$STRATEGY" &&
148 test_must_fail git diff --stat --no-index file1 file2 > output &&
149 test_cmp expect_diffstat output
152 test_expect_success "$STRATEGY diff" '
153 test_must_fail git diff --no-index "--diff-algorithm=$STRATEGY" file1 file2 > output &&
154 test_cmp expect output
157 test_expect_success "$STRATEGY diff command line precedence before attributes" '
158 echo "file* diff=driver" >.gitattributes &&
159 git config diff.driver.algorithm myers &&
160 test_must_fail git diff --no-index "--diff-algorithm=$STRATEGY" file1 file2 > output &&
161 test_cmp expect output
164 test_expect_success "$STRATEGY diff attributes precedence before config" '
165 git config diff.algorithm default &&
166 echo "file* diff=driver" >.gitattributes &&
167 git config diff.driver.algorithm "$STRATEGY" &&
168 test_must_fail git diff --no-index file1 file2 > output &&
169 test_cmp expect output
172 test_expect_success "$STRATEGY diff output is valid" '
173 mv file2 expect &&
174 git apply < output &&
175 test_cmp expect file2
179 test_diff_unique() {
180 cat >uniq1 <<\EOF
189 cat >uniq2 <<\EOF
198 uniq1=$(git rev-parse --short $(git hash-object uniq1))
199 uniq2=$(git rev-parse --short $(git hash-object uniq2))
200 cat >expect <<EOF
201 diff --git a/uniq1 b/uniq2
202 index $uniq1..$uniq2 100644
203 --- a/uniq1
204 +++ b/uniq2
205 @@ -1,6 +1,6 @@
220 STRATEGY=$1
222 test_expect_success 'completely different files' '
223 test_must_fail git diff --no-index "--$STRATEGY" uniq1 uniq2 > output &&
224 test_cmp expect output