1 /* ui-diff.c: show diff between two blobs
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 #include "ui-shared.h"
13 #include "ui-ssdiff.h"
15 unsigned char old_rev_sha1[20];
16 unsigned char new_rev_sha1[20];
18 static int files, slots;
19 static int total_adds, total_rems, max_changes;
20 static int lines_added, lines_removed;
22 static struct fileinfo {
24 unsigned char old_sha1[20];
25 unsigned char new_sha1[20];
26 unsigned short old_mode;
27 unsigned short new_mode;
32 unsigned long old_size;
33 unsigned long new_size;
37 static int use_ssdiff = 0;
38 static struct diff_filepair *current_filepair;
40 struct diff_filespec *cgit_get_current_old_file(void)
42 return current_filepair->one;
45 struct diff_filespec *cgit_get_current_new_file(void)
47 return current_filepair->two;
50 static void print_fileinfo(struct fileinfo *info)
54 switch (info->status) {
55 case DIFF_STATUS_ADDED:
58 case DIFF_STATUS_COPIED:
61 case DIFF_STATUS_DELETED:
64 case DIFF_STATUS_MODIFIED:
67 case DIFF_STATUS_RENAMED:
70 case DIFF_STATUS_TYPE_CHANGED:
73 case DIFF_STATUS_UNKNOWN:
76 case DIFF_STATUS_UNMERGED:
80 die("bug: unhandled diff status %c", info->status);
84 htmlf("<td class='mode'>");
85 if (is_null_sha1(info->new_sha1)) {
86 cgit_print_filemode(info->old_mode);
88 cgit_print_filemode(info->new_mode);
91 if (info->old_mode != info->new_mode &&
92 !is_null_sha1(info->old_sha1) &&
93 !is_null_sha1(info->new_sha1)) {
94 html("<span class='modechange'>[");
95 cgit_print_filemode(info->old_mode);
98 htmlf("</td><td class='%s'>", class);
99 cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
100 ctx.qry.sha2, info->new_path, 0);
101 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) {
103 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed");
104 html_txt(info->old_path);
107 html("</td><td class='right'>");
109 htmlf("bin</td><td class='graph'>%ld -> %ld bytes",
110 info->old_size, info->new_size);
113 htmlf("%d", info->added + info->removed);
114 html("</td><td class='graph'>");
115 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
116 htmlf("<td class='add' style='width: %.1f%%;'/>",
117 info->added * 100.0 / max_changes);
118 htmlf("<td class='rem' style='width: %.1f%%;'/>",
119 info->removed * 100.0 / max_changes);
120 htmlf("<td class='none' style='width: %.1f%%;'/>",
121 (max_changes - info->removed - info->added) * 100.0 / max_changes);
122 html("</tr></table></td></tr>\n");
125 static void count_diff_lines(char *line, int len)
127 if (line && (len > 0)) {
130 else if (line[0] == '-')
135 static void inspect_filepair(struct diff_filepair *pair)
138 unsigned long old_size = 0;
139 unsigned long new_size = 0;
143 cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, &new_size,
144 &binary, 0, ctx.qry.ignorews, count_diff_lines);
145 if (files >= slots) {
150 items = xrealloc(items, slots * sizeof(struct fileinfo));
152 items[files-1].status = pair->status;
153 hashcpy(items[files-1].old_sha1, pair->one->sha1);
154 hashcpy(items[files-1].new_sha1, pair->two->sha1);
155 items[files-1].old_mode = pair->one->mode;
156 items[files-1].new_mode = pair->two->mode;
157 items[files-1].old_path = xstrdup(pair->one->path);
158 items[files-1].new_path = xstrdup(pair->two->path);
159 items[files-1].added = lines_added;
160 items[files-1].removed = lines_removed;
161 items[files-1].old_size = old_size;
162 items[files-1].new_size = new_size;
163 items[files-1].binary = binary;
164 if (lines_added + lines_removed > max_changes)
165 max_changes = lines_added + lines_removed;
166 total_adds += lines_added;
167 total_rems += lines_removed;
170 static void cgit_print_diffstat(const unsigned char *old_sha1,
171 const unsigned char *new_sha1,
176 html("<div class='diffstat-header'>");
177 cgit_diff_link("Diffstat", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
178 ctx.qry.sha2, NULL, 0);
180 html(" (limited to '");
185 html("<table summary='diffstat' class='diffstat'>");
187 cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, prefix,
189 for (i = 0; i<files; i++)
190 print_fileinfo(&items[i]);
192 html("<div class='diffstat-summary'>");
193 htmlf("%d files changed, %d insertions, %d deletions",
194 files, total_adds, total_rems);
200 * print a single line returned from xdiff
202 static void print_line(char *line, int len)
205 char c = line[len-1];
209 else if (line[0] == '-')
211 else if (line[0] == '@')
214 htmlf("<div class='%s'>", class);
221 static void header(unsigned char *sha1, char *path1, int mode1,
222 unsigned char *sha2, char *path2, int mode2)
224 char *abbrev1, *abbrev2;
227 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
228 html("<div class='head'>");
229 html("diff --git a/");
235 htmlf("<br/>new file mode %.6o", mode2);
238 htmlf("<br/>deleted file mode %.6o", mode1);
241 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
242 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
243 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
246 if (mode1 != 0 && mode2 != 0) {
247 htmlf(" %.6o", mode1);
249 htmlf("..%.6o", mode2);
251 if (is_null_sha1(sha1)) {
257 cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
258 sha1_to_hex(old_rev_sha1), path1);
261 if (is_null_sha1(sha2)) {
267 cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
268 sha1_to_hex(new_rev_sha1), path2);
275 static void filepair_cb(struct diff_filepair *pair)
277 unsigned long old_size = 0;
278 unsigned long new_size = 0;
280 linediff_fn print_line_fn = print_line;
282 current_filepair = pair;
284 cgit_ssdiff_header_begin();
285 print_line_fn = cgit_ssdiff_line_cb;
287 header(pair->one->sha1, pair->one->path, pair->one->mode,
288 pair->two->sha1, pair->two->path, pair->two->mode);
290 cgit_ssdiff_header_end();
291 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
292 if (S_ISGITLINK(pair->one->mode))
293 print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
294 if (S_ISGITLINK(pair->two->mode))
295 print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
297 cgit_ssdiff_footer();
300 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
301 &new_size, &binary, ctx.qry.context,
302 ctx.qry.ignorews, print_line_fn))
303 cgit_print_error("Error running diff");
306 html("<tr><td colspan='4'>Binary files differ</td></tr>");
308 html("Binary files differ");
311 cgit_ssdiff_footer();
314 void cgit_print_diff_ctrls()
318 html("<div class='cgit-panel'>");
319 html("<b>diff options</b>");
320 html("<form method='get' action='.'>");
321 cgit_add_hidden_formfields(1, 0, ctx.qry.page);
323 html("<tr><td colspan='2'/></tr>");
325 html("<td class='label'>context:</td>");
326 html("<td class='ctrl'>");
327 html("<select name='context' onchange='this.form.submit();'>");
328 curr = ctx.qry.context;
331 for (i = 1; i <= 10; i++)
332 html_intoption(i, fmt("%d", i), curr);
333 for (i = 15; i <= 40; i += 5)
334 html_intoption(i, fmt("%d", i), curr);
338 html("<td class='label'>space:</td>");
339 html("<td class='ctrl'>");
340 html("<select name='ignorews' onchange='this.form.submit();'>");
341 html_intoption(0, "include", ctx.qry.ignorews);
342 html_intoption(1, "ignore", ctx.qry.ignorews);
346 html("<td class='label'>mode:</td>");
347 html("<td class='ctrl'>");
348 html("<select name='ss' onchange='this.form.submit();'>");
349 curr = ctx.qry.has_ssdiff ? ctx.qry.ssdiff : ctx.cfg.ssdiff;
350 html_intoption(0, "unified", curr);
351 html_intoption(1, "ssdiff", curr);
352 html("</select></td></tr>");
353 html("<tr><td/><td class='ctrl'>");
354 html("<noscript><input type='submit' value='reload'/></noscript>");
355 html("</td></tr></table>");
360 void cgit_print_diff(const char *new_rev, const char *old_rev,
361 const char *prefix, int show_ctrls)
363 enum object_type type;
365 struct commit *commit, *commit2;
368 new_rev = ctx.qry.head;
369 get_sha1(new_rev, new_rev_sha1);
370 type = sha1_object_info(new_rev_sha1, &size);
371 if (type == OBJ_BAD) {
372 cgit_print_error("Bad object name: %s", new_rev);
375 commit = lookup_commit_reference(new_rev_sha1);
376 if (!commit || parse_commit(commit)) {
377 cgit_print_error("Bad commit: %s", sha1_to_hex(new_rev_sha1));
382 get_sha1(old_rev, old_rev_sha1);
383 else if (commit->parents && commit->parents->item)
384 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
386 hashclr(old_rev_sha1);
388 if (!is_null_sha1(old_rev_sha1)) {
389 type = sha1_object_info(old_rev_sha1, &size);
390 if (type == OBJ_BAD) {
391 cgit_print_error("Bad object name: %s", sha1_to_hex(old_rev_sha1));
394 commit2 = lookup_commit_reference(old_rev_sha1);
395 if (!commit2 || parse_commit(commit2)) {
396 cgit_print_error("Bad commit: %s", sha1_to_hex(old_rev_sha1));
401 use_ssdiff = ctx.qry.has_ssdiff ? ctx.qry.ssdiff : ctx.cfg.ssdiff;
404 cgit_print_diff_ctrls();
406 cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix);
409 html("<table summary='ssdiff' class='ssdiff'>");
411 html("<table summary='diff' class='diff'>");
414 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix,