blob: ad543f457de588e9b92a548e704ce02190f79c8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
 * The side by side rendering of a diff, the layout a reader gets instead of
 * the unified listing. ui-diff.c drives it, handing over each line xdiff
 * produces and calling in around the header and the footer of every file.
 * Removed and added lines are held back until their run ends, so that the two
 * sides can be paired into a row apiece. When a run has as many removals as
 * additions the paired lines are compared character by character, and what
 * differs within them is marked.
 */

#include "cgit.h"
#include "html.h"
#include "ui-diff.h"
#include "ui-shared.h"
#include "ui-ssdiff.h"

// One line held back until its run ends, owning the copy taken of it.
struct deferred_line {
	int line_no;
	char *line;
	struct deferred_line *next;
};

static int current_old_line, current_new_line;
static int **lcs_table;
static struct deferred_line *deferred_old, *deferred_old_last;
static struct deferred_line *deferred_new, *deferred_new_last;

/*
 * The table is reused by every comparison and nothing clears it in between,
 * because the fill in longest_common_subsequence works back from the far
 * corner and writes every cell it goes on to read.
 */
static void create_lcs_table(void)
{
	int i;

	if (lcs_table)
		return;

	lcs_table = xcalloc(MAX_SSDIFF_M, sizeof(int *));
	lcs_table[0] = xcalloc(MAX_SSDIFF_SIZE, sizeof(int));
	for (i = 1; i < MAX_SSDIFF_M; i++)
		lcs_table[i] = lcs_table[0] + i * MAX_SSDIFF_N;
}

/*
 * The characters the two lines have in common, in order, which the caller
 * owns. A line too long for the table gets NULL back and is shown whole
 * instead.
 */
static char *longest_common_subsequence(const char *old_line, const char *new_line)
{
	int old_len = strlen(old_line);
	int new_len = strlen(new_line);
	int i, j, pos, lcs_len;
	char *lcs;

	if (old_len >= MAX_SSDIFF_M || new_len >= MAX_SSDIFF_N)
		return NULL;

	create_lcs_table();

	for (i = old_len; i >= 0; i--) {
		for (j = new_len; j >= 0; j--) {
			if (old_line[i] == '\0' || new_line[j] == '\0') {
				lcs_table[i][j] = 0;
			} else if (old_line[i] == new_line[j]) {
				lcs_table[i][j] = 1 + lcs_table[i + 1][j + 1];
			} else {
				int drop_old = lcs_table[i + 1][j];
				int drop_new = lcs_table[i][j + 1];

				lcs_table[i][j] = (drop_old > drop_new ? drop_old : drop_new);
			}
		}
	}

	lcs_len = lcs_table[0][0];
	lcs = xmalloc(lcs_len + 2);
	memset(lcs, 0, sizeof(*lcs) * (lcs_len + 2));

	pos = 0;
	i = 0;
	j = 0;
	while (i < old_len && j < new_len) {
		if (old_line[i] == new_line[j]) {
			lcs[pos] = old_line[i];
			pos++;
			i++;
			j++;
		} else if (lcs_table[i + 1][j] >= lcs_table[i][j + 1]) {
			i++;
		} else {
			j++;
		}
	}

	return lcs;
}

/*
 * The line with its tabs expanded, which the caller owns. Built in one
 * forward pass so a tab heavy line stays linear in its own length.
 */
static char *expand_tabs(const char *line)
{
	struct strbuf out = STRBUF_INIT;
	const char *p;

	for (p = line; *p; p++) {
		if (*p == '\t')
			strbuf_addchars(&out, ' ', TAB_WIDTH - (out.len % TAB_WIDTH));
		else
			strbuf_addch(&out, *p);
	}
	return strbuf_detach(&out, NULL);
}

static void flush_run(struct strbuf *run)
{
	if (!run->len)
		return;
	html_txt(run->buf);
	strbuf_reset(run);
}

/*
 * A stretch the other side does not share is escaped in one call, so a
 * changed line does not go through the output path a byte at a time.
 */
