]>
git.cameronkatri.com Git - cgit.git/blob - ui-ssdiff.c
9 static int current_old_line
, current_new_line
;
10 static int **L
= NULL
;
12 struct deferred_lines
{
15 struct deferred_lines
*next
;
18 static struct deferred_lines
*deferred_old
, *deferred_old_last
;
19 static struct deferred_lines
*deferred_new
, *deferred_new_last
;
21 static void create_or_reset_lcs_table(void)
26 memset(*L
, 0, sizeof(int) * MAX_SSDIFF_SIZE
);
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));
35 for (i
= 1; i
< MAX_SSDIFF_M
; i
++) {
36 L
[i
] = *L
+ i
* MAX_SSDIFF_N
;
40 static char *longest_common_subsequence(char *A
, char *B
)
49 // We bail if the lines are too long
50 if (m
>= MAX_SSDIFF_M
|| n
>= MAX_SSDIFF_N
)
53 create_or_reset_lcs_table();
55 for (i
= m
; i
>= 0; i
--) {
56 for (j
= n
; j
>= 0; j
--) {
57 if (A
[i
] == '\0' || B
[j
] == '\0') {
59 } else if (A
[i
] == B
[j
]) {
60 L
[i
][j
] = 1 + L
[i
+ 1][j
+ 1];
64 L
[i
][j
] = (tmp1
> tmp2
? tmp1
: tmp2
);
70 result
= xmalloc(lcs_length
+ 2);
71 memset(result
, 0, sizeof(*result
) * (lcs_length
+ 2));
76 while (i
< m
&& j
< n
) {
82 } else if (L
[i
+ 1][j
] >= L
[i
][j
+ 1]) {
92 static int line_from_hunk(char *line
, char type
)
97 buf1
= strchr(line
, type
);
101 buf2
= strchr(buf1
, ',');
105 buf2
= xmalloc(len
+ 1);
106 strlcpy(buf2
, buf1
, len
+ 1);
112 static char *replace_tabs(char *line
)
114 char *prev_buf
= line
;
116 size_t linelen
= strlen(line
);
128 for (i
= 0; i
< linelen
; i
++) {
132 result_len
= linelen
+ n_tabs
* 8;
133 result
= xmalloc(result_len
+ 1);
137 cur_buf
= strchr(prev_buf
, '\t');
139 strncat(result
, prev_buf
, result_len
);
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';
147 prev_buf
= cur_buf
+ 1;
152 static int calc_deferred_lines(struct deferred_lines
*start
)
154 struct deferred_lines
*item
= start
;
163 static void deferred_old_add(char *line
, int line_no
)
165 struct deferred_lines
*item
= xmalloc(sizeof(struct deferred_lines
));
166 item
->line
= xstrdup(line
);
167 item
->line_no
= line_no
;
170 deferred_old_last
->next
= item
;
171 deferred_old_last
= item
;
173 deferred_old
= deferred_old_last
= item
;
177 static void deferred_new_add(char *line
, int line_no
)
179 struct deferred_lines
*item
= xmalloc(sizeof(struct deferred_lines
));
180 item
->line
= xstrdup(line
);
181 item
->line_no
= line_no
;
184 deferred_new_last
->next
= item
;
185 deferred_new_last
= item
;
187 deferred_new
= deferred_new_last
= item
;
191 static void print_part_with_lcs(char *class, char *line
, char *lcs
)
193 int line_len
= strlen(line
);
199 for (i
= 0; i
< line_len
; i
++) {
202 if (line
[i
] == lcs
[j
])
206 htmlf("<span class='%s'>", class);
208 } else if (line
[i
] == lcs
[j
]) {
217 static void print_ssdiff_line(char *class,
221 char *new_line
, int individual_chars
)
226 old_line
= replace_tabs(old_line
+ 1);
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
);
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='");
239 htmlf("' id='%s'>%s</a>", lineno_str
, lineno_str
+ 1);
241 htmlf("<td class='%s'>", class);
244 htmlf("<td class='lineno'></td><td class='%s'>", class);
246 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
249 print_part_with_lcs("del", old_line
, lcs
);
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='");
262 htmlf("' id='%s'>%s</a>", lineno_str
, lineno_str
+ 1);
264 htmlf("<td class='%s'>", class);
267 htmlf("<td class='lineno'></td><td class='%s'>", class);
269 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
272 print_part_with_lcs("add", new_line
, lcs
);
286 static void print_deferred_old_lines(void)
288 struct deferred_lines
*iter_old
, *tmp
;
289 iter_old
= deferred_old
;
291 print_ssdiff_line("del", iter_old
->line_no
,
292 iter_old
->line
, -1, NULL
, 0);
293 tmp
= iter_old
->next
;
299 static void print_deferred_new_lines(void)
301 struct deferred_lines
*iter_new
, *tmp
;
302 iter_new
= deferred_new
;
304 print_ssdiff_line("add", -1, NULL
,
305 iter_new
->line_no
, iter_new
->line
, 0);
306 tmp
= iter_new
->next
;
312 static void print_deferred_changed_lines(void)
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);
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
,
325 iter_new
->line_no
, iter_new
->line
,
328 print_ssdiff_line("changed", iter_old
->line_no
,
329 iter_old
->line
, -1, NULL
, 0);
331 print_ssdiff_line("changed", -1, NULL
,
332 iter_new
->line_no
, iter_new
->line
, 0);
334 tmp
= iter_old
->next
;
340 tmp
= iter_new
->next
;
347 void cgit_ssdiff_print_deferred_lines(void)
349 if (!deferred_old
&& !deferred_new
)
351 if (deferred_old
&& !deferred_new
)
352 print_deferred_old_lines();
353 else if (!deferred_old
&& deferred_new
)
354 print_deferred_new_lines();
356 print_deferred_changed_lines();
357 deferred_old
= deferred_old_last
= NULL
;
358 deferred_new
= deferred_new_last
= NULL
;
362 * print a single line returned from xdiff
364 void cgit_ssdiff_line_cb(char *line
, int len
)
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
, '+');
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'>");
391 html("<tr><td colspan='4' class='ctx'>");
398 void cgit_ssdiff_header_begin(void)
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'>");
406 void cgit_ssdiff_header_end(void)
411 void cgit_ssdiff_footer(void)
413 if (deferred_old
|| deferred_new
)
414 cgit_ssdiff_print_deferred_lines();
415 html("<tr><td class='foot' colspan='4'></td></tr>");