summaryrefslogtreecommitdiffabout
path: root/ui-diff.c
authorLars Hjemli <hjemli@gmail.com>2011-02-19 13:51:00 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2011-02-19 13:51:00 (UTC)
commit979c460e7f71d153ae79da67b8b21c3412f0fe02 (patch) (unidiff)
tree6da9ffb66ed0a68205e6644cb7e2b4652d6684be /ui-diff.c
parentfb9e6d1594a24fe4e551fd57a9c91fd18b14806e (diff)
parent0141b9f889bbaa1fe474f9a98dd377138ac73054 (diff)
downloadcgit-979c460e7f71d153ae79da67b8b21c3412f0fe02.zip
cgit-979c460e7f71d153ae79da67b8b21c3412f0fe02.tar.gz
cgit-979c460e7f71d153ae79da67b8b21c3412f0fe02.tar.bz2
Merge branch 'br/misc'
* br/misc: Use transparent background for the cgit logo ssdiff: anchors for ssdiff implement repo.logo and repo.logo-link
Diffstat (limited to 'ui-diff.c') (more/less context) (ignore whitespace changes)
-rw-r--r--ui-diff.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/ui-diff.c b/ui-diff.c
index 7ff7e46..a53425d 100644
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -1,132 +1,143 @@
1/* ui-diff.c: show diff between two blobs 1/* ui-diff.c: show diff between two blobs
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "html.h" 10#include "html.h"
11#include "ui-shared.h" 11#include "ui-shared.h"
12#include "ui-ssdiff.h" 12#include "ui-ssdiff.h"
13 13
14unsigned char old_rev_sha1[20]; 14unsigned char old_rev_sha1[20];
15unsigned char new_rev_sha1[20]; 15unsigned char new_rev_sha1[20];
16 16
17static int files, slots; 17static int files, slots;
18static int total_adds, total_rems, max_changes; 18static int total_adds, total_rems, max_changes;
19static int lines_added, lines_removed; 19static int lines_added, lines_removed;
20 20
21static struct fileinfo { 21static struct fileinfo {
22 char status; 22 char status;
23 unsigned char old_sha1[20]; 23 unsigned char old_sha1[20];
24 unsigned char new_sha1[20]; 24 unsigned char new_sha1[20];
25 unsigned short old_mode; 25 unsigned short old_mode;
26 unsigned short new_mode; 26 unsigned short new_mode;
27 char *old_path; 27 char *old_path;
28 char *new_path; 28 char *new_path;
29 unsigned int added; 29 unsigned int added;
30 unsigned int removed; 30 unsigned int removed;
31 unsigned long old_size; 31 unsigned long old_size;
32 unsigned long new_size; 32 unsigned long new_size;
33 int binary:1; 33 int binary:1;
34} *items; 34} *items;
35 35
36static int use_ssdiff = 0; 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}
37 48
38static void print_fileinfo(struct fileinfo *info) 49static void print_fileinfo(struct fileinfo *info)
39{ 50{
40 char *class; 51 char *class;
41 52
42 switch (info->status) { 53 switch (info->status) {
43 case DIFF_STATUS_ADDED: 54 case DIFF_STATUS_ADDED:
44 class = "add"; 55 class = "add";
45 break; 56 break;
46 case DIFF_STATUS_COPIED: 57 case DIFF_STATUS_COPIED:
47 class = "cpy"; 58 class = "cpy";
48 break; 59 break;
49 case DIFF_STATUS_DELETED: 60 case DIFF_STATUS_DELETED:
50 class = "del"; 61 class = "del";
51 break; 62 break;
52 case DIFF_STATUS_MODIFIED: 63 case DIFF_STATUS_MODIFIED:
53 class = "upd"; 64 class = "upd";
54 break; 65 break;
55 case DIFF_STATUS_RENAMED: 66 case DIFF_STATUS_RENAMED:
56 class = "mov"; 67 class = "mov";
57 break; 68 break;
58 case DIFF_STATUS_TYPE_CHANGED: 69 case DIFF_STATUS_TYPE_CHANGED:
59 class = "typ"; 70 class = "typ";
60 break; 71 break;
61 case DIFF_STATUS_UNKNOWN: 72 case DIFF_STATUS_UNKNOWN:
62 class = "unk"; 73 class = "unk";
63 break; 74 break;
64 case DIFF_STATUS_UNMERGED: 75 case DIFF_STATUS_UNMERGED:
65 class = "stg"; 76 class = "stg";
66 break; 77 break;
67 default: 78 default:
68 die("bug: unhandled diff status %c", info->status); 79 die("bug: unhandled diff status %c", info->status);
69 } 80 }
70 81
71 html("<tr>"); 82 html("<tr>");
72 htmlf("<td class='mode'>"); 83 htmlf("<td class='mode'>");
73 if (is_null_sha1(info->new_sha1)) { 84 if (is_null_sha1(info->new_sha1)) {
74 cgit_print_filemode(info->old_mode); 85 cgit_print_filemode(info->old_mode);
75 } else { 86 } else {
76 cgit_print_filemode(info->new_mode); 87 cgit_print_filemode(info->new_mode);
77 } 88 }
78 89
79 if (info->old_mode != info->new_mode && 90 if (info->old_mode != info->new_mode &&
80 !is_null_sha1(info->old_sha1) && 91 !is_null_sha1(info->old_sha1) &&
81 !is_null_sha1(info->new_sha1)) { 92 !is_null_sha1(info->new_sha1)) {
82 html("<span class='modechange'>["); 93 html("<span class='modechange'>[");
83 cgit_print_filemode(info->old_mode); 94 cgit_print_filemode(info->old_mode);
84 html("]</span>"); 95 html("]</span>");
85 } 96 }
86 htmlf("</td><td class='%s'>", class); 97 htmlf("</td><td class='%s'>", class);
87 cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.sha1, 98 cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
88 ctx.qry.sha2, info->new_path, 0); 99 ctx.qry.sha2, info->new_path, 0);
89 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) 100 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED)
90 htmlf(" (%s from %s)", 101 htmlf(" (%s from %s)",
91 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed", 102 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed",
92 info->old_path); 103 info->old_path);
93 html("</td><td class='right'>"); 104 html("</td><td class='right'>");
94 if (info->binary) { 105 if (info->binary) {
95 htmlf("bin</td><td class='graph'>%ld -> %ld bytes", 106 htmlf("bin</td><td class='graph'>%ld -> %ld bytes",
96 info->old_size, info->new_size); 107 info->old_size, info->new_size);
97 return; 108 return;
98 } 109 }
99 htmlf("%d", info->added + info->removed); 110 htmlf("%d", info->added + info->removed);
100 html("</td><td class='graph'>"); 111 html("</td><td class='graph'>");
101 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes)); 112 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
102 htmlf("<td class='add' style='width: %.1f%%;'/>", 113 htmlf("<td class='add' style='width: %.1f%%;'/>",
103 info->added * 100.0 / max_changes); 114 info->added * 100.0 / max_changes);
104 htmlf("<td class='rem' style='width: %.1f%%;'/>", 115 htmlf("<td class='rem' style='width: %.1f%%;'/>",
105 info->removed * 100.0 / max_changes); 116 info->removed * 100.0 / max_changes);
106 htmlf("<td class='none' style='width: %.1f%%;'/>", 117 htmlf("<td class='none' style='width: %.1f%%;'/>",
107 (max_changes - info->removed - info->added) * 100.0 / max_changes); 118 (max_changes - info->removed - info->added) * 100.0 / max_changes);
108 html("</tr></table></td></tr>\n"); 119 html("</tr></table></td></tr>\n");
109} 120}
110 121
111static void count_diff_lines(char *line, int len) 122static void count_diff_lines(char *line, int len)
112{ 123{
113 if (line && (len > 0)) { 124 if (line && (len > 0)) {
114 if (line[0] == '+') 125 if (line[0] == '+')
115 lines_added++; 126 lines_added++;
116 else if (line[0] == '-') 127 else if (line[0] == '-')
117 lines_removed++; 128 lines_removed++;
118 } 129 }
119} 130}
120 131
121static void inspect_filepair(struct diff_filepair *pair) 132static void inspect_filepair(struct diff_filepair *pair)
122{ 133{
123 int binary = 0; 134 int binary = 0;
124 unsigned long old_size = 0; 135 unsigned long old_size = 0;
125 unsigned long new_size = 0; 136 unsigned long new_size = 0;
126 files++; 137 files++;
127 lines_added = 0; 138 lines_added = 0;
128 lines_removed = 0; 139 lines_removed = 0;
129 cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, &new_size, 140 cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, &new_size,
130 &binary, 0, ctx.qry.ignorews, count_diff_lines); 141 &binary, 0, ctx.qry.ignorews, count_diff_lines);
131 if (files >= slots) { 142 if (files >= slots) {
132 if (slots == 0) 143 if (slots == 0)
@@ -191,181 +202,182 @@ void cgit_print_diffstat(const unsigned char *old_sha1,
191} 202}
192 203
193 204
194/* 205/*
195 * print a single line returned from xdiff 206 * print a single line returned from xdiff
196 */ 207 */
197static void print_line(char *line, int len) 208static void print_line(char *line, int len)
198{ 209{
199 char *class = "ctx"; 210 char *class = "ctx";
200 char c = line[len-1]; 211 char c = line[len-1];
201 212
202 if (line[0] == '+') 213 if (line[0] == '+')
203 class = "add"; 214 class = "add";
204 else if (line[0] == '-') 215 else if (line[0] == '-')
205 class = "del"; 216 class = "del";
206 else if (line[0] == '@') 217 else if (line[0] == '@')
207 class = "hunk"; 218 class = "hunk";
208 219
209 htmlf("<div class='%s'>", class); 220 htmlf("<div class='%s'>", class);
210 line[len-1] = '\0'; 221 line[len-1] = '\0';
211 html_txt(line); 222 html_txt(line);
212 html("</div>"); 223 html("</div>");
213 line[len-1] = c; 224 line[len-1] = c;
214} 225}
215 226
216static void header(unsigned char *sha1, char *path1, int mode1, 227static void header(unsigned char *sha1, char *path1, int mode1,
217 unsigned char *sha2, char *path2, int mode2) 228 unsigned char *sha2, char *path2, int mode2)
218{ 229{
219 char *abbrev1, *abbrev2; 230 char *abbrev1, *abbrev2;
220 int subproject; 231 int subproject;
221 232
222 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); 233 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
223 html("<div class='head'>"); 234 html("<div class='head'>");
224 html("diff --git a/"); 235 html("diff --git a/");
225 html_txt(path1); 236 html_txt(path1);
226 html(" b/"); 237 html(" b/");
227 html_txt(path2); 238 html_txt(path2);
228 239
229 if (is_null_sha1(sha1)) 240 if (is_null_sha1(sha1))
230 path1 = "dev/null"; 241 path1 = "dev/null";
231 if (is_null_sha1(sha2)) 242 if (is_null_sha1(sha2))
232 path2 = "dev/null"; 243 path2 = "dev/null";
233 244
234 if (mode1 == 0) 245 if (mode1 == 0)
235 htmlf("<br/>new file mode %.6o", mode2); 246 htmlf("<br/>new file mode %.6o", mode2);
236 247
237 if (mode2 == 0) 248 if (mode2 == 0)
238 htmlf("<br/>deleted file mode %.6o", mode1); 249 htmlf("<br/>deleted file mode %.6o", mode1);
239 250
240 if (!subproject) { 251 if (!subproject) {
241 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); 252 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
242 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); 253 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
243 htmlf("<br/>index %s..%s", abbrev1, abbrev2); 254 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
244 free(abbrev1); 255 free(abbrev1);
245 free(abbrev2); 256 free(abbrev2);
246 if (mode1 != 0 && mode2 != 0) { 257 if (mode1 != 0 && mode2 != 0) {
247 htmlf(" %.6o", mode1); 258 htmlf(" %.6o", mode1);
248 if (mode2 != mode1) 259 if (mode2 != mode1)
249 htmlf("..%.6o", mode2); 260 htmlf("..%.6o", mode2);
250 } 261 }
251 html("<br/>--- a/"); 262 html("<br/>--- a/");
252 if (mode1 != 0) 263 if (mode1 != 0)
253 cgit_tree_link(path1, NULL, NULL, ctx.qry.head, 264 cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
254 sha1_to_hex(old_rev_sha1), path1); 265 sha1_to_hex(old_rev_sha1), path1);
255 else 266 else
256 html_txt(path1); 267 html_txt(path1);
257 html("<br/>+++ b/"); 268 html("<br/>+++ b/");
258 if (mode2 != 0) 269 if (mode2 != 0)
259 cgit_tree_link(path2, NULL, NULL, ctx.qry.head, 270 cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
260 sha1_to_hex(new_rev_sha1), path2); 271 sha1_to_hex(new_rev_sha1), path2);
261 else 272 else
262 html_txt(path2); 273 html_txt(path2);
263 } 274 }
264 html("</div>"); 275 html("</div>");
265} 276}
266 277
267static void print_ssdiff_link() 278static void print_ssdiff_link()
268{ 279{
269 if (!strcmp(ctx.qry.page, "diff")) { 280 if (!strcmp(ctx.qry.page, "diff")) {
270 if (use_ssdiff) 281 if (use_ssdiff)
271 cgit_diff_link("Unidiff", NULL, NULL, ctx.qry.head, 282 cgit_diff_link("Unidiff", NULL, NULL, ctx.qry.head,
272 ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1); 283 ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1);
273 else 284 else
274 cgit_diff_link("Side-by-side diff", NULL, NULL, 285 cgit_diff_link("Side-by-side diff", NULL, NULL,
275 ctx.qry.head, ctx.qry.sha1, 286 ctx.qry.head, ctx.qry.sha1,
276 ctx.qry.sha2, ctx.qry.path, 1); 287 ctx.qry.sha2, ctx.qry.path, 1);
277 } 288 }
278} 289}
279 290
280static void filepair_cb(struct diff_filepair *pair) 291static void filepair_cb(struct diff_filepair *pair)
281{ 292{
282 unsigned long old_size = 0; 293 unsigned long old_size = 0;
283 unsigned long new_size = 0; 294 unsigned long new_size = 0;
284 int binary = 0; 295 int binary = 0;
285 linediff_fn print_line_fn = print_line; 296 linediff_fn print_line_fn = print_line;
286 297
298 current_filepair = pair;
287 if (use_ssdiff) { 299 if (use_ssdiff) {
288 cgit_ssdiff_header_begin(); 300 cgit_ssdiff_header_begin();
289 print_line_fn = cgit_ssdiff_line_cb; 301 print_line_fn = cgit_ssdiff_line_cb;
290 } 302 }
291 header(pair->one->sha1, pair->one->path, pair->one->mode, 303 header(pair->one->sha1, pair->one->path, pair->one->mode,
292 pair->two->sha1, pair->two->path, pair->two->mode); 304 pair->two->sha1, pair->two->path, pair->two->mode);
293 if (use_ssdiff) 305 if (use_ssdiff)
294 cgit_ssdiff_header_end(); 306 cgit_ssdiff_header_end();
295 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { 307 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
296 if (S_ISGITLINK(pair->one->mode)) 308 if (S_ISGITLINK(pair->one->mode))
297 print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); 309 print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
298 if (S_ISGITLINK(pair->two->mode)) 310 if (S_ISGITLINK(pair->two->mode))
299 print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); 311 print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
300 if (use_ssdiff) 312 if (use_ssdiff)
301 cgit_ssdiff_footer(); 313 cgit_ssdiff_footer();
302 return; 314 return;
303 } 315 }
304 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, 316 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
305 &new_size, &binary, ctx.qry.context, 317 &new_size, &binary, ctx.qry.context,
306 ctx.qry.ignorews, print_line_fn)) 318 ctx.qry.ignorews, print_line_fn))
307 cgit_print_error("Error running diff"); 319 cgit_print_error("Error running diff");
308 if (binary) { 320 if (binary) {
309 if (use_ssdiff) 321 if (use_ssdiff)
310 html("<tr><td colspan='4'>Binary files differ</td></tr>"); 322 html("<tr><td colspan='4'>Binary files differ</td></tr>");
311 else 323 else
312 html("Binary files differ"); 324 html("Binary files differ");
313 } 325 }
314 if (use_ssdiff) 326 if (use_ssdiff)
315 cgit_ssdiff_footer(); 327 cgit_ssdiff_footer();
316} 328}
317 329
318void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) 330void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix)
319{ 331{
320 enum object_type type; 332 enum object_type type;
321 unsigned long size; 333 unsigned long size;
322 struct commit *commit, *commit2; 334 struct commit *commit, *commit2;
323 335
324 if (!new_rev) 336 if (!new_rev)
325 new_rev = ctx.qry.head; 337 new_rev = ctx.qry.head;
326 get_sha1(new_rev, new_rev_sha1); 338 get_sha1(new_rev, new_rev_sha1);
327 type = sha1_object_info(new_rev_sha1, &size); 339 type = sha1_object_info(new_rev_sha1, &size);
328 if (type == OBJ_BAD) { 340 if (type == OBJ_BAD) {
329 cgit_print_error(fmt("Bad object name: %s", new_rev)); 341 cgit_print_error(fmt("Bad object name: %s", new_rev));
330 return; 342 return;
331 } 343 }
332 commit = lookup_commit_reference(new_rev_sha1); 344 commit = lookup_commit_reference(new_rev_sha1);
333 if (!commit || parse_commit(commit)) 345 if (!commit || parse_commit(commit))
334 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1))); 346 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
335 347
336 if (old_rev) 348 if (old_rev)
337 get_sha1(old_rev, old_rev_sha1); 349 get_sha1(old_rev, old_rev_sha1);
338 else if (commit->parents && commit->parents->item) 350 else if (commit->parents && commit->parents->item)
339 hashcpy(old_rev_sha1, commit->parents->item->object.sha1); 351 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
340 else 352 else
341 hashclr(old_rev_sha1); 353 hashclr(old_rev_sha1);
342 354
343 if (!is_null_sha1(old_rev_sha1)) { 355 if (!is_null_sha1(old_rev_sha1)) {
344 type = sha1_object_info(old_rev_sha1, &size); 356 type = sha1_object_info(old_rev_sha1, &size);
345 if (type == OBJ_BAD) { 357 if (type == OBJ_BAD) {
346 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1))); 358 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
347 return; 359 return;
348 } 360 }
349 commit2 = lookup_commit_reference(old_rev_sha1); 361 commit2 = lookup_commit_reference(old_rev_sha1);
350 if (!commit2 || parse_commit(commit2)) 362 if (!commit2 || parse_commit(commit2))
351 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1))); 363 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
352 } 364 }
353 365
354 if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff)) 366 if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff))
355 use_ssdiff = 1; 367 use_ssdiff = 1;
356 368
357 print_ssdiff_link(); 369 print_ssdiff_link();
358 cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix); 370 cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix);
359 371
360 if (use_ssdiff) { 372 if (use_ssdiff) {
361 html("<table summary='ssdiff' class='ssdiff'>"); 373 html("<table summary='ssdiff' class='ssdiff'>");
362 } else { 374 } else {
363 html("<table summary='diff' class='diff'>"); 375 html("<table summary='diff' class='diff'>");
364 html("<tr><td>"); 376 html("<tr><td>");
365 } 377 }
366 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix, 378 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix,
367 ctx.qry.ignorews); 379 ctx.qry.ignorews);
368 if (!use_ssdiff) 380 if (!use_ssdiff)
369 html("</td></tr>"); 381 html("</td></tr>");
370 html("</table>"); 382 html("</table>");
371} 383}