]>
git.cameronkatri.com Git - cgit.git/blob - ui-ssdiff.c
8 static int current_old_line
, current_new_line
;
10 struct deferred_lines
{
13 struct deferred_lines
*next
;
16 static struct deferred_lines
*deferred_old
, *deferred_old_last
;
17 static struct deferred_lines
*deferred_new
, *deferred_new_last
;
19 static char *longest_common_subsequence(char *A
, char *B
)
29 for (i
= m
; i
>= 0; i
--) {
30 for (j
= n
; j
>= 0; j
--) {
31 if (A
[i
] == '\0' || B
[j
] == '\0') {
33 } else if (A
[i
] == B
[j
]) {
34 L
[i
][j
] = 1 + L
[i
+ 1][j
+ 1];
38 L
[i
][j
] = (tmp1
> tmp2
? tmp1
: tmp2
);
44 result
= xmalloc(lcs_length
+ 2);
45 memset(result
, 0, sizeof(*result
) * (lcs_length
+ 2));
50 while (i
< m
&& j
< n
) {
56 } else if (L
[i
+ 1][j
] >= L
[i
][j
+ 1]) {
65 static int line_from_hunk(char *line
, char type
)
70 buf1
= strchr(line
, type
);
74 buf2
= strchr(buf1
, ',');
78 buf2
= xmalloc(len
+ 1);
79 strncpy(buf2
, buf1
, len
);
86 static char *replace_tabs(char *line
)
88 char *prev_buf
= line
;
90 int linelen
= strlen(line
);
102 for (i
= 0; i
< linelen
; i
++)
105 result
= xmalloc(linelen
+ n_tabs
* 8 + 1);
109 cur_buf
= strchr(prev_buf
, '\t');
111 strcat(result
, prev_buf
);
115 strncat(result
, spaces
, 8 - (strlen(result
) % 8));
116 strncat(result
, prev_buf
, cur_buf
- prev_buf
);
118 prev_buf
= cur_buf
+ 1;
123 static int calc_deferred_lines(struct deferred_lines
*start
)
125 struct deferred_lines
*item
= start
;
134 static void deferred_old_add(char *line
, int line_no
)
136 struct deferred_lines
*item
= xmalloc(sizeof(struct deferred_lines
));
137 item
->line
= xstrdup(line
);
138 item
->line_no
= line_no
;
141 deferred_old_last
->next
= item
;
142 deferred_old_last
= item
;
144 deferred_old
= deferred_old_last
= item
;
148 static void deferred_new_add(char *line
, int line_no
)
150 struct deferred_lines
*item
= xmalloc(sizeof(struct deferred_lines
));
151 item
->line
= xstrdup(line
);
152 item
->line_no
= line_no
;
155 deferred_new_last
->next
= item
;
156 deferred_new_last
= item
;
158 deferred_new
= deferred_new_last
= item
;
162 static void print_part_with_lcs(char *class, char *line
, char *lcs
)
164 int line_len
= strlen(line
);
170 for (i
= 0; i
< line_len
; i
++) {
173 if (line
[i
] == lcs
[j
])
177 htmlf("<span class='%s'>", class);
179 } else if (line
[i
] == lcs
[j
]) {
188 static void print_ssdiff_line(char *class,
192 char *new_line
, int individual_chars
)
197 old_line
= replace_tabs(old_line
+ 1);
199 new_line
= replace_tabs(new_line
+ 1);
200 if (individual_chars
&& old_line
&& new_line
)
201 lcs
= longest_common_subsequence(old_line
, new_line
);
203 if (old_line_no
> 0) {
204 struct diff_filespec
*old_file
= cgit_get_current_old_file();
205 char *lineno_str
= fmt("n%d", old_line_no
);
206 char *id_str
= fmt("%s#%s", is_null_sha1(old_file
->sha1
)?"HEAD":sha1_to_hex(old_rev_sha1
), lineno_str
);
207 html("<td class='lineno'><a class='no' href='");
208 html(cgit_fileurl(ctx
.repo
->url
, "tree", old_file
->path
, id_str
));
209 htmlf("' id='%s' name='%s'>%s</a>", lineno_str
, lineno_str
, lineno_str
+ 1);
211 htmlf("<td class='%s'>", class);
213 htmlf("<td class='lineno'></td><td class='%s'>", class);
215 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
218 print_part_with_lcs("del", old_line
, lcs
);
224 if (new_line_no
> 0) {
225 struct diff_filespec
*new_file
= cgit_get_current_new_file();
226 char *lineno_str
= fmt("n%d", new_line_no
);
227 char *id_str
= fmt("%s#%s", is_null_sha1(new_file
->sha1
)?"HEAD":sha1_to_hex(new_rev_sha1
), lineno_str
);
228 html("<td class='lineno'><a class='no' href='");
229 html(cgit_fileurl(ctx
.repo
->url
, "tree", new_file
->path
, id_str
));
230 htmlf("' id='%s' name='%s'>%s</a>", lineno_str
, lineno_str
, lineno_str
+ 1);
232 htmlf("<td class='%s'>", class);
234 htmlf("<td class='lineno'></td><td class='%s'>", class);
236 htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
239 print_part_with_lcs("add", new_line
, lcs
);
253 static void print_deferred_old_lines()
255 struct deferred_lines
*iter_old
, *tmp
;
256 iter_old
= deferred_old
;
258 print_ssdiff_line("del", iter_old
->line_no
,
259 iter_old
->line
, -1, NULL
, 0);
260 tmp
= iter_old
->next
;
266 static void print_deferred_new_lines()
268 struct deferred_lines
*iter_new
, *tmp
;
269 iter_new
= deferred_new
;
271 print_ssdiff_line("add", -1, NULL
,
272 iter_new
->line_no
, iter_new
->line
, 0);
273 tmp
= iter_new
->next
;
279 static void print_deferred_changed_lines()
281 struct deferred_lines
*iter_old
, *iter_new
, *tmp
;
282 int n_old_lines
= calc_deferred_lines(deferred_old
);
283 int n_new_lines
= calc_deferred_lines(deferred_new
);
284 int individual_chars
= (n_old_lines
== n_new_lines
? 1 : 0);
286 iter_old
= deferred_old
;
287 iter_new
= deferred_new
;
288 while (iter_old
|| iter_new
) {
289 if (iter_old
&& iter_new
)
290 print_ssdiff_line("changed", iter_old
->line_no
,
292 iter_new
->line_no
, iter_new
->line
,
295 print_ssdiff_line("changed", iter_old
->line_no
,
296 iter_old
->line
, -1, NULL
, 0);
298 print_ssdiff_line("changed", -1, NULL
,
299 iter_new
->line_no
, iter_new
->line
, 0);
301 tmp
= iter_old
->next
;
307 tmp
= iter_new
->next
;
314 void cgit_ssdiff_print_deferred_lines()
316 if (!deferred_old
&& !deferred_new
)
318 if (deferred_old
&& !deferred_new
)
319 print_deferred_old_lines();
320 else if (!deferred_old
&& deferred_new
)
321 print_deferred_new_lines();
323 print_deferred_changed_lines();
324 deferred_old
= deferred_old_last
= NULL
;
325 deferred_new
= deferred_new_last
= NULL
;
329 * print a single line returned from xdiff
331 void cgit_ssdiff_line_cb(char *line
, int len
)
333 char c
= line
[len
- 1];
334 line
[len
- 1] = '\0';
335 if (line
[0] == '@') {
336 current_old_line
= line_from_hunk(line
, '-');
337 current_new_line
= line_from_hunk(line
, '+');
340 if (line
[0] == ' ') {
341 if (deferred_old
|| deferred_new
)
342 cgit_ssdiff_print_deferred_lines();
343 print_ssdiff_line("ctx", current_old_line
, line
,
344 current_new_line
, line
, 0);
345 current_old_line
+= 1;
346 current_new_line
+= 1;
347 } else if (line
[0] == '+') {
348 deferred_new_add(line
, current_new_line
);
349 current_new_line
+= 1;
350 } else if (line
[0] == '-') {
351 deferred_old_add(line
, current_old_line
);
352 current_old_line
+= 1;
353 } else if (line
[0] == '@') {
354 html("<tr><td colspan='4' class='hunk'>");
358 html("<tr><td colspan='4' class='ctx'>");
365 void cgit_ssdiff_header_begin()
367 current_old_line
= -1;
368 current_new_line
= -1;
369 html("<tr><td class='space' colspan='4'><div></div></td></tr>");
370 html("<tr><td class='head' colspan='4'>");
373 void cgit_ssdiff_header_end()
378 void cgit_ssdiff_footer()
380 if (deferred_old
|| deferred_new
)
381 cgit_ssdiff_print_deferred_lines();
382 html("<tr><td class='foot' colspan='4'></td></tr>");