]>
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(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
);
108 int res
= atoi(buf2
);
113 static char *replace_tabs(char *line
)
115 char *prev_buf
= line
;
117 int linelen
= strlen(line
);
129 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
);
142 strncat(result
, spaces
, 8 - (strlen(result
) % 8));
143 strncat(result
, prev_buf
, cur_buf
- prev_buf
);
145 prev_buf
= cur_buf
+ 1;
150 static int calc_deferred_lines(struct deferred_lines
*start
)
152 struct deferred_lines
*item
= start
;
161 static void deferred_old_add(char *line
, int line_no
)
163 struct deferred_lines
*item
= xmalloc(sizeof(struct deferred_lines
));
164 item
->line
= xstrdup(line
);
165 item
->line_no
= line_no
;
168 deferred_old_last
->next
= item
;
169 deferred_old_last
= item
;
171 deferred_old
= deferred_old_last
= item
;
175 static void deferred_new_add(char *line
, int line_no
)
177 struct deferred_lines
*item
= xmalloc(sizeof(struct deferred_lines
));
178 item
->line
= xstrdup(line
);
179 item
->line_no
= line_no
;
182 deferred_new_last
->next
= item
;
183 deferred_new_last
= item
;
185 deferred_new
= deferred_new_last
= item
;
189 static void print_part_with_lcs(char *class, char *line
, char *lcs
)
191 int line_len
= strlen(line
);
197 for (i
= 0; i
< line_len
; i
++) {
200 if (line
[i
] == lcs
[j
])
204 htmlf("<span class='%s'>", class);
206 } else if (line
[i
] == lcs
[j
]) {
215 static void print_ssdiff_line(char *class,
219 char *new_line
, int individual_chars
)
224 old_line
= replace_tabs(old_line
+ 1);
226 new_line
= replace_tabs(new_line
+ 1);
227 if (individual_chars
&& old_line
&& new_line
)
228 lcs
= longest_common_subsequence(old_line
, new_line
);
230 if (old_line_no
> 0) {
231 struct diff_filespec
*old_file
= cgit_get_current_old_file();
232 char *lineno_str
= fmt("n%d", old_line_no
);
233 char *id_str
= fmt("%s#%s", is_null_sha1(old_file
->sha1
)?"HEAD":sha1_to_hex(old_rev_sha1
), lineno_str
);
234 html("<td class='lineno'><a class='no' href='");
235 html(cgit_fileurl(ctx
.repo
->url
, "tree", old_file
->path
, id_str
));
236 htmlf("' id='%s' name='%s'>%s</a>", lineno_str
, lineno_str
, lineno_str
+ 1);
238 htmlf("<td class='%s'>", class);
240 htmlf("<td class='lineno'></td><td class='%s'>", class);
242 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
245 print_part_with_lcs("del", old_line
, lcs
);
251 if (new_line_no
> 0) {
252 struct diff_filespec
*new_file
= cgit_get_current_new_file();
253 char *lineno_str
= fmt("n%d", new_line_no
);
254 char *id_str
= fmt("%s#%s", is_null_sha1(new_file
->sha1
)?"HEAD":sha1_to_hex(new_rev_sha1
), lineno_str
);
255 html("<td class='lineno'><a class='no' href='");
256 html(cgit_fileurl(ctx
.repo
->url
, "tree", new_file
->path
, id_str
));
257 htmlf("' id='%s' name='%s'>%s</a>", lineno_str
, lineno_str
, lineno_str
+ 1);
259 htmlf("<td class='%s'>", class);
261 htmlf("<td class='lineno'></td><td class='%s'>", class);
263 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
266 print_part_with_lcs("add", new_line
, lcs
);
280 static void print_deferred_old_lines()
282 struct deferred_lines
*iter_old
, *tmp
;
283 iter_old
= deferred_old
;
285 print_ssdiff_line("del", iter_old
->line_no
,
286 iter_old
->line
, -1, NULL
, 0);
287 tmp
= iter_old
->next
;
293 static void print_deferred_new_lines()
295 struct deferred_lines
*iter_new
, *tmp
;
296 iter_new
= deferred_new
;
298 print_ssdiff_line("add", -1, NULL
,
299 iter_new
->line_no
, iter_new
->line
, 0);
300 tmp
= iter_new
->next
;
306 static void print_deferred_changed_lines()
308 struct deferred_lines
*iter_old
, *iter_new
, *tmp
;
309 int n_old_lines
= calc_deferred_lines(deferred_old
);
310 int n_new_lines
= calc_deferred_lines(deferred_new
);
311 int individual_chars
= (n_old_lines
== n_new_lines
? 1 : 0);
313 iter_old
= deferred_old
;
314 iter_new
= deferred_new
;
315 while (iter_old
|| iter_new
) {
316 if (iter_old
&& iter_new
)
317 print_ssdiff_line("changed", iter_old
->line_no
,
319 iter_new
->line_no
, iter_new
->line
,
322 print_ssdiff_line("changed", iter_old
->line_no
,
323 iter_old
->line
, -1, NULL
, 0);
325 print_ssdiff_line("changed", -1, NULL
,
326 iter_new
->line_no
, iter_new
->line
, 0);
328 tmp
= iter_old
->next
;
334 tmp
= iter_new
->next
;
341 void cgit_ssdiff_print_deferred_lines()
343 if (!deferred_old
&& !deferred_new
)
345 if (deferred_old
&& !deferred_new
)
346 print_deferred_old_lines();
347 else if (!deferred_old
&& deferred_new
)
348 print_deferred_new_lines();
350 print_deferred_changed_lines();
351 deferred_old
= deferred_old_last
= NULL
;
352 deferred_new
= deferred_new_last
= NULL
;
356 * print a single line returned from xdiff
358 void cgit_ssdiff_line_cb(char *line
, int len
)
360 char c
= line
[len
- 1];
361 line
[len
- 1] = '\0';
362 if (line
[0] == '@') {
363 current_old_line
= line_from_hunk(line
, '-');
364 current_new_line
= line_from_hunk(line
, '+');
367 if (line
[0] == ' ') {
368 if (deferred_old
|| deferred_new
)
369 cgit_ssdiff_print_deferred_lines();
370 print_ssdiff_line("ctx", current_old_line
, line
,
371 current_new_line
, line
, 0);
372 current_old_line
+= 1;
373 current_new_line
+= 1;
374 } else if (line
[0] == '+') {
375 deferred_new_add(line
, current_new_line
);
376 current_new_line
+= 1;
377 } else if (line
[0] == '-') {
378 deferred_old_add(line
, current_old_line
);
379 current_old_line
+= 1;
380 } else if (line
[0] == '@') {
381 html("<tr><td colspan='4' class='hunk'>");
385 html("<tr><td colspan='4' class='ctx'>");
392 void cgit_ssdiff_header_begin()
394 current_old_line
= -1;
395 current_new_line
= -1;
396 html("<tr><td class='space' colspan='4'><div></div></td></tr>");
397 html("<tr><td class='head' colspan='4'>");
400 void cgit_ssdiff_header_end()
405 void cgit_ssdiff_footer()
407 if (deferred_old
|| deferred_new
)
408 cgit_ssdiff_print_deferred_lines();
409 html("<tr><td class='foot' colspan='4'></td></tr>");