static void print_line_with_lcs(const char *class, const char *line, const char *lcs)
{
	int len = strlen(line);
	int in_common = 1;
	int matched = 0;
	int i;
	struct strbuf run = STRBUF_INIT;

	for (i = 0; i < len; i++) {
		if (in_common) {
			if (line[i] == lcs[matched]) {
				matched++;
			} else {
				in_common = 0;
				flush_run(&run);
				htmlf("<span class='%s'>", class);
			}
		} else if (line[i] == lcs[matched]) {
			in_common = 1;
			flush_run(&run);
			html("</span>");
			matched++;
		}
		strbuf_addch(&run, line[i]);
	}
	flush_run(&run);
	if (!in_common)
		html("</span>");
	strbuf_release(&run);
}

static void print_lineno_cell(struct diff_filespec *file, const struct object_id *rev, int line_no)
{
	struct strbuf path = STRBUF_INIT;
	char *anchor, *query, *fileurl;
	const char *rev_hex;

	anchor = cgit_fmt("n%d", line_no);
	rev_hex = is_null_oid(&file->oid) ? "HEAD" : oid_to_hex(rev);
	query = cgit_fmt("id=%s#%s", rev_hex, anchor);
	// The path is repository content, so percent-encode it so that the url
	// stays a url whatever bytes the path holds.
	if (file->path)
		strbuf_add_percentencode(&path, file->path, 0);
	fileurl = cgit_fileurl(ctx.repo->url, "tree", path.buf, query);
	html("<td class='lineno'><a href='");
	html_attr(fileurl);
	htmlf("'>%s</a>", anchor + 1);
	html("</td>");
	free(fileurl);
	strbuf_release(&path);
}

static void print_row(const char *class, int old_line_no, char *old_line,
	int new_line_no, char *new_line, int highlight_chars)
{
	char *lcs = NULL;

	// The first byte of a line is the marker xdiff put on it, not text.
	if (old_line)
		old_line = expand_tabs(old_line + 1);
	if (new_line)
		new_line = expand_tabs(new_line + 1);
	if (highlight_chars && old_line && new_line)
		lcs = longest_common_subsequence(old_line, new_line);
	html("<tr>\n");
	if (old_line_no > 0) {
		print_lineno_cell(cgit_get_current_old_file(), old_rev_oid, old_line_no);
		htmlf("<td class='%s'>", class);
	} else if (old_line) {
		htmlf("<td class='lineno'></td><td class='%s'>", class);
	} else {
		htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
	}
	if (old_line) {
		if (lcs)
			print_line_with_lcs("del", old_line, lcs);
		else
			html_txt(old_line);
	}

	html("</td>\n");
	if (new_line_no > 0) {
		print_lineno_cell(cgit_get_current_new_file(), new_rev_oid, new_line_no);
		htmlf("<td class='%s'>", class);
	} else if (new_line) {
		htmlf("<td class='lineno'></td><td class='%s'>", class);
	} else {
		htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
	}
	if (new_line) {
		if (lcs)
			print_line_with_lcs("add", new_line, lcs);
		else
			html_txt(new_line);
	}

	html("</td></tr>\n");
	free(lcs);
	free(new_line);
	free(old_line);
}

static void defer_line(struct deferred_line **head, struct deferred_line **last,
	const char *line, int line_no)
{
	struct deferred_line *item = xmalloc(sizeof(*item));

	item->line = xstrdup(line);
	item->line_no = line_no;
	item->next = NULL;
	if (*head)
		(*last)->next = item;
	else
		*head = item;
	*last = item;
}

/*
 * print_row frees only what it derives from the line it is handed and never
 * keeps the pointer itself, so the item can go as soon as its row is written.
 */
static void free_deferred(struct deferred_line *item)
{
	free(item->line);
	free(item);
}

static int count_deferred(struct deferred_line *item)
{
	int count = 0;

	while (item) {
		count++;
		item = item->next;
	}
	return count;
}

static void print_deferred_old_lines(void)
{
	struct deferred_line *item = deferred_old;
	struct deferred_line *next;

	while (item) {
		print_row("del", item->line_no, item->line, -1, NULL, 0);
		next = item->next;
		free_deferred(item);
		item = next;
	}
}

