]> git.cameronkatri.com Git - cgit.git/blob - ui-diff.c
Merge branch 'stable'
[cgit.git] / ui-diff.c
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
14 unsigned char old_rev_sha1[20];
15 unsigned char new_rev_sha1[20];
16
17 static int files, slots;
18 static int total_adds, total_rems, max_changes;
19 static int lines_added, lines_removed;
20
21 static 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
36 static int use_ssdiff = 0;
37 static struct diff_filepair *current_filepair;
38
39 struct diff_filespec *cgit_get_current_old_file(void)
40 {
41 return current_filepair->one;
42 }
43
44 struct diff_filespec *cgit_get_current_new_file(void)
45 {
46 return current_filepair->two;
47 }
48
49 static 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 ",
102 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed");
103 html_txt(info->old_path);
104 html(")");
105 }
106 html("</td><td class='right'>");
107 if (info->binary) {
108 htmlf("bin</td><td class='graph'>%ld -> %ld bytes",
109 info->old_size, info->new_size);
110 return;
111 }
112 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
124 static 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
134 static void inspect_filepair(struct diff_filepair *pair)
135 {
136 int binary = 0;
137 unsigned long old_size = 0;
138 unsigned long new_size = 0;
139 files++;
140 lines_added = 0;
141 lines_removed = 0;
142 cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, &new_size,
143 &binary, 0, ctx.qry.ignorews, count_diff_lines);
144 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;
160 items[files-1].old_size = old_size;
161 items[files-1].new_size = new_size;
162 items[files-1].binary = binary;
163 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
169 void cgit_print_diffstat(const unsigned char *old_sha1,
170 const unsigned char *new_sha1, const char *prefix)
171 {
172 int i;
173
174 html("<div class='diffstat-header'>");
175 cgit_diff_link("Diffstat", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
176 ctx.qry.sha2, NULL, 0);
177 if (prefix) {
178 html(" (limited to '");
179 html_txt(prefix);
180 html("')");
181 }
182 html("</div>");
183 html("<table summary='diffstat' class='diffstat'>");
184 max_changes = 0;
185 cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, prefix,
186 ctx.qry.ignorews);
187 for(i = 0; i<files; i++)
188 print_fileinfo(&items[i]);
189 html("</table>");
190 html("<div class='diffstat-summary'>");
191 htmlf("%d files changed, %d insertions, %d deletions",
192 files, total_adds, total_rems);
193 html("</div>");
194 }
195
196
197 /*
198 * print a single line returned from xdiff
199 */
200 static void print_line(char *line, int len)
201 {
202 char *class = "ctx";
203 char c = line[len-1];
204
205 if (line[0] == '+')
206 class = "add";
207 else if (line[0] == '-')
208 class = "del";
209 else if (line[0] == '@')
210 class = "hunk";
211
212 htmlf("<div class='%s'>", class);
213 line[len-1] = '\0';
214 html_txt(line);
215 html("</div>");
216 line[len-1] = c;
217 }
218
219 static void header(unsigned char *sha1, char *path1, int mode1,
220 unsigned char *sha2, char *path2, int mode2)
221 {
222 char *abbrev1, *abbrev2;
223 int subproject;
224
225 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
226 html("<div class='head'>");
227 html("diff --git a/");
228 html_txt(path1);
229 html(" b/");
230 html_txt(path2);
231
232 if (is_null_sha1(sha1))
233 path1 = "dev/null";
234 if (is_null_sha1(sha2))
235 path2 = "dev/null";
236
237 if (mode1 == 0)
238 htmlf("<br/>new file mode %.6o", mode2);
239
240 if (mode2 == 0)
241 htmlf("<br/>deleted file mode %.6o", mode1);
242
243 if (!subproject) {
244 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
245 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
246 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
247 free(abbrev1);
248 free(abbrev2);
249 if (mode1 != 0 && mode2 != 0) {
250 htmlf(" %.6o", mode1);
251 if (mode2 != mode1)
252 htmlf("..%.6o", mode2);
253 }
254 html("<br/>--- a/");
255 if (mode1 != 0)
256 cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
257 sha1_to_hex(old_rev_sha1), path1);
258 else
259 html_txt(path1);
260 html("<br/>+++ b/");
261 if (mode2 != 0)
262 cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
263 sha1_to_hex(new_rev_sha1), path2);
264 else
265 html_txt(path2);
266 }
267 html("</div>");
268 }
269
270 static void filepair_cb(struct diff_filepair *pair)
271 {
272 unsigned long old_size = 0;
273 unsigned long new_size = 0;
274 int binary = 0;
275 linediff_fn print_line_fn = print_line;
276
277 current_filepair = pair;
278 if (use_ssdiff) {
279 cgit_ssdiff_header_begin();
280 print_line_fn = cgit_ssdiff_line_cb;
281 }
282 header(pair->one->sha1, pair->one->path, pair->one->mode,
283 pair->two->sha1, pair->two->path, pair->two->mode);
284 if (use_ssdiff)
285 cgit_ssdiff_header_end();
286 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
287 if (S_ISGITLINK(pair->one->mode))
288 print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
289 if (S_ISGITLINK(pair->two->mode))
290 print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
291 if (use_ssdiff)
292 cgit_ssdiff_footer();
293 return;
294 }
295 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
296 &new_size, &binary, ctx.qry.context,
297 ctx.qry.ignorews, print_line_fn))
298 cgit_print_error("Error running diff");
299 if (binary) {
300 if (use_ssdiff)
301 html("<tr><td colspan='4'>Binary files differ</td></tr>");
302 else
303 html("Binary files differ");
304 }
305 if (use_ssdiff)
306 cgit_ssdiff_footer();
307 }
308
309 void cgit_print_diff_ctrls()
310 {
311 int i, curr;
312
313 html("<div class='cgit-panel'>");
314 html("<b>diff options</b>");
315 html("<form method='get' action='.'>");
316 cgit_add_hidden_formfields(1, 0, ctx.qry.page);
317 html("<table>");
318 html("<tr><td colspan='2'/></tr>");
319 html("<tr>");
320 html("<td class='label'>context:</td>");
321 html("<td class='ctrl'>");
322 html("<select name='context' onchange='this.form.submit();'>");
323 curr = ctx.qry.context;
324 if (!curr)
325 curr = 3;
326 for (i = 1; i <= 10; i++)
327 html_intoption(i, fmt("%d", i), curr);
328 for (i = 15; i <= 40; i += 5)
329 html_intoption(i, fmt("%d", i), curr);
330 html("</select>");
331 html("</td>");
332 html("</tr><tr>");
333 html("<td class='label'>space:</td>");
334 html("<td class='ctrl'>");
335 html("<select name='ignorews' onchange='this.form.submit();'>");
336 html_intoption(0, "include", ctx.qry.ignorews);
337 html_intoption(1, "ignore", ctx.qry.ignorews);
338 html("</select>");
339 html("</td>");
340 html("</tr><tr>");
341 html("<td class='label'>mode:</td>");
342 html("<td class='ctrl'>");
343 html("<select name='ss' onchange='this.form.submit();'>");
344 curr = ctx.qry.has_ssdiff ? ctx.qry.ssdiff : ctx.cfg.ssdiff;
345 html_intoption(0, "unified", curr);
346 html_intoption(1, "ssdiff", curr);
347 html("</select></td></tr>");
348 html("<tr><td/><td class='ctrl'>");
349 html("<noscript><input type='submit' value='reload'/></noscript>");
350 html("</td></tr></table>");
351 html("</form>");
352 html("</div>");
353 }
354
355 void cgit_print_diff(const char *new_rev, const char *old_rev,
356 const char *prefix, int show_ctrls)
357 {
358 enum object_type type;
359 unsigned long size;
360 struct commit *commit, *commit2;
361
362 if (!new_rev)
363 new_rev = ctx.qry.head;
364 get_sha1(new_rev, new_rev_sha1);
365 type = sha1_object_info(new_rev_sha1, &size);
366 if (type == OBJ_BAD) {
367 cgit_print_error(fmt("Bad object name: %s", new_rev));
368 return;
369 }
370 commit = lookup_commit_reference(new_rev_sha1);
371 if (!commit || parse_commit(commit)) {
372 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
373 return;
374 }
375
376 if (old_rev)
377 get_sha1(old_rev, old_rev_sha1);
378 else if (commit->parents && commit->parents->item)
379 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
380 else
381 hashclr(old_rev_sha1);
382
383 if (!is_null_sha1(old_rev_sha1)) {
384 type = sha1_object_info(old_rev_sha1, &size);
385 if (type == OBJ_BAD) {
386 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
387 return;
388 }
389 commit2 = lookup_commit_reference(old_rev_sha1);
390 if (!commit2 || parse_commit(commit2)) {
391 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
392 return;
393 }
394 }
395
396 use_ssdiff = ctx.qry.has_ssdiff ? ctx.qry.ssdiff : ctx.cfg.ssdiff;
397
398 if (show_ctrls)
399 cgit_print_diff_ctrls();
400
401 cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix);
402
403 if (use_ssdiff) {
404 html("<table summary='ssdiff' class='ssdiff'>");
405 } else {
406 html("<table summary='diff' class='diff'>");
407 html("<tr><td>");
408 }
409 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix,
410 ctx.qry.ignorews);
411 if (!use_ssdiff)
412 html("</td></tr>");
413 html("</table>");
414 }