blob: 7de7802bad6e87f0553d2b4d81dd264d5a3ca8f9 [file] [log] [blame]
Lars Hjemli36aba002006-12-20 22:48:27 +01001/* ui-diff.c: show diff between two blobs
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010010#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010011#include "ui-shared.h"
Ragnar Ouchterlony40e174d2009-09-13 19:36:35 +020012#include "ui-ssdiff.h"
Lars Hjemli36aba002006-12-20 22:48:27 +010013
Lars Hjemlie238ebe2007-10-01 12:30:29 +020014unsigned char old_rev_sha1[20];
15unsigned char new_rev_sha1[20];
16
Lars Hjemlife1230d2008-04-24 23:32:02 +020017static int files, slots;
18static int total_adds, total_rems, max_changes;
19static int lines_added, lines_removed;
Lars Hjemlife1230d2008-04-24 23:32:02 +020020
21static struct fileinfo {
22 char status;
23 unsigned char old_sha1[20];
24 unsigned char new_sha1[20];
25 unsigned short old_mode;
26 unsigned short new_mode;
27 char *old_path;
28 char *new_path;
29 unsigned int added;
30 unsigned int removed;
Lars Hjemlic495cf02009-01-31 10:40:40 +010031 unsigned long old_size;
32 unsigned long new_size;
33 int binary:1;
Lars Hjemlife1230d2008-04-24 23:32:02 +020034} *items;
35
Ragnar Ouchterlony40e174d2009-09-13 19:36:35 +020036static int use_ssdiff = 0;
Bernhard Reutner-Fischere52040b2010-12-23 12:47:55 +010037static struct diff_filepair *current_filepair;
38
39struct diff_filespec *cgit_get_current_old_file(void)
40{
41 return current_filepair->one;
42}
43
44struct diff_filespec *cgit_get_current_new_file(void)
45{
46 return current_filepair->two;
47}
Lars Hjemlife1230d2008-04-24 23:32:02 +020048
49static void print_fileinfo(struct fileinfo *info)
50{
51 char *class;
52
53 switch (info->status) {
54 case DIFF_STATUS_ADDED:
55 class = "add";
56 break;
57 case DIFF_STATUS_COPIED:
58 class = "cpy";
59 break;
60 case DIFF_STATUS_DELETED:
61 class = "del";
62 break;
63 case DIFF_STATUS_MODIFIED:
64 class = "upd";
65 break;
66 case DIFF_STATUS_RENAMED:
67 class = "mov";
68 break;
69 case DIFF_STATUS_TYPE_CHANGED:
70 class = "typ";
71 break;
72 case DIFF_STATUS_UNKNOWN:
73 class = "unk";
74 break;
75 case DIFF_STATUS_UNMERGED:
76 class = "stg";
77 break;
78 default:
79 die("bug: unhandled diff status %c", info->status);
80 }
81
82 html("<tr>");
83 htmlf("<td class='mode'>");
84 if (is_null_sha1(info->new_sha1)) {
85 cgit_print_filemode(info->old_mode);
86 } else {
87 cgit_print_filemode(info->new_mode);
88 }
89
90 if (info->old_mode != info->new_mode &&
91 !is_null_sha1(info->old_sha1) &&
92 !is_null_sha1(info->new_sha1)) {
93 html("<span class='modechange'>[");
94 cgit_print_filemode(info->old_mode);
95 html("]</span>");
96 }
97 htmlf("</td><td class='%s'>", class);
Lars Hjemli04619c92008-09-23 17:47:26 +020098 cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +020099 ctx.qry.sha2, info->new_path, 0);
Lukas Fleischerbebe89d2011-07-22 13:47:19 +0200100 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) {
101 htmlf(" (%s from ",
102 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed");
103 html_txt(info->old_path);
104 html(")");
105 }
Lars Hjemlife1230d2008-04-24 23:32:02 +0200106 html("</td><td class='right'>");
Lars Hjemlic495cf02009-01-31 10:40:40 +0100107 if (info->binary) {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -0400108 htmlf("bin</td><td class='graph'>%ld -> %ld bytes",
Lars Hjemlic495cf02009-01-31 10:40:40 +0100109 info->old_size, info->new_size);
110 return;
111 }
Lars Hjemlife1230d2008-04-24 23:32:02 +0200112 htmlf("%d", info->added + info->removed);
113 html("</td><td class='graph'>");
114 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
115 htmlf("<td class='add' style='width: %.1f%%;'/>",
116 info->added * 100.0 / max_changes);
117 htmlf("<td class='rem' style='width: %.1f%%;'/>",
118 info->removed * 100.0 / max_changes);
119 htmlf("<td class='none' style='width: %.1f%%;'/>",
120 (max_changes - info->removed - info->added) * 100.0 / max_changes);
121 html("</tr></table></td></tr>\n");
122}
123
124static void count_diff_lines(char *line, int len)
125{
126 if (line && (len > 0)) {
127 if (line[0] == '+')
128 lines_added++;
129 else if (line[0] == '-')
130 lines_removed++;
131 }
132}
133
134static void inspect_filepair(struct diff_filepair *pair)
135{
Lars Hjemlic495cf02009-01-31 10:40:40 +0100136 int binary = 0;
137 unsigned long old_size = 0;
138 unsigned long new_size = 0;
Lars Hjemlife1230d2008-04-24 23:32:02 +0200139 files++;
140 lines_added = 0;
141 lines_removed = 0;
Lars Hjemlic495cf02009-01-31 10:40:40 +0100142 cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, &new_size,
Johan Herland2cc8b992010-06-24 17:52:57 +0200143 &binary, 0, ctx.qry.ignorews, count_diff_lines);
Lars Hjemlife1230d2008-04-24 23:32:02 +0200144 if (files >= slots) {
145 if (slots == 0)
146 slots = 4;
147 else
148 slots = slots * 2;
149 items = xrealloc(items, slots * sizeof(struct fileinfo));
150 }
151 items[files-1].status = pair->status;
152 hashcpy(items[files-1].old_sha1, pair->one->sha1);
153 hashcpy(items[files-1].new_sha1, pair->two->sha1);
154 items[files-1].old_mode = pair->one->mode;
155 items[files-1].new_mode = pair->two->mode;
156 items[files-1].old_path = xstrdup(pair->one->path);
157 items[files-1].new_path = xstrdup(pair->two->path);
158 items[files-1].added = lines_added;
159 items[files-1].removed = lines_removed;
Lars Hjemlic495cf02009-01-31 10:40:40 +0100160 items[files-1].old_size = old_size;
161 items[files-1].new_size = new_size;
162 items[files-1].binary = binary;
Lars Hjemlife1230d2008-04-24 23:32:02 +0200163 if (lines_added + lines_removed > max_changes)
164 max_changes = lines_added + lines_removed;
165 total_adds += lines_added;
166 total_rems += lines_removed;
167}
168
Lukas Fleischerbafab422013-03-04 08:52:33 +0100169static void cgit_print_diffstat(const unsigned char *old_sha1,
170 const unsigned char *new_sha1,
171 const char *prefix)
Lars Hjemlife1230d2008-04-24 23:32:02 +0200172{
Lars Hjemli962a2482011-03-06 23:59:56 +0100173 int i;
Lars Hjemlife1230d2008-04-24 23:32:02 +0200174
Lars Hjemlif82b1942008-09-23 17:54:45 +0200175 html("<div class='diffstat-header'>");
176 cgit_diff_link("Diffstat", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200177 ctx.qry.sha2, NULL, 0);
Lukasz Janyst7f3c6e02011-03-05 14:10:55 +0100178 if (prefix) {
179 html(" (limited to '");
180 html_txt(prefix);
181 html("')");
182 }
Lars Hjemlif82b1942008-09-23 17:54:45 +0200183 html("</div>");
Lars Hjemlife1230d2008-04-24 23:32:02 +0200184 html("<table summary='diffstat' class='diffstat'>");
185 max_changes = 0;
Johan Herland2cc8b992010-06-24 17:52:57 +0200186 cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, prefix,
187 ctx.qry.ignorews);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500188 for (i = 0; i<files; i++)
Lars Hjemlife1230d2008-04-24 23:32:02 +0200189 print_fileinfo(&items[i]);
190 html("</table>");
191 html("<div class='diffstat-summary'>");
192 htmlf("%d files changed, %d insertions, %d deletions",
193 files, total_adds, total_rems);
194 html("</div>");
195}
196
197
Lars Hjemli36aba002006-12-20 22:48:27 +0100198/*
199 * print a single line returned from xdiff
200 */
201static void print_line(char *line, int len)
202{
203 char *class = "ctx";
204 char c = line[len-1];
205
206 if (line[0] == '+')
207 class = "add";
208 else if (line[0] == '-')
209 class = "del";
210 else if (line[0] == '@')
211 class = "hunk";
212
213 htmlf("<div class='%s'>", class);
214 line[len-1] = '\0';
215 html_txt(line);
216 html("</div>");
217 line[len-1] = c;
218}
219
Lars Hjemlif4f13392007-05-16 04:21:06 +0200220static void header(unsigned char *sha1, char *path1, int mode1,
221 unsigned char *sha2, char *path2, int mode2)
Lars Hjemlia342ac62007-05-14 18:31:05 +0200222{
223 char *abbrev1, *abbrev2;
Lars Hjemlif4f13392007-05-16 04:21:06 +0200224 int subproject;
225
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -0500226 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
Lars Hjemlia342ac62007-05-14 18:31:05 +0200227 html("<div class='head'>");
228 html("diff --git a/");
229 html_txt(path1);
230 html(" b/");
231 html_txt(path2);
Lars Hjemlif4f13392007-05-16 04:21:06 +0200232
Lars Hjemlif4f13392007-05-16 04:21:06 +0200233 if (mode1 == 0)
234 htmlf("<br/>new file mode %.6o", mode2);
235
236 if (mode2 == 0)
237 htmlf("<br/>deleted file mode %.6o", mode1);
238
239 if (!subproject) {
240 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
241 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
242 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
243 free(abbrev1);
244 free(abbrev2);
245 if (mode1 != 0 && mode2 != 0) {
246 htmlf(" %.6o", mode1);
247 if (mode2 != mode1)
248 htmlf("..%.6o", mode2);
249 }
Michael Halstead62a40c72012-11-14 12:41:01 -0800250 if (is_null_sha1(sha1)) {
251 path1 = "dev/null";
252 html("<br/>--- /");
253 } else
254 html("<br/>--- a/");
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200255 if (mode1 != 0)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100256 cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200257 sha1_to_hex(old_rev_sha1), path1);
258 else
259 html_txt(path1);
Michael Halstead62a40c72012-11-14 12:41:01 -0800260 if (is_null_sha1(sha2)) {
261 path2 = "dev/null";
262 html("<br/>+++ /");
263 } else
264 html("<br/>+++ b/");
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200265 if (mode2 != 0)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100266 cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200267 sha1_to_hex(new_rev_sha1), path2);
268 else
269 html_txt(path2);
Lars Hjemlif4f13392007-05-16 04:21:06 +0200270 }
Lars Hjemlia342ac62007-05-14 18:31:05 +0200271 html("</div>");
272}
273
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200274static void filepair_cb(struct diff_filepair *pair)
275{
Lars Hjemlic495cf02009-01-31 10:40:40 +0100276 unsigned long old_size = 0;
277 unsigned long new_size = 0;
278 int binary = 0;
Ragnar Ouchterlony40e174d2009-09-13 19:36:35 +0200279 linediff_fn print_line_fn = print_line;
Lars Hjemlic495cf02009-01-31 10:40:40 +0100280
Bernhard Reutner-Fischere52040b2010-12-23 12:47:55 +0100281 current_filepair = pair;
Ragnar Ouchterlony40e174d2009-09-13 19:36:35 +0200282 if (use_ssdiff) {
Ragnar Ouchterlony207cc342009-09-15 19:44:37 +0200283 cgit_ssdiff_header_begin();
Ragnar Ouchterlony40e174d2009-09-13 19:36:35 +0200284 print_line_fn = cgit_ssdiff_line_cb;
285 }
Ragnar Ouchterlony207cc342009-09-15 19:44:37 +0200286 header(pair->one->sha1, pair->one->path, pair->one->mode,
287 pair->two->sha1, pair->two->path, pair->two->mode);
288 if (use_ssdiff)
289 cgit_ssdiff_header_end();
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -0500290 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
291 if (S_ISGITLINK(pair->one->mode))
Ragnar Ouchterlony207cc342009-09-15 19:44:37 +0200292 print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -0500293 if (S_ISGITLINK(pair->two->mode))
Ragnar Ouchterlony207cc342009-09-15 19:44:37 +0200294 print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
Ragnar Ouchterlony4a198e42009-09-16 18:56:26 +0200295 if (use_ssdiff)
296 cgit_ssdiff_footer();
Lars Hjemlif4f13392007-05-16 04:21:06 +0200297 return;
298 }
Ragnar Ouchterlony40e174d2009-09-13 19:36:35 +0200299 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
Johan Herland2cc8b992010-06-24 17:52:57 +0200300 &new_size, &binary, ctx.qry.context,
301 ctx.qry.ignorews, print_line_fn))
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200302 cgit_print_error("Error running diff");
Ragnar Ouchterlony4a198e42009-09-16 18:56:26 +0200303 if (binary) {
304 if (use_ssdiff)
305 html("<tr><td colspan='4'>Binary files differ</td></tr>");
306 else
307 html("Binary files differ");
308 }
Ragnar Ouchterlony40e174d2009-09-13 19:36:35 +0200309 if (use_ssdiff)
310 cgit_ssdiff_footer();
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200311}
312
Lars Hjemli962a2482011-03-06 23:59:56 +0100313void cgit_print_diff_ctrls()
314{
315 int i, curr;
316
317 html("<div class='cgit-panel'>");
318 html("<b>diff options</b>");
319 html("<form method='get' action='.'>");
320 cgit_add_hidden_formfields(1, 0, ctx.qry.page);
321 html("<table>");
322 html("<tr><td colspan='2'/></tr>");
323 html("<tr>");
324 html("<td class='label'>context:</td>");
325 html("<td class='ctrl'>");
326 html("<select name='context' onchange='this.form.submit();'>");
327 curr = ctx.qry.context;
328 if (!curr)
329 curr = 3;
330 for (i = 1; i <= 10; i++)
331 html_intoption(i, fmt("%d", i), curr);
332 for (i = 15; i <= 40; i += 5)
333 html_intoption(i, fmt("%d", i), curr);
334 html("</select>");
335 html("</td>");
336 html("</tr><tr>");
337 html("<td class='label'>space:</td>");
338 html("<td class='ctrl'>");
339 html("<select name='ignorews' onchange='this.form.submit();'>");
340 html_intoption(0, "include", ctx.qry.ignorews);
341 html_intoption(1, "ignore", ctx.qry.ignorews);
342 html("</select>");
343 html("</td>");
344 html("</tr><tr>");
345 html("<td class='label'>mode:</td>");
346 html("<td class='ctrl'>");
347 html("<select name='ss' onchange='this.form.submit();'>");
Tim Chen19574252012-01-03 16:09:59 +0000348 curr = ctx.qry.has_ssdiff ? ctx.qry.ssdiff : ctx.cfg.ssdiff;
Lars Hjemli962a2482011-03-06 23:59:56 +0100349 html_intoption(0, "unified", curr);
350 html_intoption(1, "ssdiff", curr);
351 html("</select></td></tr>");
352 html("<tr><td/><td class='ctrl'>");
353 html("<noscript><input type='submit' value='reload'/></noscript>");
354 html("</td></tr></table>");
355 html("</form>");
356 html("</div>");
357}
358
359void cgit_print_diff(const char *new_rev, const char *old_rev,
360 const char *prefix, int show_ctrls)
Lars Hjemli36aba002006-12-20 22:48:27 +0100361{
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200362 enum object_type type;
363 unsigned long size;
Lars Hjemli4a0be582007-06-17 18:12:03 +0200364 struct commit *commit, *commit2;
Lars Hjemlif9ff7df2007-05-16 00:58:35 +0200365
Lars Hjemli4a0be582007-06-17 18:12:03 +0200366 if (!new_rev)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100367 new_rev = ctx.qry.head;
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200368 get_sha1(new_rev, new_rev_sha1);
369 type = sha1_object_info(new_rev_sha1, &size);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200370 if (type == OBJ_BAD) {
371 cgit_print_error(fmt("Bad object name: %s", new_rev));
372 return;
373 }
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200374 commit = lookup_commit_reference(new_rev_sha1);
Lukas Fleischer9afc8832011-04-05 10:38:53 +0200375 if (!commit || parse_commit(commit)) {
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200376 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
Lukas Fleischer9afc8832011-04-05 10:38:53 +0200377 return;
378 }
Lars Hjemli36aba002006-12-20 22:48:27 +0100379
Lars Hjemli4a0be582007-06-17 18:12:03 +0200380 if (old_rev)
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200381 get_sha1(old_rev, old_rev_sha1);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200382 else if (commit->parents && commit->parents->item)
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200383 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200384 else
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200385 hashclr(old_rev_sha1);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200386
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200387 if (!is_null_sha1(old_rev_sha1)) {
388 type = sha1_object_info(old_rev_sha1, &size);
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200389 if (type == OBJ_BAD) {
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200390 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200391 return;
392 }
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200393 commit2 = lookup_commit_reference(old_rev_sha1);
Lukas Fleischer9afc8832011-04-05 10:38:53 +0200394 if (!commit2 || parse_commit(commit2)) {
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200395 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
Lukas Fleischer9afc8832011-04-05 10:38:53 +0200396 return;
397 }
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200398 }
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200399
Tim Chenf2ced532012-01-03 16:02:14 +0000400 use_ssdiff = ctx.qry.has_ssdiff ? ctx.qry.ssdiff : ctx.cfg.ssdiff;
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200401
Lars Hjemli962a2482011-03-06 23:59:56 +0100402 if (show_ctrls)
403 cgit_print_diff_ctrls();
404
Johan Herlandc46e4682010-06-10 01:09:31 +0200405 cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix);
Lars Hjemlife1230d2008-04-24 23:32:02 +0200406
Ragnar Ouchterlony207cc342009-09-15 19:44:37 +0200407 if (use_ssdiff) {
408 html("<table summary='ssdiff' class='ssdiff'>");
409 } else {
410 html("<table summary='diff' class='diff'>");
411 html("<tr><td>");
412 }
Johan Herland2cc8b992010-06-24 17:52:57 +0200413 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix,
414 ctx.qry.ignorews);
Ragnar Ouchterlony207cc342009-09-15 19:44:37 +0200415 if (!use_ssdiff)
416 html("</td></tr>");
Ondrej Jirman0928d882007-05-26 01:14:25 +0200417 html("</table>");
Lars Hjemli36aba002006-12-20 22:48:27 +0100418}