]> git.cameronkatri.com Git - cgit.git/blob - ui-ssdiff.c
git: update to v2.19.1
[cgit.git] / ui-ssdiff.c
1 #include "cgit.h"
2 #include "ui-ssdiff.h"
3 #include "html.h"
4 #include "ui-shared.h"
5 #include "ui-diff.h"
6
7 extern int use_ssdiff;
8
9 static int current_old_line, current_new_line;
10 static int **L = NULL;
11
12 struct deferred_lines {
13 int line_no;
14 char *line;
15 struct deferred_lines *next;
16 };
17
18 static struct deferred_lines *deferred_old, *deferred_old_last;
19 static struct deferred_lines *deferred_new, *deferred_new_last;
20
21 static void create_or_reset_lcs_table(void)
22 {
23 int i;
24
25 if (L != NULL) {
26 memset(*L, 0, sizeof(int) * MAX_SSDIFF_SIZE);
27 return;
28 }
29
30 // xcalloc will die if we ran out of memory;
31 // not very helpful for debugging
32 L = (int**)xcalloc(MAX_SSDIFF_M, sizeof(int *));
33 *L = (int*)xcalloc(MAX_SSDIFF_SIZE, sizeof(int));
34
35 for (i = 1; i < MAX_SSDIFF_M; i++) {
36 L[i] = *L + i * MAX_SSDIFF_N;
37 }
38 }
39
40 static char *longest_common_subsequence(char *A, char *B)
41 {
42 int i, j, ri;
43 int m = strlen(A);
44 int n = strlen(B);
45 int tmp1, tmp2;
46 int lcs_length;
47 char *result;
48
49 // We bail if the lines are too long
50 if (m >= MAX_SSDIFF_M || n >= MAX_SSDIFF_N)
51 return NULL;
52
53 create_or_reset_lcs_table();
54
55 for (i = m; i >= 0; i--) {
56 for (j = n; j >= 0; j--) {
57 if (A[i] == '\0' || B[j] == '\0') {
58 L[i][j] = 0;
59 } else if (A[i] == B[j]) {
60 L[i][j] = 1 + L[i + 1][j + 1];
61 } else {
62 tmp1 = L[i + 1][j];
63 tmp2 = L[i][j + 1];
64 L[i][j] = (tmp1 > tmp2 ? tmp1 : tmp2);
65 }
66 }
67 }
68
69 lcs_length = L[0][0];
70 result = xmalloc(lcs_length + 2);
71 memset(result, 0, sizeof(*result) * (lcs_length + 2));
72
73 ri = 0;
74 i = 0;
75 j = 0;
76 while (i < m && j < n) {
77 if (A[i] == B[j]) {
78 result[ri] = A[i];
79 ri += 1;
80 i += 1;
81 j += 1;
82 } else if (L[i + 1][j] >= L[i][j + 1]) {
83 i += 1;
84 } else {
85 j += 1;
86 }
87 }
88
89 return result;
90 }
91
92 static int line_from_hunk(char *line, char type)
93 {
94 char *buf1, *buf2;
95 int len, res;
96
97 buf1 = strchr(line, type);
98 if (buf1 == NULL)
99 return 0;
100 buf1 += 1;
101 buf2 = strchr(buf1, ',');
102 if (buf2 == NULL)
103 return 0;
104 len = buf2 - buf1;
105 buf2 = xmalloc(len + 1);
106 strlcpy(buf2, buf1, len + 1);
107 res = atoi(buf2);
108 free(buf2);
109 return res;
110 }
111
112 static char *replace_tabs(char *line)
113 {
114 char *prev_buf = line;
115 char *cur_buf;
116 size_t linelen = strlen(line);
117 int n_tabs = 0;
118 int i;
119 char *result;
120 int result_len;
121
122 if (linelen == 0) {
123 result = xmalloc(1);
124 result[0] = '\0';
125 return result;
126 }
127
128 for (i = 0; i < linelen; i++) {
129 if (line[i] == '\t')
130 n_tabs += 1;
131 }
132 result_len = linelen + n_tabs * 8;
133 result = xmalloc(result_len + 1);
134 result[0] = '\0';
135
136 for (;;) {
137 cur_buf = strchr(prev_buf, '\t');
138 if (!cur_buf) {
139 strncat(result, prev_buf, result_len);
140 break;
141 } else {
142 strncat(result, prev_buf, cur_buf - prev_buf);
143 linelen = strlen(result);
144 memset(&result[linelen], ' ', 8 - (linelen % 8));
145 result[linelen + 8 - (linelen % 8)] = '\0';
146 }
147 prev_buf = cur_buf + 1;
148 }
149 return result;
150 }
151
152 static int calc_deferred_lines(struct deferred_lines *start)
153 {
154 struct deferred_lines *item = start;
155 int result = 0;
156 while (item) {
157 result += 1;
158 item = item->next;
159 }
160 return result;
161 }
162
163 static void deferred_old_add(char *line, int line_no)
164 {
165 struct deferred_lines *item = xmalloc(sizeof(struct deferred_lines));
166 item->line = xstrdup(line);
167 item->line_no = line_no;
168 item->next = NULL;
169 if (deferred_old) {
170 deferred_old_last->next = item;
171 deferred_old_last = item;
172 } else {
173 deferred_old = deferred_old_last = item;
174 }
175 }
176
177 static void deferred_new_add(char *line, int line_no)
178 {
179 struct deferred_lines *item = xmalloc(sizeof(struct deferred_lines));
180 item->line = xstrdup(line);
181 item->line_no = line_no;
182 item->next = NULL;
183 if (deferred_new) {
184 deferred_new_last->next = item;
185 deferred_new_last = item;
186 } else {
187 deferred_new = deferred_new_last = item;
188 }
189 }
190
191 static void print_part_with_lcs(char *class, char *line, char *lcs)
192 {
193 int line_len = strlen(line);
194 int i, j;
195 char c[2] = " ";
196 int same = 1;
197
198 j = 0;
199 for (i = 0; i < line_len; i++) {
200 c[0] = line[i];
201 if (same) {
202 if (line[i] == lcs[j])
203 j += 1;
204 else {
205 same = 0;
206 htmlf("<span class='%s'>", class);
207 }
208 } else if (line[i] == lcs[j]) {
209 same = 1;
210 htmlf("</span>");
211 j += 1;
212 }
213 html_txt(c);
214 }
215 }
216
217 static void print_ssdiff_line(char *class,
218 int old_line_no,
219 char *old_line,
220 int new_line_no,
221 char *new_line, int individual_chars)
222 {
223 char *lcs = NULL;
224
225 if (old_line)
226 old_line = replace_tabs(old_line + 1);
227 if (new_line)
228 new_line = replace_tabs(new_line + 1);
229 if (individual_chars && old_line && new_line)
230 lcs = longest_common_subsequence(old_line, new_line);
231 html("<tr>\n");
232 if (old_line_no > 0) {
233 struct diff_filespec *old_file = cgit_get_current_old_file();
234 char *lineno_str = fmt("n%d", old_line_no);
235 char *id_str = fmt("id=%s#%s", is_null_oid(&old_file->oid)?"HEAD":oid_to_hex(old_rev_oid), lineno_str);
236 char *fileurl = cgit_fileurl(ctx.repo->url, "tree", old_file->path, id_str);
237 html("<td class='lineno'><a href='");
238 html(fileurl);
239 htmlf("' id='%s'>%s</a>", lineno_str, lineno_str + 1);
240 html("</td>");
241 htmlf("<td class='%s'>", class);
242 free(fileurl);
243 } else if (old_line)
244 htmlf("<td class='lineno'></td><td class='%s'>", class);
245 else
246 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
247 if (old_line) {
248 if (lcs)
249 print_part_with_lcs("del", old_line, lcs);
250 else
251 html_txt(old_line);
252 }
253
254 html("</td>\n");
255 if (new_line_no > 0) {
256 struct diff_filespec *new_file = cgit_get_current_new_file();
257 char *lineno_str = fmt("n%d", new_line_no);
258 char *id_str = fmt("id=%s#%s", is_null_oid(&new_file->oid)?"HEAD":oid_to_hex(new_rev_oid), lineno_str);
259 char *fileurl = cgit_fileurl(ctx.repo->url, "tree", new_file->path, id_str);
260 html("<td class='lineno'><a href='");
261 html(fileurl);
262 htmlf("' id='%s'>%s</a>", lineno_str, lineno_str + 1);
263 html("</td>");
264 htmlf("<td class='%s'>", class);
265 free(fileurl);
266 } else if (new_line)
267 htmlf("<td class='lineno'></td><td class='%s'>", class);
268 else
269 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
270 if (new_line) {
271 if (lcs)
272 print_part_with_lcs("add", new_line, lcs);
273 else
274 html_txt(new_line);
275 }
276
277 html("</td></tr>");
278 if (lcs)
279 free(lcs);
280 if (new_line)
281 free(new_line);
282 if (old_line)
283 free(old_line);
284 }
285
286 static void print_deferred_old_lines(void)
287 {
288 struct deferred_lines *iter_old, *tmp;
289 iter_old = deferred_old;
290 while (iter_old) {
291 print_ssdiff_line("del", iter_old->line_no,
292 iter_old->line, -1, NULL, 0);
293 tmp = iter_old->next;
294 free(iter_old);
295 iter_old = tmp;
296 }
297 }
298
299 static void print_deferred_new_lines(void)
300 {
301 struct deferred_lines *iter_new, *tmp;
302 iter_new = deferred_new;
303 while (iter_new) {
304 print_ssdiff_line("add", -1, NULL,
305 iter_new->line_no, iter_new->line, 0);
306 tmp = iter_new->next;
307 free(iter_new);
308 iter_new = tmp;
309 }
310 }
311
312 static void print_deferred_changed_lines(void)
313 {
314 struct deferred_lines *iter_old, *iter_new, *tmp;
315 int n_old_lines = calc_deferred_lines(deferred_old);
316 int n_new_lines = calc_deferred_lines(deferred_new);
317 int individual_chars = (n_old_lines == n_new_lines ? 1 : 0);
318
319 iter_old = deferred_old;
320 iter_new = deferred_new;
321 while (iter_old || iter_new) {
322 if (iter_old && iter_new)
323 print_ssdiff_line("changed", iter_old->line_no,
324 iter_old->line,
325 iter_new->line_no, iter_new->line,
326 individual_chars);
327 else if (iter_old)
328 print_ssdiff_line("changed", iter_old->line_no,
329 iter_old->line, -1, NULL, 0);
330 else if (iter_new)
331 print_ssdiff_line("changed", -1, NULL,
332 iter_new->line_no, iter_new->line, 0);
333 if (iter_old) {
334 tmp = iter_old->next;
335 free(iter_old);
336 iter_old = tmp;
337 }
338
339 if (iter_new) {
340 tmp = iter_new->next;
341 free(iter_new);
342 iter_new = tmp;
343 }
344 }
345 }
346
347 void cgit_ssdiff_print_deferred_lines(void)
348 {
349 if (!deferred_old && !deferred_new)
350 return;
351 if (deferred_old && !deferred_new)
352 print_deferred_old_lines();
353 else if (!deferred_old && deferred_new)
354 print_deferred_new_lines();
355 else
356 print_deferred_changed_lines();
357 deferred_old = deferred_old_last = NULL;
358 deferred_new = deferred_new_last = NULL;
359 }
360
361 /*
362 * print a single line returned from xdiff
363 */
364 void cgit_ssdiff_line_cb(char *line, int len)
365 {
366 char c = line[len - 1];
367 line[len - 1] = '\0';
368 if (line[0] == '@') {
369 current_old_line = line_from_hunk(line, '-');
370 current_new_line = line_from_hunk(line, '+');
371 }
372
373 if (line[0] == ' ') {
374 if (deferred_old || deferred_new)
375 cgit_ssdiff_print_deferred_lines();
376 print_ssdiff_line("ctx", current_old_line, line,
377 current_new_line, line, 0);
378 current_old_line += 1;
379 current_new_line += 1;
380 } else if (line[0] == '+') {
381 deferred_new_add(line, current_new_line);
382 current_new_line += 1;
383 } else if (line[0] == '-') {
384 deferred_old_add(line, current_old_line);
385 current_old_line += 1;
386 } else if (line[0] == '@') {
387 html("<tr><td colspan='4' class='hunk'>");
388 html_txt(line);
389 html("</td></tr>");
390 } else {
391 html("<tr><td colspan='4' class='ctx'>");
392 html_txt(line);
393 html("</td></tr>");
394 }
395 line[len - 1] = c;
396 }
397
398 void cgit_ssdiff_header_begin(void)
399 {
400 current_old_line = -1;
401 current_new_line = -1;
402 html("<tr><td class='space' colspan='4'><div></div></td></tr>");
403 html("<tr><td class='head' colspan='4'>");
404 }
405
406 void cgit_ssdiff_header_end(void)
407 {
408 html("</td><tr>");
409 }
410
411 void cgit_ssdiff_footer(void)
412 {
413 if (deferred_old || deferred_new)
414 cgit_ssdiff_print_deferred_lines();
415 html("<tr><td class='foot' colspan='4'></td></tr>");
416 }