]>
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()
26 memset(*L
, 0, sizeof(*L
) * 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
)
45 int tmp1
, tmp2
, length
;
49 length
= (m
+ 1) * (n
+ 1);
51 // We bail if the lines are too long
52 if (length
> MAX_SSDIFF_SIZE
)
55 create_or_reset_lcs_table();
57 for (i
= m
; i
>= 0; i
--) {
58 for (j
= n
; j
>= 0; j
--) {
59 if (A
[i
] == '\0' || B
[j
] == '\0') {
61 } else if (A
[i
] == B
[j
]) {
62 L
[i
][j
] = 1 + L
[i
+ 1][j
+ 1];
66 L
[i
][j
] = (tmp1
> tmp2
? tmp1
: tmp2
);
72 result
= xmalloc(lcs_length
+ 2);
73 memset(result
, 0, sizeof(*result
) * (lcs_length
+ 2));
78 while (i
< m
&& j
< n
) {
84 } else if (L
[i
+ 1][j
] >= L
[i
][j
+ 1]) {
94 static int line_from_hunk(char *line
, char type
)
99 buf1
= strchr(line
, type
);
103 buf2
= strchr(buf1
, ',');
107 buf2
= xmalloc(len
+ 1);
108 strncpy(buf2
, buf1
, len
);
110 int res
= atoi(buf2
);
115 static char *replace_tabs(char *line
)
117 char *prev_buf
= line
;
119 int linelen
= strlen(line
);
131 for (i
= 0; i
< linelen
; i
++)
134 result
= xmalloc(linelen
+ n_tabs
* 8 + 1);
138 cur_buf
= strchr(prev_buf
, '\t');
140 strcat(result
, prev_buf
);
144 strncat(result
, spaces
, 8 - (strlen(result
) % 8));
145 strncat(result
, prev_buf
, cur_buf
- prev_buf
);
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("%s#%s", is_null_sha1(old_file
->sha1
)?"HEAD":sha1_to_hex(old_rev_sha1
), lineno_str
);
236 html("<td class='lineno'><a class='no' href='");
237 html(cgit_fileurl(ctx
.repo
->url
, "tree", old_file
->path
, id_str
));
238 htmlf("' id='%s' name='%s'>%s</a>", lineno_str
, lineno_str
, lineno_str
+ 1);
240 htmlf("<td class='%s'>", class);
242 htmlf("<td class='lineno'></td><td class='%s'>", class);
244 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
247 print_part_with_lcs("del", old_line
, lcs
);
253 if (new_line_no
> 0) {
254 struct diff_filespec
*new_file
= cgit_get_current_new_file();
255 char *lineno_str
= fmt("n%d", new_line_no
);
256 char *id_str
= fmt("%s#%s", is_null_sha1(new_file
->sha1
)?"HEAD":sha1_to_hex(new_rev_sha1
), lineno_str
);
257 html("<td class='lineno'><a class='no' href='");
258 html(cgit_fileurl(ctx
.repo
->url
, "tree", new_file
->path
, id_str
));
259 htmlf("' id='%s' name='%s'>%s</a>", lineno_str
, lineno_str
, lineno_str
+ 1);
261 htmlf("<td class='%s'>", class);
263 htmlf("<td class='lineno'></td><td class='%s'>", class);
265 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
268 print_part_with_lcs("add", new_line
, lcs
);
282 static void print_deferred_old_lines()
284 struct deferred_lines
*iter_old
, *tmp
;
285 iter_old
= deferred_old
;
287 print_ssdiff_line("del", iter_old
->line_no
,
288 iter_old
->line
, -1, NULL
, 0);
289 tmp
= iter_old
->next
;
295 static void print_deferred_new_lines()
297 struct deferred_lines
*iter_new
, *tmp
;
298 iter_new
= deferred_new
;
300 print_ssdiff_line("add", -1, NULL
,
301 iter_new
->line_no
, iter_new
->line
, 0);
302 tmp
= iter_new
->next
;
308 static void print_deferred_changed_lines()
310 struct deferred_lines
*iter_old
, *iter_new
, *tmp
;
311 int n_old_lines
= calc_deferred_lines(deferred_old
);
312 int n_new_lines
= calc_deferred_lines(deferred_new
);
313 int individual_chars
= (n_old_lines
== n_new_lines
? 1 : 0);
315 iter_old
= deferred_old
;
316 iter_new
= deferred_new
;
317 while (iter_old
|| iter_new
) {
318 if (iter_old
&& iter_new
)
319 print_ssdiff_line("changed", iter_old
->line_no
,
321 iter_new
->line_no
, iter_new
->line
,
324 print_ssdiff_line("changed", iter_old
->line_no
,
325 iter_old
->line
, -1, NULL
, 0);
327 print_ssdiff_line("changed", -1, NULL
,
328 iter_new
->line_no
, iter_new
->line
, 0);
330 tmp
= iter_old
->next
;
336 tmp
= iter_new
->next
;
343 void cgit_ssdiff_print_deferred_lines()
345 if (!deferred_old
&& !deferred_new
)
347 if (deferred_old
&& !deferred_new
)
348 print_deferred_old_lines();
349 else if (!deferred_old
&& deferred_new
)
350 print_deferred_new_lines();
352 print_deferred_changed_lines();
353 deferred_old
= deferred_old_last
= NULL
;
354 deferred_new
= deferred_new_last
= NULL
;
358 * print a single line returned from xdiff
360 void cgit_ssdiff_line_cb(char *line
, int len
)
362 char c
= line
[len
- 1];
363 line
[len
- 1] = '\0';
364 if (line
[0] == '@') {
365 current_old_line
= line_from_hunk(line
, '-');
366 current_new_line
= line_from_hunk(line
, '+');
369 if (line
[0] == ' ') {
370 if (deferred_old
|| deferred_new
)
371 cgit_ssdiff_print_deferred_lines();
372 print_ssdiff_line("ctx", current_old_line
, line
,
373 current_new_line
, line
, 0);
374 current_old_line
+= 1;
375 current_new_line
+= 1;
376 } else if (line
[0] == '+') {
377 deferred_new_add(line
, current_new_line
);
378 current_new_line
+= 1;
379 } else if (line
[0] == '-') {
380 deferred_old_add(line
, current_old_line
);
381 current_old_line
+= 1;
382 } else if (line
[0] == '@') {
383 html("<tr><td colspan='4' class='hunk'>");
387 html("<tr><td colspan='4' class='ctx'>");
394 void cgit_ssdiff_header_begin()
396 current_old_line
= -1;
397 current_new_line
= -1;
398 html("<tr><td class='space' colspan='4'><div></div></td></tr>");
399 html("<tr><td class='head' colspan='4'>");
402 void cgit_ssdiff_header_end()
407 void cgit_ssdiff_footer()
409 if (deferred_old
|| deferred_new
)
410 cgit_ssdiff_print_deferred_lines();
411 html("<tr><td class='foot' colspan='4'></td></tr>");