Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 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" |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 10 | #include "html.h" |
Lars Hjemli | a4d1ca1 | 2008-03-24 16:50:57 +0100 | [diff] [blame] | 11 | #include "ui-shared.h" |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 12 | #include "ui-ssdiff.h" |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 13 | |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 14 | unsigned char old_rev_sha1[20]; |
| 15 | unsigned char new_rev_sha1[20]; |
| 16 | |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 17 | static int files, slots; |
| 18 | static int total_adds, total_rems, max_changes; |
| 19 | static int lines_added, lines_removed; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 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; |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 31 | unsigned long old_size; |
| 32 | unsigned long new_size; |
| 33 | int binary:1; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 34 | } *items; |
| 35 | |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 36 | static int use_ssdiff = 0; |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 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 | } |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 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); |
Lars Hjemli | 04619c9 | 2008-09-23 17:47:26 +0200 | [diff] [blame] | 98 | cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
Ragnar Ouchterlony | c358aa3 | 2009-09-14 20:19:02 +0200 | [diff] [blame] | 99 | ctx.qry.sha2, info->new_path, 0); |
Lukas Fleischer | bebe89d | 2011-07-22 13:47:19 +0200 | [diff] [blame^] | 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 | } |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 106 | html("</td><td class='right'>"); |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 107 | if (info->binary) { |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 108 | htmlf("bin</td><td class='graph'>%ld -> %ld bytes", |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 109 | info->old_size, info->new_size); |
| 110 | return; |
| 111 | } |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 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 | { |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 136 | int binary = 0; |
| 137 | unsigned long old_size = 0; |
| 138 | unsigned long new_size = 0; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 139 | files++; |
| 140 | lines_added = 0; |
| 141 | lines_removed = 0; |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 142 | cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, &new_size, |
Johan Herland | 2cc8b99 | 2010-06-24 17:52:57 +0200 | [diff] [blame] | 143 | &binary, 0, ctx.qry.ignorews, count_diff_lines); |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 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; |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 160 | items[files-1].old_size = old_size; |
| 161 | items[files-1].new_size = new_size; |
| 162 | items[files-1].binary = binary; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 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, |
Johan Herland | c46e468 | 2010-06-10 01:09:31 +0200 | [diff] [blame] | 170 | const unsigned char *new_sha1, const char *prefix) |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 171 | { |
Johan Herland | d20313e | 2010-06-10 20:15:51 +0200 | [diff] [blame] | 172 | int i, save_context = ctx.qry.context; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 173 | |
Lars Hjemli | f82b194 | 2008-09-23 17:54:45 +0200 | [diff] [blame] | 174 | html("<div class='diffstat-header'>"); |
| 175 | cgit_diff_link("Diffstat", NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
Ragnar Ouchterlony | c358aa3 | 2009-09-14 20:19:02 +0200 | [diff] [blame] | 176 | ctx.qry.sha2, NULL, 0); |
Lukasz Janyst | 7f3c6e0 | 2011-03-05 14:10:55 +0100 | [diff] [blame] | 177 | if (prefix) { |
| 178 | html(" (limited to '"); |
| 179 | html_txt(prefix); |
| 180 | html("')"); |
| 181 | } |
Johan Herland | d20313e | 2010-06-10 20:15:51 +0200 | [diff] [blame] | 182 | html(" ("); |
| 183 | ctx.qry.context = (save_context > 0 ? save_context : 3) << 1; |
| 184 | cgit_self_link("more", NULL, NULL, &ctx); |
| 185 | html("/"); |
| 186 | ctx.qry.context = (save_context > 3 ? save_context : 3) >> 1; |
| 187 | cgit_self_link("less", NULL, NULL, &ctx); |
| 188 | ctx.qry.context = save_context; |
| 189 | html(" context)"); |
Johan Herland | 72ef913 | 2010-06-24 17:53:20 +0200 | [diff] [blame] | 190 | html(" ("); |
| 191 | ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2; |
| 192 | cgit_self_link(ctx.qry.ignorews ? "ignore" : "show", NULL, NULL, &ctx); |
| 193 | ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2; |
| 194 | html(" whitespace changes)"); |
Lars Hjemli | f82b194 | 2008-09-23 17:54:45 +0200 | [diff] [blame] | 195 | html("</div>"); |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 196 | html("<table summary='diffstat' class='diffstat'>"); |
| 197 | max_changes = 0; |
Johan Herland | 2cc8b99 | 2010-06-24 17:52:57 +0200 | [diff] [blame] | 198 | cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, prefix, |
| 199 | ctx.qry.ignorews); |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 200 | for(i = 0; i<files; i++) |
| 201 | print_fileinfo(&items[i]); |
| 202 | html("</table>"); |
| 203 | html("<div class='diffstat-summary'>"); |
| 204 | htmlf("%d files changed, %d insertions, %d deletions", |
| 205 | files, total_adds, total_rems); |
| 206 | html("</div>"); |
| 207 | } |
| 208 | |
| 209 | |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 210 | /* |
| 211 | * print a single line returned from xdiff |
| 212 | */ |
| 213 | static void print_line(char *line, int len) |
| 214 | { |
| 215 | char *class = "ctx"; |
| 216 | char c = line[len-1]; |
| 217 | |
| 218 | if (line[0] == '+') |
| 219 | class = "add"; |
| 220 | else if (line[0] == '-') |
| 221 | class = "del"; |
| 222 | else if (line[0] == '@') |
| 223 | class = "hunk"; |
| 224 | |
| 225 | htmlf("<div class='%s'>", class); |
| 226 | line[len-1] = '\0'; |
| 227 | html_txt(line); |
| 228 | html("</div>"); |
| 229 | line[len-1] = c; |
| 230 | } |
| 231 | |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 232 | static void header(unsigned char *sha1, char *path1, int mode1, |
| 233 | unsigned char *sha2, char *path2, int mode2) |
Lars Hjemli | a342ac6 | 2007-05-14 18:31:05 +0200 | [diff] [blame] | 234 | { |
| 235 | char *abbrev1, *abbrev2; |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 236 | int subproject; |
| 237 | |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 238 | subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); |
Lars Hjemli | a342ac6 | 2007-05-14 18:31:05 +0200 | [diff] [blame] | 239 | html("<div class='head'>"); |
| 240 | html("diff --git a/"); |
| 241 | html_txt(path1); |
| 242 | html(" b/"); |
| 243 | html_txt(path2); |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 244 | |
| 245 | if (is_null_sha1(sha1)) |
| 246 | path1 = "dev/null"; |
| 247 | if (is_null_sha1(sha2)) |
| 248 | path2 = "dev/null"; |
| 249 | |
| 250 | if (mode1 == 0) |
| 251 | htmlf("<br/>new file mode %.6o", mode2); |
| 252 | |
| 253 | if (mode2 == 0) |
| 254 | htmlf("<br/>deleted file mode %.6o", mode1); |
| 255 | |
| 256 | if (!subproject) { |
| 257 | abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); |
| 258 | abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); |
| 259 | htmlf("<br/>index %s..%s", abbrev1, abbrev2); |
| 260 | free(abbrev1); |
| 261 | free(abbrev2); |
| 262 | if (mode1 != 0 && mode2 != 0) { |
| 263 | htmlf(" %.6o", mode1); |
| 264 | if (mode2 != mode1) |
| 265 | htmlf("..%.6o", mode2); |
| 266 | } |
| 267 | html("<br/>--- a/"); |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 268 | if (mode1 != 0) |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 269 | cgit_tree_link(path1, NULL, NULL, ctx.qry.head, |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 270 | sha1_to_hex(old_rev_sha1), path1); |
| 271 | else |
| 272 | html_txt(path1); |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 273 | html("<br/>+++ b/"); |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 274 | if (mode2 != 0) |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 275 | cgit_tree_link(path2, NULL, NULL, ctx.qry.head, |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 276 | sha1_to_hex(new_rev_sha1), path2); |
| 277 | else |
| 278 | html_txt(path2); |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 279 | } |
Lars Hjemli | a342ac6 | 2007-05-14 18:31:05 +0200 | [diff] [blame] | 280 | html("</div>"); |
| 281 | } |
| 282 | |
Ragnar Ouchterlony | c358aa3 | 2009-09-14 20:19:02 +0200 | [diff] [blame] | 283 | static void print_ssdiff_link() |
| 284 | { |
| 285 | if (!strcmp(ctx.qry.page, "diff")) { |
| 286 | if (use_ssdiff) |
| 287 | cgit_diff_link("Unidiff", NULL, NULL, ctx.qry.head, |
Ragnar Ouchterlony | 4a198e4 | 2009-09-16 18:56:26 +0200 | [diff] [blame] | 288 | ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1); |
Ragnar Ouchterlony | c358aa3 | 2009-09-14 20:19:02 +0200 | [diff] [blame] | 289 | else |
| 290 | cgit_diff_link("Side-by-side diff", NULL, NULL, |
| 291 | ctx.qry.head, ctx.qry.sha1, |
Ragnar Ouchterlony | 4a198e4 | 2009-09-16 18:56:26 +0200 | [diff] [blame] | 292 | ctx.qry.sha2, ctx.qry.path, 1); |
Ragnar Ouchterlony | c358aa3 | 2009-09-14 20:19:02 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 296 | static void filepair_cb(struct diff_filepair *pair) |
| 297 | { |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 298 | unsigned long old_size = 0; |
| 299 | unsigned long new_size = 0; |
| 300 | int binary = 0; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 301 | linediff_fn print_line_fn = print_line; |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 302 | |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 303 | current_filepair = pair; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 304 | if (use_ssdiff) { |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 305 | cgit_ssdiff_header_begin(); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 306 | print_line_fn = cgit_ssdiff_line_cb; |
| 307 | } |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 308 | header(pair->one->sha1, pair->one->path, pair->one->mode, |
| 309 | pair->two->sha1, pair->two->path, pair->two->mode); |
| 310 | if (use_ssdiff) |
| 311 | cgit_ssdiff_header_end(); |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 312 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { |
| 313 | if (S_ISGITLINK(pair->one->mode)) |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 314 | print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 315 | if (S_ISGITLINK(pair->two->mode)) |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 316 | print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); |
Ragnar Ouchterlony | 4a198e4 | 2009-09-16 18:56:26 +0200 | [diff] [blame] | 317 | if (use_ssdiff) |
| 318 | cgit_ssdiff_footer(); |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 319 | return; |
| 320 | } |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 321 | if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, |
Johan Herland | 2cc8b99 | 2010-06-24 17:52:57 +0200 | [diff] [blame] | 322 | &new_size, &binary, ctx.qry.context, |
| 323 | ctx.qry.ignorews, print_line_fn)) |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 324 | cgit_print_error("Error running diff"); |
Ragnar Ouchterlony | 4a198e4 | 2009-09-16 18:56:26 +0200 | [diff] [blame] | 325 | if (binary) { |
| 326 | if (use_ssdiff) |
| 327 | html("<tr><td colspan='4'>Binary files differ</td></tr>"); |
| 328 | else |
| 329 | html("Binary files differ"); |
| 330 | } |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 331 | if (use_ssdiff) |
| 332 | cgit_ssdiff_footer(); |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Lars Hjemli | 1a6025b | 2007-10-01 11:46:38 +0200 | [diff] [blame] | 335 | void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 336 | { |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 337 | enum object_type type; |
| 338 | unsigned long size; |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 339 | struct commit *commit, *commit2; |
Lars Hjemli | f9ff7df | 2007-05-16 00:58:35 +0200 | [diff] [blame] | 340 | |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 341 | if (!new_rev) |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 342 | new_rev = ctx.qry.head; |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 343 | get_sha1(new_rev, new_rev_sha1); |
| 344 | type = sha1_object_info(new_rev_sha1, &size); |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 345 | if (type == OBJ_BAD) { |
| 346 | cgit_print_error(fmt("Bad object name: %s", new_rev)); |
| 347 | return; |
| 348 | } |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 349 | commit = lookup_commit_reference(new_rev_sha1); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 350 | if (!commit || parse_commit(commit)) { |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 351 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1))); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 352 | return; |
| 353 | } |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 354 | |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 355 | if (old_rev) |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 356 | get_sha1(old_rev, old_rev_sha1); |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 357 | else if (commit->parents && commit->parents->item) |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 358 | hashcpy(old_rev_sha1, commit->parents->item->object.sha1); |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 359 | else |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 360 | hashclr(old_rev_sha1); |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 361 | |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 362 | if (!is_null_sha1(old_rev_sha1)) { |
| 363 | type = sha1_object_info(old_rev_sha1, &size); |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 364 | if (type == OBJ_BAD) { |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 365 | cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1))); |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 366 | return; |
| 367 | } |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 368 | commit2 = lookup_commit_reference(old_rev_sha1); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 369 | if (!commit2 || parse_commit(commit2)) { |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 370 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1))); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 371 | return; |
| 372 | } |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 373 | } |
Ragnar Ouchterlony | c358aa3 | 2009-09-14 20:19:02 +0200 | [diff] [blame] | 374 | |
| 375 | if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff)) |
| 376 | use_ssdiff = 1; |
| 377 | |
| 378 | print_ssdiff_link(); |
Johan Herland | c46e468 | 2010-06-10 01:09:31 +0200 | [diff] [blame] | 379 | cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix); |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 380 | |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 381 | if (use_ssdiff) { |
| 382 | html("<table summary='ssdiff' class='ssdiff'>"); |
| 383 | } else { |
| 384 | html("<table summary='diff' class='diff'>"); |
| 385 | html("<tr><td>"); |
| 386 | } |
Johan Herland | 2cc8b99 | 2010-06-24 17:52:57 +0200 | [diff] [blame] | 387 | cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix, |
| 388 | ctx.qry.ignorews); |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 389 | if (!use_ssdiff) |
| 390 | html("</td></tr>"); |
Ondrej Jirman | 0928d88 | 2007-05-26 01:14:25 +0200 | [diff] [blame] | 391 | html("</table>"); |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 392 | } |