]>
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 strncpy(buf2
, buf1
, len
);
113 static char *replace_tabs(char *line
)
115 char *prev_buf
= line
;
117 size_t linelen
= strlen(line
);
128 for (i
= 0; i
< linelen
; i
++) {
132 result
= xmalloc(linelen
+ n_tabs
* 8 + 1);
136 cur_buf
= strchr(prev_buf
, '\t');
138 strcat(result
, prev_buf
);
141 strncat(result
, prev_buf
, cur_buf
- prev_buf
);
142 linelen
= strlen(result
);
143 memset(&result
[linelen
], ' ', 8 - (linelen
% 8));
144 result
[linelen
+ 8 - (linelen
% 8)] = '\0';
146 prev_buf
= cur_buf
+ 1;
151 static int calc_deferred_lines(struct deferred_lines
*start
)
153 struct deferred_lines
*item
= start
;
162 static void deferred_old_add(char *line
, int line_no
)
164 struct deferred_lines
*item
= xmalloc(sizeof(struct deferred_lines
));
165 item
->line
= xstrdup(line
);
166 item
->line_no
= line_no
;
169 deferred_old_last
->next
= item
;
170 deferred_old_last
= item
;
172 deferred_old
= deferred_old_last
= item
;
176 static void deferred_new_add(char *line
, int line_no
)
178 struct deferred_lines
*item
= xmalloc(sizeof(struct deferred_lines
));
179 item
->line
= xstrdup(line
);
180 item
->line_no
= line_no
;
183 deferred_new_last
->next
= item
;
184 deferred_new_last
= item
;
186 deferred_new
= deferred_new_last
= item
;
190 static void print_part_with_lcs(char *class, char *line
, char *lcs
)
192 int line_len
= strlen(line
);
198 for (i
= 0; i
< line_len
; i
++) {
201 if (line
[i
] == lcs
[j
])
205 htmlf("<span class='%s'>", class);
207 } else if (line
[i
] == lcs
[j
]) {
216 static void print_ssdiff_line(char *class,
220 char *new_line
, int individual_chars
)
225 old_line
= replace_tabs(old_line
+ 1);
227 new_line
= replace_tabs(new_line
+ 1);
228 if (individual_chars
&& old_line
&& new_line
)
229 lcs
= longest_common_subsequence(old_line
, new_line
);
231 if (old_line_no
> 0) {
232 struct diff_filespec
*old_file
= cgit_get_current_old_file();
233 char *lineno_str
= fmt("n%d", old_line_no
);
234 char *id_str
= fmt("id=%s#%s", is_null_oid(&old_file
->oid
)?"HEAD":oid_to_hex(old_rev_oid
), lineno_str
);
235 char *fileurl
= cgit_fileurl(ctx
.repo
->url
, "tree", old_file
->path
, id_str
);
236 html("<td class='lineno'><a href='");
238 htmlf("' id='%s'>%s</a>", lineno_str
, lineno_str
+ 1);
240 htmlf("<td class='%s'>", class);
243 htmlf("<td class='lineno'></td><td class='%s'>", class);
245 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
248 print_part_with_lcs("del", old_line
, lcs
);
254 if (new_line_no
> 0) {
255 struct diff_filespec
*new_file
= cgit_get_current_new_file();
256 char *lineno_str
= fmt("n%d", new_line_no
);
257 char *id_str
= fmt("id=%s#%s", is_null_oid(&new_file
->oid
)?"HEAD":oid_to_hex(new_rev_oid
), lineno_str
);
258 char *fileurl
= cgit_fileurl(ctx
.repo
->url
, "tree", new_file
->path
, id_str
);
259 html("<td class='lineno'><a href='");
261 htmlf("' id='%s'>%s</a>", lineno_str
, lineno_str
+ 1);
263 htmlf("<td class='%s'>", class);
266 htmlf("<td class='lineno'></td><td class='%s'>", class);
268 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
271 print_part_with_lcs("add", new_line
, lcs
);
285 static void print_deferred_old_lines(void)
287 struct deferred_lines
*iter_old
, *tmp
;
288 iter_old
= deferred_old
;
290 print_ssdiff_line("del", iter_old
->line_no
,
291 iter_old
->line
, -1, NULL
, 0);
292 tmp
= iter_old
->next
;
298 static void print_deferred_new_lines(void)
300 struct deferred_lines
*iter_new
, *tmp
;
301 iter_new
= deferred_new
;
303 print_ssdiff_line("add", -1, NULL
,
304 iter_new
->line_no
, iter_new
->line
, 0);
305 tmp
= iter_new
->next
;
311 static void print_deferred_changed_lines(void)
313 struct deferred_lines
*iter_old
, *iter_new
, *tmp
;
314 int n_old_lines
= calc_deferred_lines(deferred_old
);
315 int n_new_lines
= calc_deferred_lines(deferred_new
);
316 int individual_chars
= (n_old_lines
== n_new_lines
? 1 : 0);
318 iter_old
= deferred_old
;
319 iter_new
= deferred_new
;
320 while (iter_old
|| iter_new
) {
321 if (iter_old
&& iter_new
)
322 print_ssdiff_line("changed", iter_old
->line_no
,
324 iter_new
->line_no
, iter_new
->line
,
327 print_ssdiff_line("changed", iter_old
->line_no
,
328 iter_old
->line
, -1, NULL
, 0);
330 print_ssdiff_line("changed", -1, NULL
,
331 iter_new
->line_no
, iter_new
->line
, 0);
333 tmp
= iter_old
->next
;
339 tmp
= iter_new
->next
;
346 void cgit_ssdiff_print_deferred_lines(void)
348 if (!deferred_old
&& !deferred_new
)
350 if (deferred_old
&& !deferred_new
)
351 print_deferred_old_lines();
352 else if (!deferred_old
&& deferred_new
)
353 print_deferred_new_lines();
355 print_deferred_changed_lines();
356 deferred_old
= deferred_old_last
= NULL
;
357 deferred_new
= deferred_new_last
= NULL
;
361 * print a single line returned from xdiff
363 void cgit_ssdiff_line_cb(char *line
, int len
)
365 char c
= line
[len
- 1];
366 line
[len
- 1] = '\0';
367 if (line
[0] == '@') {
368 current_old_line
= line_from_hunk(line
, '-');
369 current_new_line
= line_from_hunk(line
, '+');
372 if (line
[0] == ' ') {
373 if (deferred_old
|| deferred_new
)
374 cgit_ssdiff_print_deferred_lines();
375 print_ssdiff_line("ctx", current_old_line
, line
,
376 current_new_line
, line
, 0);
377 current_old_line
+= 1;
378 current_new_line
+= 1;
379 } else if (line
[0] == '+') {
380 deferred_new_add(line
, current_new_line
);
381 current_new_line
+= 1;
382 } else if (line
[0] == '-') {
383 deferred_old_add(line
, current_old_line
);
384 current_old_line
+= 1;
385 } else if (line
[0] == '@') {
386 html("<tr><td colspan='4' class='hunk'>");
390 html("<tr><td colspan='4' class='ctx'>");
397 void cgit_ssdiff_header_begin(void)
399 current_old_line
= -1;
400 current_new_line
= -1;
401 html("<tr><td class='space' colspan='4'><div></div></td></tr>");
402 html("<tr><td class='head' colspan='4'>");
405 void cgit_ssdiff_header_end(void)
410 void cgit_ssdiff_footer(void)
412 if (deferred_old
|| deferred_new
)
413 cgit_ssdiff_print_deferred_lines();
414 html("<tr><td class='foot' colspan='4'></td></tr>");