Raw
1 #!/bin/sh
2
3 test_description='git blame with specific diff algorithm'
4
5 . ./test-lib.sh
6
7 test_expect_success setup '
8 cat >file.c <<-\EOF &&
9 int f(int x, int y)
10 {
11 if (x == 0)
12 {
13 return y;
14 }
15 return x;
16 }
17
18 int g(size_t u)
19 {
20 while (u < 30)
21 {
22 u++;
23 }
24 return u;
25 }
26 EOF
27 test_write_lines x x x x >file.txt &&
28 git add file.c file.txt &&
29 GIT_AUTHOR_NAME=Commit_1 git commit -m Commit_1 &&
30
31 cat >file.c <<-\EOF &&
32 int g(size_t u)
33 {
34 while (u < 30)
35 {
36 u++;
37 }
38 return u;
39 }
40
41 int h(int x, int y, int z)
42 {
43 if (z == 0)
44 {
45 return x;
46 }
47 return y;
48 }
49 EOF
50 test_write_lines x x x A B C D x E F G >file.txt &&
51 git add file.c file.txt &&
52 GIT_AUTHOR_NAME=Commit_2 git commit -m Commit_2
53 '
54
55 test_expect_success 'blame uses Myers diff algorithm by default' '
56 cat >expected <<-\EOF &&
57 Commit_2 int g(size_t u)
58 Commit_1 {
59 Commit_2 while (u < 30)
60 Commit_1 {
61 Commit_2 u++;
62 Commit_1 }
63 Commit_2 return u;
64 Commit_1 }
65 Commit_1
66 Commit_2 int h(int x, int y, int z)
67 Commit_1 {
68 Commit_2 if (z == 0)
69 Commit_1 {
70 Commit_2 return x;
71 Commit_1 }
72 Commit_2 return y;
73 Commit_1 }
74 EOF
75
76 git blame file.c >output &&
77 sed -e "s/^[^ ]* (\([^ ]*\) [^)]*)/\1/g" output >without_varying_parts &&
78 sed -e "s/ *$//g" without_varying_parts >actual &&
79 test_cmp expected actual
80 '
81
82 test_expect_success 'blame honors --diff-algorithm option' '
83 cat >expected <<-\EOF &&
84 Commit_1 int g(size_t u)
85 Commit_1 {
86 Commit_1 while (u < 30)
87 Commit_1 {
88 Commit_1 u++;
89 Commit_1 }
90 Commit_1 return u;
91 Commit_1 }
92 Commit_2
93 Commit_2 int h(int x, int y, int z)
94 Commit_2 {
95 Commit_2 if (z == 0)
96 Commit_2 {
97 Commit_2 return x;
98 Commit_2 }
99 Commit_2 return y;
100 Commit_2 }
101 EOF
102
103 git blame file.c --diff-algorithm histogram >output &&
104 sed -e "s/^[^ ]* (\([^ ]*\) [^)]*)/\1/g" output >without_varying_parts &&
105 sed -e "s/ *$//g" without_varying_parts >actual &&
106 test_cmp expected actual
107 '
108
109 test_expect_success 'blame honors diff.algorithm config variable' '
110 cat >expected <<-\EOF &&
111 Commit_1 int g(size_t u)
112 Commit_1 {
113 Commit_1 while (u < 30)
114 Commit_1 {
115 Commit_1 u++;
116 Commit_1 }
117 Commit_1 return u;
118 Commit_1 }
119 Commit_2
120 Commit_2 int h(int x, int y, int z)
121 Commit_2 {
122 Commit_2 if (z == 0)
123 Commit_2 {
124 Commit_2 return x;
125 Commit_2 }
126 Commit_2 return y;
127 Commit_2 }
128 EOF
129
130 git -c diff.algorithm=histogram blame file.c >output &&
131 sed -e "s/^[^ ]* (\([^ ]*\) [^)]*)/\1/g" \
132 -e "s/ *$//g" output >actual &&
133 test_cmp expected actual
134 '
135
136 test_expect_success 'blame gives priority to --diff-algorithm over diff.algorithm' '
137 cat >expected <<-\EOF &&
138 Commit_1 int g(size_t u)
139 Commit_1 {
140 Commit_1 while (u < 30)
141 Commit_1 {
142 Commit_1 u++;
143 Commit_1 }
144 Commit_1 return u;
145 Commit_1 }
146 Commit_2
147 Commit_2 int h(int x, int y, int z)
148 Commit_2 {
149 Commit_2 if (z == 0)
150 Commit_2 {
151 Commit_2 return x;
152 Commit_2 }
153 Commit_2 return y;
154 Commit_2 }
155 EOF
156
157 git -c diff.algorithm=myers blame file.c --diff-algorithm histogram >output &&
158 sed -e "s/^[^ ]* (\([^ ]*\) [^)]*)/\1/g" \
159 -e "s/ *$//g" output >actual &&
160 test_cmp expected actual
161 '
162
163 test_expect_success 'blame honors --minimal option' '
164 cat >expected <<-\EOF &&
165 Commit_1 x
166 Commit_1 x
167 Commit_1 x
168 Commit_2 A
169 Commit_2 B
170 Commit_2 C
171 Commit_2 D
172 Commit_1 x
173 Commit_2 E
174 Commit_2 F
175 Commit_2 G
176 EOF
177
178 git blame file.txt --minimal >output &&
179 sed -e "s/^[^ ]* (\([^ ]*\) [^)]*)/\1/g" output >actual &&
180 test_cmp expected actual
181 '
182
183 test_expect_success 'blame respects the order of diff options' '
184 cat >expected <<-\EOF &&
185 Commit_1 x
186 Commit_1 x
187 Commit_1 x
188 Commit_2 A
189 Commit_2 B
190 Commit_2 C
191 Commit_2 D
192 Commit_2 x
193 Commit_2 E
194 Commit_2 F
195 Commit_2 G
196 EOF
197
198 git blame file.txt --minimal --diff-algorithm myers >output &&
199 sed -e "s/^[^ ]* (\([^ ]*\) [^)]*)/\1/g" output >actual &&
200 test_cmp expected actual
201 '
202
203 test_done