ui-diff.c (view raw)
1/* 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"
10#include "html.h"
11#include "ui-shared.h"
12#include "ui-ssdiff.h"
13
14unsigned char old_rev_sha1[20];
15unsigned char new_rev_sha1[20];
16
17static int files, slots;
18static int total_adds, total_rems, max_changes;
19static int lines_added, lines_removed;
20
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;
31 unsigned long old_size;
32 unsigned long new_size;
33 int binary:1;
34} *items;
35
36static int use_ssdiff = 0;
37static 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}
48
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);
98 cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
99 ctx.qry.sha2, info->new_path, 0);
100 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED)
101 htmlf(" (%s from %s)",
102 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed",
103 info->old_path);
104 html("</td><td class='right'>");
105 if (info->binary) {
106 htmlf("bin</td><td class='graph'>%ld -> %ld bytes",
107 info->old_size, info->new_size);
108 return;
109 }
110 htmlf("%d", info->added + info->removed);
111 html("</td><td class='graph'>");
112 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
113 htmlf("<td class='add' style='width: %.1f%%;'/>",
114 info->added * 100.0 / max_changes);
115 htmlf("<td class='rem' style='width: %.1f%%;'/>",
116 info->removed * 100.0 / max_changes);
117 htmlf("<td class='none' style='width: %.1f%%;'/>",
118 (max_changes - info->removed - info->added) * 100.0 / max_changes);
119 html("</tr></table></td></tr>\n");
120}
121
122static void count_diff_lines(char *line, int len)
123{
124 if (line && (len > 0)) {
125 if (line[0] == '+')
126 lines_added++;
127 else if (line[0] == '-')
128 lines_removed++;
129 }
130}
131
132static void inspect_filepair(struct diff_filepair *pair)
133{
134 int binary = 0;
135 unsigned long old_size = 0;
136 unsigned long new_size = 0;
137 files++;
138 lines_added = 0;
139 lines_removed = 0;
140 cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, &new_size,
141 &binary, 0, ctx.qry.ignorews, count_diff_lines);
142 if (files >= slots) {
143 if (slots == 0)
144 slots = 4;
145 else
146 slots = slots * 2;
147 items = xrealloc(items, slots * sizeof(struct fileinfo));
148 }
149 items[files-1].status = pair->status;
150 hashcpy(items[files-1].old_sha1, pair->one->sha1);
151 hashcpy(items[files-1].new_sha1, pair->two->sha1);
152 items[files-1].old_mode = pair->one->mode;
153 items[files-1].new_mode = pair->two->mode;
154 items[files-1].old_path = xstrdup(pair->one->path);
155 items[files-1].new_path = xstrdup(pair->two->path);
156 items[files-1].added = lines_added;
157 items[files-1].removed = lines_removed;
158 items[files-1].old_size = old_size;
159 items[files-1].new_size = new_size;
160 items[files-1].binary = binary;
161 if (lines_added + lines_removed > max_changes)
162 max_changes = lines_added + lines_removed;
163 total_adds += lines_added;
164 total_rems += lines_removed;
165}
166
167void cgit_print_diffstat(const unsigned char *old_sha1,
168 const unsigned char *new_sha1, const char *prefix)
169{
170 int i, save_context = ctx.qry.context;
171
172 html("<div class='diffstat-header'>");
173 cgit_diff_link("Diffstat", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
174 ctx.qry.sha2, NULL, 0);
175 if (prefix)
176 htmlf(" (limited to '%s')", prefix);
177 html(" (");
178 ctx.qry.context = (save_context > 0 ? save_context : 3) << 1;
179 cgit_self_link("more", NULL, NULL, &ctx);
180 html("/");
181 ctx.qry.context = (save_context > 3 ? save_context : 3) >> 1;
182 cgit_self_link("less", NULL, NULL, &ctx);
183 ctx.qry.context = save_context;
184 html(" context)");
185 html(" (");
186 ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2;
187 cgit_self_link(ctx.qry.ignorews ? "ignore" : "show", NULL, NULL, &ctx);
188 ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2;
189 html(" whitespace changes)");
190 html("</div>");
191 html("<table summary='diffstat' class='diffstat'>");
192 max_changes = 0;
193 cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, prefix,
194 ctx.qry.ignorews);
195 for(i = 0; i<files; i++)
196 print_fileinfo(&items[i]);
197 html("</table>");
198 html("<div class='diffstat-summary'>");
199 htmlf("%d files changed, %d insertions, %d deletions",
200 files, total_adds, total_rems);
201 html("</div>");
202}
203
204
205/*
206 * print a single line returned from xdiff
207 */
208static void print_line(char *line, int len)
209{
210 char *class = "ctx";
211 char c = line[len-1];
212
213 if (line[0] == '+')
214 class = "add";
215 else if (line[0] == '-')
216 class = "del";
217 else if (line[0] == '@')
218 class = "hunk";
219
220 htmlf("<div class='%s'>", class);
221 line[len-1] = '\0';
222 html_txt(line);
223 html("</div>");
224 line[len-1] = c;
225}
226
227static void header(unsigned char *sha1, char *path1, int mode1,
228 unsigned char *sha2, char *path2, int mode2)
229{
230 char *abbrev1, *abbrev2;
231 int subproject;
232
233 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
234 html("<div class='head'>");
235 html("diff --git a/");
236 html_txt(path1);
237 html(" b/");
238 html_txt(path2);
239
240 if (is_null_sha1(sha1))
241 path1 = "dev/null";
242 if (is_null_sha1(sha2))
243 path2 = "dev/null";
244
245 if (mode1 == 0)
246 htmlf("<br/>new file mode %.6o", mode2);
247
248 if (mode2 == 0)
249 htmlf("<br/>deleted file mode %.6o", mode1);
250
251 if (!subproject) {
252 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
253 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
254 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
255 free(abbrev1);
256 free(abbrev2);
257 if (mode1 != 0 && mode2 != 0) {
258 htmlf(" %.6o", mode1);
259 if (mode2 != mode1)
260 htmlf("..%.6o", mode2);
261 }
262 html("<br/>--- a/");
263 if (mode1 != 0)
264 cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
265 sha1_to_hex(old_rev_sha1), path1);
266 else
267 html_txt(path1);
268 html("<br/>+++ b/");
269 if (mode2 != 0)
270 cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
271 sha1_to_hex(new_rev_sha1), path2);
272 else
273 html_txt(path2);
274 }
275 html("</div>");
276}
277
278static void print_ssdiff_link()
279{
280 if (!strcmp(ctx.qry.page, "diff")) {
281 if (use_ssdiff)
282 cgit_diff_link("Unidiff", NULL, NULL, ctx.qry.head,
283 ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1);
284 else
285 cgit_diff_link("Side-by-side diff", NULL, NULL,
286 ctx.qry.head, ctx.qry.sha1,
287 ctx.qry.sha2, ctx.qry.path, 1);
288 }
289}
290
291static void filepair_cb(struct diff_filepair *pair)
292{
293 unsigned long old_size = 0;
294 unsigned long new_size = 0;
295 int binary = 0;
296 linediff_fn print_line_fn = print_line;
297
298 current_filepair = pair;
299 if (use_ssdiff) {
300 cgit_ssdiff_header_begin();
301 print_line_fn = cgit_ssdiff_line_cb;
302 }
303 header(pair->one->sha1, pair->one->path, pair->one->mode,
304 pair->two->sha1, pair->two->path, pair->two->mode);
305 if (use_ssdiff)
306 cgit_ssdiff_header_end();
307 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
308 if (S_ISGITLINK(pair->one->mode))
309 print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
310 if (S_ISGITLINK(pair->two->mode))
311 print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
312 if (use_ssdiff)
313 cgit_ssdiff_footer();
314 return;
315 }
316 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
317 &new_size, &binary, ctx.qry.context,
318 ctx.qry.ignorews, print_line_fn))
319 cgit_print_error("Error running diff");
320 if (binary) {
321 if (use_ssdiff)
322 html("<tr><td colspan='4'>Binary files differ</td></tr>");
323 else
324 html("Binary files differ");
325 }
326 if (use_ssdiff)
327 cgit_ssdiff_footer();
328}
329
330void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix)
331{
332 enum object_type type;
333 unsigned long size;
334 struct commit *commit, *commit2;
335
336 if (!new_rev)
337 new_rev = ctx.qry.head;
338 get_sha1(new_rev, new_rev_sha1);
339 type = sha1_object_info(new_rev_sha1, &size);
340 if (type == OBJ_BAD) {
341 cgit_print_error(fmt("Bad object name: %s", new_rev));
342 return;
343 }
344 commit = lookup_commit_reference(new_rev_sha1);
345 if (!commit || parse_commit(commit))
346 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
347
348 if (old_rev)
349 get_sha1(old_rev, old_rev_sha1);
350 else if (commit->parents && commit->parents->item)
351 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
352 else
353 hashclr(old_rev_sha1);
354
355 if (!is_null_sha1(old_rev_sha1)) {
356 type = sha1_object_info(old_rev_sha1, &size);
357 if (type == OBJ_BAD) {
358 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
359 return;
360 }
361 commit2 = lookup_commit_reference(old_rev_sha1);
362 if (!commit2 || parse_commit(commit2))
363 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
364 }
365
366 if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff))
367 use_ssdiff = 1;
368
369 print_ssdiff_link();
370 cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix);
371
372 if (use_ssdiff) {
373 html("<table summary='ssdiff' class='ssdiff'>");
374 } else {
375 html("<table summary='diff' class='diff'>");
376 html("<tr><td>");
377 }
378 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix,
379 ctx.qry.ignorews);
380 if (!use_ssdiff)
381 html("</td></tr>");
382 html("</table>");
383}