1 /* ui-diff.c: show diff between two blobs
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
9 #define USE_THE_REPOSITORY_VARIABLE
14 #include "ui-shared.h"
15 #include "ui-ssdiff.h"
17 struct object_id old_rev_oid[1];
18 struct object_id new_rev_oid[1];
20 static int files, slots;
21 static int total_adds, total_rems, max_changes;
22 static int lines_added, lines_removed;
24 static struct fileinfo {
26 struct object_id old_oid[1];
27 struct object_id new_oid[1];
28 unsigned short old_mode;
29 unsigned short new_mode;
34 unsigned long old_size;
35 unsigned long new_size;
36 unsigned int binary:1;
39 static int use_ssdiff = 0;
40 static struct diff_filepair *current_filepair;
41 static const char *current_prefix;
43 struct diff_filespec *cgit_get_current_old_file(void)
45 return current_filepair->one;
48 struct diff_filespec *cgit_get_current_new_file(void)
50 return current_filepair->two;
53 static void print_fileinfo(struct fileinfo *info)
57 switch (info->status) {
58 case DIFF_STATUS_ADDED:
61 case DIFF_STATUS_COPIED:
64 case DIFF_STATUS_DELETED:
67 case DIFF_STATUS_MODIFIED:
70 case DIFF_STATUS_RENAMED:
73 case DIFF_STATUS_TYPE_CHANGED:
76 case DIFF_STATUS_UNKNOWN:
79 case DIFF_STATUS_UNMERGED:
83 die("bug: unhandled diff status %c", info->status);
87 html("<td class='mode'>");
88 if (is_null_oid(info->new_oid)) {
89 cgit_print_filemode(info->old_mode);
91 cgit_print_filemode(info->new_mode);
94 if (info->old_mode != info->new_mode &&
95 !is_null_oid(info->old_oid) &&
96 !is_null_oid(info->new_oid)) {
97 html("<span class='modechange'>[");
98 cgit_print_filemode(info->old_mode);
101 htmlf("</td><td class='%s'>", class);
102 cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.oid,
103 ctx.qry.oid2, info->new_path);
104 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) {
106 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed");
107 html_txt(info->old_path);
110 html("</td><td class='right'>");
112 htmlf("bin</td><td class='graph'>%ld -> %ld bytes",
113 info->old_size, info->new_size);
116 htmlf("%d", info->added + info->removed);
117 html("</td><td class='graph'>");
118 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
119 htmlf("<td class='add' style='width: %.1f%%;'/>",
120 info->added * 100.0 / max_changes);
121 htmlf("<td class='rem' style='width: %.1f%%;'/>",
122 info->removed * 100.0 / max_changes);
123 htmlf("<td class='none' style='width: %.1f%%;'/>",
124 (max_changes - info->removed - info->added) * 100.0 / max_changes);
125 html("</tr></table></td></tr>\n");
128 static void count_diff_lines(char *line, int len)
130 if (line && (len > 0)) {
133 else if (line[0] == '-')
138 static int show_filepair(struct diff_filepair *pair)
140 /* Always show if we have no limiting prefix. */
144 /* Show if either path in the pair begins with the prefix. */
145 if (starts_with(pair->one->path, current_prefix) ||
146 starts_with(pair->two->path, current_prefix))
149 /* Otherwise we don't want to show this filepair. */
153 static void inspect_filepair(struct diff_filepair *pair)
156 unsigned long old_size = 0;
157 unsigned long new_size = 0;
159 if (!show_filepair(pair))
165 cgit_diff_files(&pair->one->oid, &pair->two->oid, &old_size, &new_size,
166 &binary, 0, ctx.qry.ignorews, count_diff_lines);
167 if (files >= slots) {
172 items = xrealloc(items, slots * sizeof(struct fileinfo));
174 items[files-1].status = pair->status;
175 oidcpy(items[files-1].old_oid, &pair->one->oid);
176 oidcpy(items[files-1].new_oid, &pair->two->oid);
177 items[files-1].old_mode = pair->one->mode;
178 items[files-1].new_mode = pair->two->mode;
179 items[files-1].old_path = xstrdup(pair->one->path);
180 items[files-1].new_path = xstrdup(pair->two->path);
181 items[files-1].added = lines_added;
182 items[files-1].removed = lines_removed;
183 items[files-1].old_size = old_size;
184 items[files-1].new_size = new_size;
185 items[files-1].binary = binary;
186 if (lines_added + lines_removed > max_changes)
187 max_changes = lines_added + lines_removed;
188 total_adds += lines_added;
189 total_rems += lines_removed;
192 static void cgit_print_diffstat(const struct object_id *old_oid,
193 const struct object_id *new_oid,
198 html("<div class='diffstat-header'>");
199 cgit_diff_link("Diffstat", NULL, NULL, ctx.qry.head, ctx.qry.oid,
202 html(" (limited to '");
207 html("<table summary='diffstat' class='diffstat'>");
209 cgit_diff_tree(old_oid, new_oid, inspect_filepair, prefix,
211 for (i = 0; i<files; i++)
212 print_fileinfo(&items[i]);
214 html("<div class='diffstat-summary'>");
215 htmlf("%d files changed, %d insertions, %d deletions",
216 files, total_adds, total_rems);
222 * print a single line returned from xdiff
224 static void print_line(char *line, int len)
227 char c = line[len-1];
231 else if (line[0] == '-')
233 else if (line[0] == '@')
236 htmlf("<div class='%s'>", class);
243 static void header(const struct object_id *oid1, char *path1, int mode1,
244 const struct object_id *oid2, char *path2, int mode2)
246 char *abbrev1, *abbrev2;
249 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
250 html("<div class='head'>");
251 html("diff --git a/");
257 htmlf("<br/>new file mode %.6o", mode2);
260 htmlf("<br/>deleted file mode %.6o", mode1);
263 abbrev1 = xstrdup(repo_find_unique_abbrev(the_repository, oid1, DEFAULT_ABBREV));
264 abbrev2 = xstrdup(repo_find_unique_abbrev(the_repository, oid2, DEFAULT_ABBREV));
265 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
268 if (mode1 != 0 && mode2 != 0) {
269 htmlf(" %.6o", mode1);
271 htmlf("..%.6o", mode2);
273 if (is_null_oid(oid1)) {
279 cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
280 oid_to_hex(old_rev_oid), path1);
283 if (is_null_oid(oid2)) {
289 cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
290 oid_to_hex(new_rev_oid), path2);
297 static void filepair_cb(struct diff_filepair *pair)
299 unsigned long old_size = 0;
300 unsigned long new_size = 0;
302 linediff_fn print_line_fn = print_line;
304 if (!show_filepair(pair))
307 current_filepair = pair;
309 cgit_ssdiff_header_begin();
310 print_line_fn = cgit_ssdiff_line_cb;
312 header(&pair->one->oid, pair->one->path, pair->one->mode,
313 &pair->two->oid, pair->two->path, pair->two->mode);
315 cgit_ssdiff_header_end();
316 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
317 if (S_ISGITLINK(pair->one->mode))
318 print_line_fn(fmt("-Subproject %s", oid_to_hex(&pair->one->oid)), 52);
319 if (S_ISGITLINK(pair->two->mode))
320 print_line_fn(fmt("+Subproject %s", oid_to_hex(&pair->two->oid)), 52);
322 cgit_ssdiff_footer();
325 if (cgit_diff_files(&pair->one->oid, &pair->two->oid, &old_size,
326 &new_size, &binary, ctx.qry.context,
327 ctx.qry.ignorews, print_line_fn))
328 cgit_print_error("Error running diff");
331 html("<tr><td colspan='4'>Binary files differ</td></tr>");
333 html("Binary files differ");
336 cgit_ssdiff_footer();
339 void cgit_print_diff_ctrls(void)
343 html("<div class='cgit-panel'>");
344 html("<b>diff options</b>");
345 html("<form method='get'>");
346 cgit_add_hidden_formfields(1, 0, ctx.qry.page);
348 html("<tr><td colspan='2'/></tr>");
350 html("<td class='label'>context:</td>");
351 html("<td class='ctrl'>");
352 html("<select name='context' onchange='this.form.submit();'>");
353 curr = ctx.qry.context;
356 for (i = 1; i <= 10; i++)
357 html_intoption(i, fmt("%d", i), curr);
358 for (i = 15; i <= 40; i += 5)
359 html_intoption(i, fmt("%d", i), curr);
363 html("<td class='label'>space:</td>");
364 html("<td class='ctrl'>");
365 html("<select name='ignorews' onchange='this.form.submit();'>");
366 html_intoption(0, "include", ctx.qry.ignorews);
367 html_intoption(1, "ignore", ctx.qry.ignorews);
371 html("<td class='label'>mode:</td>");
372 html("<td class='ctrl'>");
373 html("<select name='dt' onchange='this.form.submit();'>");
374 curr = ctx.qry.has_difftype ? ctx.qry.difftype : ctx.cfg.difftype;
375 html_intoption(0, "unified", curr);
376 html_intoption(1, "ssdiff", curr);
377 html_intoption(2, "stat only", curr);
378 html("</select></td></tr>");
379 html("<tr><td/><td class='ctrl'>");
380 html("<noscript><input type='submit' value='reload'/></noscript>");
381 html("</td></tr></table>");
386 void cgit_print_diff(const char *new_rev, const char *old_rev,
387 const char *prefix, int show_ctrls, int raw)
389 struct commit *commit, *commit2;
390 const struct object_id *old_tree_oid, *new_tree_oid;
394 * If "follow" is set then the diff machinery needs to examine the
395 * entire commit to detect renames so we must limit the paths in our
396 * own callbacks and not pass the prefix to the diff machinery.
398 if (ctx.qry.follow && ctx.cfg.enable_follow_links) {
399 current_prefix = prefix;
402 current_prefix = NULL;
406 new_rev = ctx.qry.head;
407 if (repo_get_oid(the_repository, new_rev, new_rev_oid)) {
408 cgit_print_error_page(404, "Not found",
409 "Bad object name: %s", new_rev);
412 commit = lookup_commit_reference(the_repository, new_rev_oid);
413 if (!commit || repo_parse_commit(the_repository, commit)) {
414 cgit_print_error_page(404, "Not found",
415 "Bad commit: %s", oid_to_hex(new_rev_oid));
418 new_tree_oid = get_commit_tree_oid(commit);
421 if (repo_get_oid(the_repository, old_rev, old_rev_oid)) {
422 cgit_print_error_page(404, "Not found",
423 "Bad object name: %s", old_rev);
426 } else if (commit->parents && commit->parents->item) {
427 oidcpy(old_rev_oid, &commit->parents->item->object.oid);
429 oidclr(old_rev_oid, the_repository->hash_algo);
432 if (!is_null_oid(old_rev_oid)) {
433 commit2 = lookup_commit_reference(the_repository, old_rev_oid);
434 if (!commit2 || repo_parse_commit(the_repository, commit2)) {
435 cgit_print_error_page(404, "Not found",
436 "Bad commit: %s", oid_to_hex(old_rev_oid));
439 old_tree_oid = get_commit_tree_oid(commit2);
445 struct diff_options diffopt;
447 repo_diff_setup(the_repository, &diffopt);
448 diffopt.output_format = DIFF_FORMAT_PATCH;
449 diffopt.flags.recursive = 1;
450 diff_setup_done(&diffopt);
452 ctx.page.mimetype = "text/plain";
453 cgit_print_http_headers();
455 diff_tree_oid(old_tree_oid, new_tree_oid, "",
458 diff_root_tree_oid(new_tree_oid, "", &diffopt);
460 diffcore_std(&diffopt);
461 diff_flush(&diffopt);
466 difftype = ctx.qry.has_difftype ? ctx.qry.difftype : ctx.cfg.difftype;
467 use_ssdiff = difftype == DIFF_SSDIFF;
470 cgit_print_layout_start();
471 cgit_print_diff_ctrls();
475 * Clicking on a link to a file in the diff stat should show a diff
476 * of the file, showing the diff stat limited to a single file is
477 * pretty useless. All links from this point on will be to
478 * individual files, so we simply reset the difftype in the query
479 * here to avoid propagating DIFF_STATONLY to the individual files.
481 if (difftype == DIFF_STATONLY)
482 ctx.qry.difftype = ctx.cfg.difftype;
484 cgit_print_diffstat(old_rev_oid, new_rev_oid, prefix);
486 if (difftype == DIFF_STATONLY)
490 html("<table summary='ssdiff' class='ssdiff'>");
492 html("<table summary='diff' class='diff'>");
495 cgit_diff_tree(old_rev_oid, new_rev_oid, filepair_cb, prefix,
502 cgit_print_layout_end();