static void print_deferred_new_lines(void)
{
	struct deferred_line *item = deferred_new;
	struct deferred_line *next;

	while (item) {
		print_row("add", -1, NULL, item->line_no, item->line, 0);
		next = item->next;
		free_deferred(item);
		item = next;
	}
}

/*
 * Pairing a removal with an addition only stands for anything when the two
 * runs are the same length, which is why the character marking is offered only
 * then.
 */
static void print_deferred_changed_lines(void)
{
	struct deferred_line *old_item = deferred_old;
	struct deferred_line *new_item = deferred_new;
	struct deferred_line *next;
	int highlight_chars;

	highlight_chars = count_deferred(old_item) == count_deferred(new_item);
	while (old_item || new_item) {
		if (old_item && new_item)
			print_row("changed", old_item->line_no, old_item->line,
				new_item->line_no, new_item->line, highlight_chars);
		else if (old_item)
			print_row("changed", old_item->line_no, old_item->line, -1, NULL, 0);
		else if (new_item)
			print_row("changed", -1, NULL, new_item->line_no, new_item->line, 0);
		if (old_item) {
			next = old_item->next;
			free_deferred(old_item);
			old_item = next;
		}

		if (new_item) {
			next = new_item->next;
			free_deferred(new_item);
			new_item = next;
		}
	}
}

/*
 * git leaves the length out when a hunk covers a single line, as in
 * "@@ -1 +1 @@", so the number runs to whatever follows it rather than to a
 * comma that may belong to the other side or be missing altogether.
 */
static int hunk_start_line(const char *hunk, char marker)
{
	const char *p;
	long line_no;

	p = strchr(hunk, marker);
	if (!p)
		return 0;
	p++;
	line_no = strtol(p, NULL, 10);
	if (line_no < 0 || line_no > INT_MAX)
		return 0;
	return (int)line_no;
}

static void print_deferred_lines(void)
{
	if (!deferred_old && !deferred_new)
		return;
	if (deferred_old && !deferred_new)
		print_deferred_old_lines();
	else if (!deferred_old && deferred_new)
		print_deferred_new_lines();
	else
		print_deferred_changed_lines();
	deferred_old = deferred_old_last = NULL;
	deferred_new = deferred_new_last = NULL;
}

/*
 * The length counts the byte that ends the line, and the buffer belongs to the
 * caller, so that byte is only swapped for a NUL while the row is written and
 * is put back before returning.
 */
void cgit_ssdiff_line_cb(char *line, int len)
{
	char terminator = line[len - 1];

	line[len - 1] = '\0';
	if (line[0] == '@') {
		current_old_line = hunk_start_line(line, '-');
		current_new_line = hunk_start_line(line, '+');
	}

	if (line[0] == ' ') {
		print_deferred_lines();
		print_row("ctx", current_old_line, line, current_new_line, line, 0);
		current_old_line++;
		current_new_line++;
	} else if (line[0] == '+') {
		defer_line(&deferred_new, &deferred_new_last, line, current_new_line);
		current_new_line++;
	} else if (line[0] == '-') {
		defer_line(&deferred_old, &deferred_old_last, line, current_old_line);
		current_old_line++;
	} else if (line[0] == '@') {
		html("<tr><td colspan='4' class='hunk'>");
		html_txt(line);
		html("</td></tr>\n");
	} else {
		html("<tr><td colspan='4' class='ctx'>");
		html_txt(line);
		html("</td></tr>\n");
	}
	line[len - 1] = terminator;
}

void cgit_ssdiff_header_begin(void)
{
	current_old_line = -1;
	current_new_line = -1;
	html("<tr><td class='space' colspan='4'></td></tr>\n");
	html("<tr><td class='head' colspan='4'>");
}

void cgit_ssdiff_header_end(void)
{
	html("</td></tr>\n");
}

void cgit_ssdiff_footer(void)
{
	print_deferred_lines();
	html("<tr><td class='foot' colspan='4'></td></tr>\n");
}