]>
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 linelen
= strlen(result
);
140 strlcpy(&result
[linelen
], prev_buf
, result_len
- linelen
+ 1);
143 linelen
= strlen(result
);
144 strlcpy(&result
[linelen
], prev_buf
, cur_buf
- prev_buf
+ 1);
145 linelen
= strlen(result
);
146 memset(&result
[linelen
], ' ', 8 - (linelen
% 8));
147 result
[linelen
+ 8 - (linelen
% 8)] = '\0';
149 prev_buf
= cur_buf
+ 1;
154 static int calc_deferred_lines(struct deferred_lines
*start
)
156 struct deferred_lines
*item
= start
;
165 static void deferred_old_add(char *line
, int line_no
)
167 struct deferred_lines
*item
= xmalloc(sizeof(struct deferred_lines
));
168 item
->line
= xstrdup(line
);
169 item
->line_no
= line_no
;
172 deferred_old_last
->next
= item
;
173 deferred_old_last
= item
;
175 deferred_old
= deferred_old_last
= item
;
179 static void deferred_new_add(char *line
, int line_no
)
181 struct deferred_lines
*item
= xmalloc(sizeof(struct deferred_lines
));
182 item
->line
= xstrdup(line
);
183 item
->line_no
= line_no
;
186 deferred_new_last
->next
= item
;
187 deferred_new_last
= item
;
189 deferred_new
= deferred_new_last
= item
;
193 static void print_part_with_lcs(char *class, char *line
, char *lcs
)
195 int line_len
= strlen(line
);
201 for (i
= 0; i
< line_len
; i
++) {
204 if (line
[i
] == lcs
[j
])
208 htmlf("<span class='%s'>", class);
210 } else if (line
[i
] == lcs
[j
]) {
221 static void print_ssdiff_line(char *class,
225 char *new_line
, int individual_chars
)
230 old_line
= replace_tabs(old_line
+ 1);
232 new_line
= replace_tabs(new_line
+ 1);
233 if (individual_chars
&& old_line
&& new_line
)
234 lcs
= longest_common_subsequence(old_line
, new_line
);
236 if (old_line_no
> 0) {
237 struct diff_filespec
*old_file
= cgit_get_current_old_file();
238 char *lineno_str
= fmt("n%d", old_line_no
);
239 char *id_str
= fmt("id=%s#%s", is_null_oid(&old_file
->oid
)?"HEAD":oid_to_hex(old_rev_oid
), lineno_str
);
240 char *fileurl
= cgit_fileurl(ctx
.repo
->url
, "tree", old_file
->path
, id_str
);
241 html("<td class='lineno'><a href='");
243 htmlf("'>%s</a>", lineno_str
+ 1);
245 htmlf("<td class='%s'>", class);
248 htmlf("<td class='lineno'></td><td class='%s'>", class);
250 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
253 print_part_with_lcs("del", old_line
, lcs
);
259 if (new_line_no
> 0) {
260 struct diff_filespec
*new_file
= cgit_get_current_new_file();
261 char *lineno_str
= fmt("n%d", new_line_no
);
262 char *id_str
= fmt("id=%s#%s", is_null_oid(&new_file
->oid
)?"HEAD":oid_to_hex(new_rev_oid
), lineno_str
);
263 char *fileurl
= cgit_fileurl(ctx
.repo
->url
, "tree", new_file
->path
, id_str
);
264 html("<td class='lineno'><a href='");
266 htmlf("'>%s</a>", lineno_str
+ 1);
268 htmlf("<td class='%s'>", class);
271 htmlf("<td class='lineno'></td><td class='%s'>", class);
273 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
276 print_part_with_lcs("add", new_line
, lcs
);
290 static void print_deferred_old_lines(void)
292 struct deferred_lines
*iter_old
, *tmp
;
293 iter_old
= deferred_old
;
295 print_ssdiff_line("del", iter_old
->line_no
,
296 iter_old
->line
, -1, NULL
, 0);
297 tmp
= iter_old
->next
;
303 static void print_deferred_new_lines(void)
305 struct deferred_lines
*iter_new
, *tmp
;
306 iter_new
= deferred_new
;
308 print_ssdiff_line("add", -1, NULL
,
309 iter_new
->line_no
, iter_new
->line
, 0);
310 tmp
= iter_new
->next
;
316 static void print_deferred_changed_lines(void)
318 struct deferred_lines
*iter_old
, *iter_new
, *tmp
;
319 int n_old_lines
= calc_deferred_lines(deferred_old
);
320 int n_new_lines
= calc_deferred_lines(deferred_new
);
321 int individual_chars
= (n_old_lines
== n_new_lines
? 1 : 0);
323 iter_old
= deferred_old
;
324 iter_new
= deferred_new
;
325 while (iter_old
|| iter_new
) {
326 if (iter_old
&& iter_new
)
327 print_ssdiff_line("changed", iter_old
->line_no
,
329 iter_new
->line_no
, iter_new
->line
,
332 print_ssdiff_line("changed", iter_old
->line_no
,
333 iter_old
->line
, -1, NULL
, 0);
335 print_ssdiff_line("changed", -1, NULL
,
336 iter_new
->line_no
, iter_new
->line
, 0);
338 tmp
= iter_old
->next
;
344 tmp
= iter_new
->next
;
351 void cgit_ssdiff_print_deferred_lines(void)
353 if (!deferred_old
&& !deferred_new
)
355 if (deferred_old
&& !deferred_new
)
356 print_deferred_old_lines();
357 else if (!deferred_old
&& deferred_new
)
358 print_deferred_new_lines();
360 print_deferred_changed_lines();
361 deferred_old
= deferred_old_last
= NULL
;
362 deferred_new
= deferred_new_last
= NULL
;
366 * print a single line returned from xdiff
368 void cgit_ssdiff_line_cb(char *line
, int len
)
370 char c
= line
[len
- 1];
371 line
[len
- 1] = '\0';
372 if (line
[0] == '@') {
373 current_old_line
= line_from_hunk(line
, '-');
374 current_new_line
= line_from_hunk(line
, '+');
377 if (line
[0] == ' ') {
378 if (deferred_old
|| deferred_new
)
379 cgit_ssdiff_print_deferred_lines();
380 print_ssdiff_line("ctx", current_old_line
, line
,
381 current_new_line
, line
, 0);
382 current_old_line
+= 1;
383 current_new_line
+= 1;
384 } else if (line
[0] == '+') {
385 deferred_new_add(line
, current_new_line
);
386 current_new_line
+= 1;
387 } else if (line
[0] == '-') {
388 deferred_old_add(line
, current_old_line
);
389 current_old_line
+= 1;
390 } else if (line
[0] == '@') {
391 html("<tr><td colspan='4' class='hunk'>");
395 html("<tr><td colspan='4' class='ctx'>");
402 void cgit_ssdiff_header_begin(void)
404 current_old_line
= -1;
405 current_new_line
= -1;
406 html("<tr><td class='space' colspan='4'><div></div></td></tr>");
407 html("<tr><td class='head' colspan='4'>");
410 void cgit_ssdiff_header_end(void)
415 void cgit_ssdiff_footer(void)
417 if (deferred_old
|| deferred_new
)
418 cgit_ssdiff_print_deferred_lines();
419 html("<tr><td class='foot' colspan='4'></td></tr>");