]> git.cameronkatri.com Git - cgit.git/blob - ui-ssdiff.c
ui-shared: restrict to 15 levels
[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 html("</span>");
211 j += 1;
212 }
213 html_txt(c);
214 }
215 if (!same)
216 html("</span>");
217 }
218
219 static void print_ssdiff_line(char *class,
220 int old_line_no,
221 char *old_line,
222 int new_line_no,
223 char *new_line, int individual_chars)
224 {
225 char *lcs = NULL;
226
227 if (old_line)
228 old_line = replace_tabs(old_line + 1);
229 if (new_line)
230 new_line = replace_tabs(new_line + 1);
231 if (individual_chars && old_line && new_line)
232 lcs = longest_common_subsequence(old_line, new_line);
233 html("<tr>\n");
234 if (old_line_no > 0) {
235 struct diff_filespec *old_file = cgit_get_current_old_file();
236 char *lineno_str = fmt("n%d", old_line_no);
237 char *id_str = fmt("id=%s#%s", is_null_oid(&old_file->oid)?"HEAD":oid_to_hex(old_rev_oid), lineno_str);
238 char *fileurl = cgit_fileurl(ctx.repo->url, "tree", old_file->path, id_str);
239 html("<td class='lineno'><a href='");
240 html(fileurl);
241 htmlf("'>%s</a>", lineno_str + 1);
242 html("</td>");
243 htmlf("<td class='%s'>", class);
244 free(fileurl);
245 } else if (old_line)
246 htmlf("<td class='lineno'></td><td class='%s'>", class);
247 else
248 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
249 if (old_line) {
250 if (lcs)
251 print_part_with_lcs("del", old_line, lcs);
252 else
253 html_txt(old_line);
254 }
255
256 html("</td>\n");
257 if (new_line_no > 0) {
258 struct diff_filespec *new_file = cgit_get_current_new_file();
259 char *lineno_str = fmt("n%d", new_line_no);
260 char *id_str = fmt("id=%s#%s", is_null_oid(&new_file->oid)?"HEAD":oid_to_hex(new_rev_oid), lineno_str);
261 char *fileurl = cgit_fileurl(ctx.repo->url, "tree", new_file->path, id_str);
262 html("<td class='lineno'><a href='");
263 html(fileurl);
264 htmlf("'>%s</a>", lineno_str + 1);
265 html("</td>");
266 htmlf("<td class='%s'>", class);
267 free(fileurl);
268 } else if (new_line)
269 htmlf("<td class='lineno'></td><td class='%s'>", class);
270 else
271 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
272 if (new_line) {
273 if (lcs)
274 print_part_with_lcs("add", new_line, lcs);
275 else
276 html_txt(new_line);
277 }
278
279 html("</td></tr>");
280 if (lcs)
281 free(lcs);
282 if (new_line)
283 free(new_line);
284 if (old_line)
285 free(old_line);
286 }
287
288 static void print_deferred_old_lines(void)
289 {
290 struct deferred_lines *iter_old, *tmp;
291 iter_old = deferred_old;
292 while (iter_old) {
293 print_ssdiff_line("del", iter_old->line_no,
294 iter_old->line, -1, NULL, 0);
295 tmp = iter_old->next;
296 free(iter_old);
297 iter_old = tmp;
298 }
299 }
300
301 static void print_deferred_new_lines(void)
302 {
303 struct deferred_lines *iter_new, *tmp;
304 iter_new = deferred_new;
305 while (iter_new) {
306 print_ssdiff_line("add", -1, NULL,
307 iter_new->line_no, iter_new->line, 0);
308 tmp = iter_new->next;
309 free(iter_new);
310 iter_new = tmp;
311 }
312 }
313
314 static void print_deferred_changed_lines(void)
315 {
316 struct deferred_lines *iter_old, *iter_new, *tmp;
317 int n_old_lines = calc_deferred_lines(deferred_old);
318 int n_new_lines = calc_deferred_lines(deferred_new);
319 int individual_chars = (n_old_lines == n_new_lines ? 1 : 0);
320
321 iter_old = deferred_old;
322 iter_new = deferred_new;
323 while (iter_old || iter_new) {
324 if (iter_old && iter_new)
325 print_ssdiff_line("changed", iter_old->line_no,
326 iter_old->line,
327 iter_new->line_no, iter_new->line,
328 individual_chars);
329 else if (iter_old)
330 print_ssdiff_line("changed", iter_old->line_no,
331 iter_old->line, -1, NULL, 0);
332 else if (iter_new)
333 print_ssdiff_line("changed", -1, NULL,
334 iter_new->line_no, iter_new->line, 0);
335 if (iter_old) {
336 tmp = iter_old->next;
337 free(iter_old);
338 iter_old = tmp;
339 }
340
341 if (iter_new) {
342 tmp = iter_new->next;
343 free(iter_new);
344 iter_new = tmp;
345 }
346 }
347 }
348
349 void cgit_ssdiff_print_deferred_lines(void)
350 {
351 if (!deferred_old && !deferred_new)
352 return;
353 if (deferred_old && !deferred_new)
354 print_deferred_old_lines();
355 else if (!deferred_old && deferred_new)
356 print_deferred_new_lines();
357 else
358 print_deferred_changed_lines();
359 deferred_old = deferred_old_last = NULL;
360 deferred_new = deferred_new_last = NULL;
361 }
362
363 /*
364 * print a single line returned from xdiff
365 */
366 void cgit_ssdiff_line_cb(char *line, int len)
367 {
368 char c = line[len - 1];
369 line[len - 1] = '\0';
370 if (line[0] == '@') {
371 current_old_line = line_from_hunk(line, '-');
372 current_new_line = line_from_hunk(line, '+');
373 }
374
375 if (line[0] == ' ') {
376 if (deferred_old || deferred_new)
377 cgit_ssdiff_print_deferred_lines();
378 print_ssdiff_line("ctx", current_old_line, line,
379 current_new_line, line, 0);
380 current_old_line += 1;
381 current_new_line += 1;
382 } else if (line[0] == '+') {
383 deferred_new_add(line, current_new_line);
384 current_new_line += 1;
385 } else if (line[0] == '-') {
386 deferred_old_add(line, current_old_line);
387 current_old_line += 1;
388 } else if (line[0] == '@') {
389 html("<tr><td colspan='4' class='hunk'>");
390 html_txt(line);
391 html("</td></tr>");
392 } else {
393 html("<tr><td colspan='4' class='ctx'>");
394 html_txt(line);
395 html("</td></tr>");
396 }
397 line[len - 1] = c;
398 }
399
400 void cgit_ssdiff_header_begin(void)
401 {
402 current_old_line = -1;
403 current_new_line = -1;
404 html("<tr><td class='space' colspan='4'><div></div></td></tr>");
405 html("<tr><td class='head' colspan='4'>");
406 }
407
408 void cgit_ssdiff_header_end(void)
409 {
410 html("</td></tr>");
411 }
412
413 void cgit_ssdiff_footer(void)
414 {
415 if (deferred_old || deferred_new)
416 cgit_ssdiff_print_deferred_lines();
417 html("<tr><td class='foot' colspan='4'></td></tr>");
418 }