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" |
John Keeping | 8f20879 | 2013-04-06 11:37:59 +0100 | [diff] [blame] | 10 | #include "ui-diff.h" |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 11 | #include "html.h" |
Lars Hjemli | a4d1ca1 | 2008-03-24 16:50:57 +0100 | [diff] [blame] | 12 | #include "ui-shared.h" |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 13 | #include "ui-ssdiff.h" |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 14 | |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 15 | unsigned char old_rev_sha1[20]; |
| 16 | unsigned char new_rev_sha1[20]; |
| 17 | |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 18 | static int files, slots; |
| 19 | static int total_adds, total_rems, max_changes; |
| 20 | static int lines_added, lines_removed; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 21 | |
| 22 | static struct fileinfo { |
| 23 | char status; |
| 24 | unsigned char old_sha1[20]; |
| 25 | unsigned char new_sha1[20]; |
| 26 | unsigned short old_mode; |
| 27 | unsigned short new_mode; |
| 28 | char *old_path; |
| 29 | char *new_path; |
| 30 | unsigned int added; |
| 31 | unsigned int removed; |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 32 | unsigned long old_size; |
| 33 | unsigned long new_size; |
| 34 | int binary:1; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 35 | } *items; |
| 36 | |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 37 | static int use_ssdiff = 0; |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 38 | static struct diff_filepair *current_filepair; |
| 39 | |
| 40 | struct diff_filespec *cgit_get_current_old_file(void) |
| 41 | { |
| 42 | return current_filepair->one; |
| 43 | } |
| 44 | |
| 45 | struct diff_filespec *cgit_get_current_new_file(void) |
| 46 | { |
| 47 | return current_filepair->two; |
| 48 | } |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 49 | |
| 50 | static void print_fileinfo(struct fileinfo *info) |
| 51 | { |
| 52 | char *class; |
| 53 | |
| 54 | switch (info->status) { |
| 55 | case DIFF_STATUS_ADDED: |
| 56 | class = "add"; |
| 57 | break; |
| 58 | case DIFF_STATUS_COPIED: |
| 59 | class = "cpy"; |
| 60 | break; |
| 61 | case DIFF_STATUS_DELETED: |
| 62 | class = "del"; |
| 63 | break; |
| 64 | case DIFF_STATUS_MODIFIED: |
| 65 | class = "upd"; |
| 66 | break; |
| 67 | case DIFF_STATUS_RENAMED: |
| 68 | class = "mov"; |
| 69 | break; |
| 70 | case DIFF_STATUS_TYPE_CHANGED: |
| 71 | class = "typ"; |
| 72 | break; |
| 73 | case DIFF_STATUS_UNKNOWN: |
| 74 | class = "unk"; |
| 75 | break; |
| 76 | case DIFF_STATUS_UNMERGED: |
| 77 | class = "stg"; |
| 78 | break; |
| 79 | default: |
| 80 | die("bug: unhandled diff status %c", info->status); |
| 81 | } |
| 82 | |
| 83 | html("<tr>"); |
| 84 | htmlf("<td class='mode'>"); |
| 85 | if (is_null_sha1(info->new_sha1)) { |
| 86 | cgit_print_filemode(info->old_mode); |
| 87 | } else { |
| 88 | cgit_print_filemode(info->new_mode); |
| 89 | } |
| 90 | |
| 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); |
| 96 | html("]</span>"); |
| 97 | } |
| 98 | htmlf("</td><td class='%s'>", class); |
Lars Hjemli | 04619c9 | 2008-09-23 17:47:26 +0200 | [diff] [blame] | 99 | 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] | 100 | ctx.qry.sha2, info->new_path, 0); |
Lukas Fleischer | bebe89d | 2011-07-22 13:47:19 +0200 | [diff] [blame] | 101 | if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) { |
| 102 | htmlf(" (%s from ", |
| 103 | info->status == DIFF_STATUS_COPIED ? "copied" : "renamed"); |
| 104 | html_txt(info->old_path); |
| 105 | html(")"); |
| 106 | } |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 107 | html("</td><td class='right'>"); |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 108 | if (info->binary) { |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 109 | htmlf("bin</td><td class='graph'>%ld -> %ld bytes", |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 110 | info->old_size, info->new_size); |
| 111 | return; |
| 112 | } |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 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"); |
| 123 | } |
| 124 | |
| 125 | static void count_diff_lines(char *line, int len) |
| 126 | { |
| 127 | if (line && (len > 0)) { |
| 128 | if (line[0] == '+') |
| 129 | lines_added++; |
| 130 | else if (line[0] == '-') |
| 131 | lines_removed++; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static void inspect_filepair(struct diff_filepair *pair) |
| 136 | { |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 137 | int binary = 0; |
| 138 | unsigned long old_size = 0; |
| 139 | unsigned long new_size = 0; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 140 | files++; |
| 141 | lines_added = 0; |
| 142 | lines_removed = 0; |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 143 | 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] | 144 | &binary, 0, ctx.qry.ignorews, count_diff_lines); |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 145 | if (files >= slots) { |
| 146 | if (slots == 0) |
| 147 | slots = 4; |
| 148 | else |
| 149 | slots = slots * 2; |
| 150 | items = xrealloc(items, slots * sizeof(struct fileinfo)); |
| 151 | } |
| 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; |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 161 | items[files-1].old_size = old_size; |
| 162 | items[files-1].new_size = new_size; |
| 163 | items[files-1].binary = binary; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 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; |
| 168 | } |
| 169 | |
Lukas Fleischer | bafab42 | 2013-03-04 08:52:33 +0100 | [diff] [blame] | 170 | static void cgit_print_diffstat(const unsigned char *old_sha1, |
| 171 | const unsigned char *new_sha1, |
| 172 | const char *prefix) |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 173 | { |
Lars Hjemli | 962a248 | 2011-03-06 23:59:56 +0100 | [diff] [blame] | 174 | int i; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 175 | |
Lars Hjemli | f82b194 | 2008-09-23 17:54:45 +0200 | [diff] [blame] | 176 | html("<div class='diffstat-header'>"); |
| 177 | cgit_diff_link("Diffstat", NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
Ragnar Ouchterlony | c358aa3 | 2009-09-14 20:19:02 +0200 | [diff] [blame] | 178 | ctx.qry.sha2, NULL, 0); |
Lukasz Janyst | 7f3c6e0 | 2011-03-05 14:10:55 +0100 | [diff] [blame] | 179 | if (prefix) { |
| 180 | html(" (limited to '"); |
| 181 | html_txt(prefix); |
| 182 | html("')"); |
| 183 | } |
Lars Hjemli | f82b194 | 2008-09-23 17:54:45 +0200 | [diff] [blame] | 184 | html("</div>"); |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 185 | html("<table summary='diffstat' class='diffstat'>"); |
| 186 | max_changes = 0; |
Johan Herland | 2cc8b99 | 2010-06-24 17:52:57 +0200 | [diff] [blame] | 187 | cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, prefix, |
| 188 | ctx.qry.ignorews); |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 189 | for (i = 0; i<files; i++) |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 190 | print_fileinfo(&items[i]); |
| 191 | html("</table>"); |
| 192 | html("<div class='diffstat-summary'>"); |
| 193 | htmlf("%d files changed, %d insertions, %d deletions", |
| 194 | files, total_adds, total_rems); |
| 195 | html("</div>"); |
| 196 | } |
| 197 | |
| 198 | |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 199 | /* |
| 200 | * print a single line returned from xdiff |
| 201 | */ |
| 202 | static void print_line(char *line, int len) |
| 203 | { |
| 204 | char *class = "ctx"; |
| 205 | char c = line[len-1]; |
| 206 | |
| 207 | if (line[0] == '+') |
| 208 | class = "add"; |
| 209 | else if (line[0] == '-') |
| 210 | class = "del"; |
| 211 | else if (line[0] == '@') |
| 212 | class = "hunk"; |
| 213 | |
| 214 | htmlf("<div class='%s'>", class); |
| 215 | line[len-1] = '\0'; |
| 216 | html_txt(line); |
| 217 | html("</div>"); |
| 218 | line[len-1] = c; |
| 219 | } |
| 220 | |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 221 | static void header(unsigned char *sha1, char *path1, int mode1, |
| 222 | unsigned char *sha2, char *path2, int mode2) |
Lars Hjemli | a342ac6 | 2007-05-14 18:31:05 +0200 | [diff] [blame] | 223 | { |
| 224 | char *abbrev1, *abbrev2; |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 225 | int subproject; |
| 226 | |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 227 | subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); |
Lars Hjemli | a342ac6 | 2007-05-14 18:31:05 +0200 | [diff] [blame] | 228 | html("<div class='head'>"); |
| 229 | html("diff --git a/"); |
| 230 | html_txt(path1); |
| 231 | html(" b/"); |
| 232 | html_txt(path2); |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 233 | |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 234 | if (mode1 == 0) |
| 235 | htmlf("<br/>new file mode %.6o", mode2); |
| 236 | |
| 237 | if (mode2 == 0) |
| 238 | htmlf("<br/>deleted file mode %.6o", mode1); |
| 239 | |
| 240 | if (!subproject) { |
| 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); |
| 244 | free(abbrev1); |
| 245 | free(abbrev2); |
| 246 | if (mode1 != 0 && mode2 != 0) { |
| 247 | htmlf(" %.6o", mode1); |
| 248 | if (mode2 != mode1) |
| 249 | htmlf("..%.6o", mode2); |
| 250 | } |
Michael Halstead | 62a40c7 | 2012-11-14 12:41:01 -0800 | [diff] [blame] | 251 | if (is_null_sha1(sha1)) { |
| 252 | path1 = "dev/null"; |
| 253 | html("<br/>--- /"); |
| 254 | } else |
| 255 | html("<br/>--- a/"); |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 256 | if (mode1 != 0) |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 257 | cgit_tree_link(path1, NULL, NULL, ctx.qry.head, |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 258 | sha1_to_hex(old_rev_sha1), path1); |
| 259 | else |
| 260 | html_txt(path1); |
Michael Halstead | 62a40c7 | 2012-11-14 12:41:01 -0800 | [diff] [blame] | 261 | if (is_null_sha1(sha2)) { |
| 262 | path2 = "dev/null"; |
| 263 | html("<br/>+++ /"); |
| 264 | } else |
| 265 | html("<br/>+++ b/"); |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 266 | if (mode2 != 0) |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 267 | cgit_tree_link(path2, NULL, NULL, ctx.qry.head, |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 268 | sha1_to_hex(new_rev_sha1), path2); |
| 269 | else |
| 270 | html_txt(path2); |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 271 | } |
Lars Hjemli | a342ac6 | 2007-05-14 18:31:05 +0200 | [diff] [blame] | 272 | html("</div>"); |
| 273 | } |
| 274 | |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 275 | static void filepair_cb(struct diff_filepair *pair) |
| 276 | { |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 277 | unsigned long old_size = 0; |
| 278 | unsigned long new_size = 0; |
| 279 | int binary = 0; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 280 | linediff_fn print_line_fn = print_line; |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 281 | |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 282 | current_filepair = pair; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 283 | if (use_ssdiff) { |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 284 | cgit_ssdiff_header_begin(); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 285 | print_line_fn = cgit_ssdiff_line_cb; |
| 286 | } |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 287 | header(pair->one->sha1, pair->one->path, pair->one->mode, |
| 288 | pair->two->sha1, pair->two->path, pair->two->mode); |
| 289 | if (use_ssdiff) |
| 290 | cgit_ssdiff_header_end(); |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 291 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { |
| 292 | if (S_ISGITLINK(pair->one->mode)) |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 293 | 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] | 294 | if (S_ISGITLINK(pair->two->mode)) |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 295 | 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] | 296 | if (use_ssdiff) |
| 297 | cgit_ssdiff_footer(); |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 298 | return; |
| 299 | } |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 300 | if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, |
Johan Herland | 2cc8b99 | 2010-06-24 17:52:57 +0200 | [diff] [blame] | 301 | &new_size, &binary, ctx.qry.context, |
| 302 | ctx.qry.ignorews, print_line_fn)) |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 303 | cgit_print_error("Error running diff"); |
Ragnar Ouchterlony | 4a198e4 | 2009-09-16 18:56:26 +0200 | [diff] [blame] | 304 | if (binary) { |
| 305 | if (use_ssdiff) |
| 306 | html("<tr><td colspan='4'>Binary files differ</td></tr>"); |
| 307 | else |
| 308 | html("Binary files differ"); |
| 309 | } |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 310 | if (use_ssdiff) |
| 311 | cgit_ssdiff_footer(); |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Lars Hjemli | 962a248 | 2011-03-06 23:59:56 +0100 | [diff] [blame] | 314 | void cgit_print_diff_ctrls() |
| 315 | { |
| 316 | int i, curr; |
| 317 | |
| 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); |
| 322 | html("<table>"); |
| 323 | html("<tr><td colspan='2'/></tr>"); |
| 324 | html("<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; |
| 329 | if (!curr) |
| 330 | curr = 3; |
| 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); |
| 335 | html("</select>"); |
| 336 | html("</td>"); |
| 337 | html("</tr><tr>"); |
| 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); |
| 343 | html("</select>"); |
| 344 | html("</td>"); |
| 345 | html("</tr><tr>"); |
| 346 | html("<td class='label'>mode:</td>"); |
| 347 | html("<td class='ctrl'>"); |
| 348 | html("<select name='ss' onchange='this.form.submit();'>"); |
Tim Chen | 1957425 | 2012-01-03 16:09:59 +0000 | [diff] [blame] | 349 | curr = ctx.qry.has_ssdiff ? ctx.qry.ssdiff : ctx.cfg.ssdiff; |
Lars Hjemli | 962a248 | 2011-03-06 23:59:56 +0100 | [diff] [blame] | 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>"); |
| 356 | html("</form>"); |
| 357 | html("</div>"); |
| 358 | } |
| 359 | |
| 360 | void cgit_print_diff(const char *new_rev, const char *old_rev, |
Lukas Fleischer | 9003cc1 | 2013-08-14 10:50:32 +0200 | [diff] [blame] | 361 | const char *prefix, int show_ctrls, int raw) |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 362 | { |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 363 | struct commit *commit, *commit2; |
Lukas Fleischer | 2da46fe | 2013-08-27 10:40:50 +0200 | [diff] [blame^] | 364 | const unsigned char *old_tree_sha1, *new_tree_sha1; |
Lars Hjemli | f9ff7df | 2007-05-16 00:58:35 +0200 | [diff] [blame] | 365 | |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 366 | if (!new_rev) |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 367 | new_rev = ctx.qry.head; |
Lukas Fleischer | 01db083 | 2013-08-20 18:56:12 +0200 | [diff] [blame] | 368 | if (get_sha1(new_rev, new_rev_sha1)) { |
John Keeping | ed5bd30 | 2013-04-06 11:23:52 +0100 | [diff] [blame] | 369 | cgit_print_error("Bad object name: %s", new_rev); |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 370 | return; |
| 371 | } |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 372 | commit = lookup_commit_reference(new_rev_sha1); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 373 | if (!commit || parse_commit(commit)) { |
John Keeping | ed5bd30 | 2013-04-06 11:23:52 +0100 | [diff] [blame] | 374 | cgit_print_error("Bad commit: %s", sha1_to_hex(new_rev_sha1)); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 375 | return; |
| 376 | } |
Lukas Fleischer | 2da46fe | 2013-08-27 10:40:50 +0200 | [diff] [blame^] | 377 | new_tree_sha1 = commit->tree->object.sha1; |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 378 | |
Lukas Fleischer | 01db083 | 2013-08-20 18:56:12 +0200 | [diff] [blame] | 379 | if (old_rev) { |
| 380 | if (get_sha1(old_rev, old_rev_sha1)) { |
| 381 | cgit_print_error("Bad object name: %s", old_rev); |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 382 | return; |
| 383 | } |
Lukas Fleischer | 01db083 | 2013-08-20 18:56:12 +0200 | [diff] [blame] | 384 | } else if (commit->parents && commit->parents->item) { |
| 385 | hashcpy(old_rev_sha1, commit->parents->item->object.sha1); |
| 386 | } else { |
| 387 | hashclr(old_rev_sha1); |
| 388 | } |
| 389 | |
| 390 | if (!is_null_sha1(old_rev_sha1)) { |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 391 | commit2 = lookup_commit_reference(old_rev_sha1); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 392 | if (!commit2 || parse_commit(commit2)) { |
John Keeping | ed5bd30 | 2013-04-06 11:23:52 +0100 | [diff] [blame] | 393 | cgit_print_error("Bad commit: %s", sha1_to_hex(old_rev_sha1)); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 394 | return; |
| 395 | } |
Lukas Fleischer | 2da46fe | 2013-08-27 10:40:50 +0200 | [diff] [blame^] | 396 | old_tree_sha1 = commit2->tree->object.sha1; |
| 397 | } else { |
| 398 | old_tree_sha1 = NULL; |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 399 | } |
Ragnar Ouchterlony | c358aa3 | 2009-09-14 20:19:02 +0200 | [diff] [blame] | 400 | |
Lukas Fleischer | 9003cc1 | 2013-08-14 10:50:32 +0200 | [diff] [blame] | 401 | if (raw) { |
Lukas Fleischer | 2da46fe | 2013-08-27 10:40:50 +0200 | [diff] [blame^] | 402 | struct diff_options diffopt; |
| 403 | |
| 404 | diff_setup(&diffopt); |
| 405 | diffopt.output_format = DIFF_FORMAT_PATCH; |
| 406 | DIFF_OPT_SET(&diffopt, RECURSIVE); |
| 407 | diff_setup_done(&diffopt); |
| 408 | |
Lukas Fleischer | 9003cc1 | 2013-08-14 10:50:32 +0200 | [diff] [blame] | 409 | ctx.page.mimetype = "text/plain"; |
| 410 | cgit_print_http_headers(&ctx); |
Lukas Fleischer | 2da46fe | 2013-08-27 10:40:50 +0200 | [diff] [blame^] | 411 | if (old_tree_sha1) { |
| 412 | diff_tree_sha1(old_tree_sha1, new_tree_sha1, "", |
| 413 | &diffopt); |
| 414 | } else { |
| 415 | diff_root_tree_sha1(new_tree_sha1, "", &diffopt); |
| 416 | } |
| 417 | diffcore_std(&diffopt); |
| 418 | diff_flush(&diffopt); |
| 419 | |
Lukas Fleischer | 9003cc1 | 2013-08-14 10:50:32 +0200 | [diff] [blame] | 420 | return; |
| 421 | } |
| 422 | |
Tim Chen | f2ced53 | 2012-01-03 16:02:14 +0000 | [diff] [blame] | 423 | use_ssdiff = ctx.qry.has_ssdiff ? ctx.qry.ssdiff : ctx.cfg.ssdiff; |
Ragnar Ouchterlony | c358aa3 | 2009-09-14 20:19:02 +0200 | [diff] [blame] | 424 | |
Lars Hjemli | 962a248 | 2011-03-06 23:59:56 +0100 | [diff] [blame] | 425 | if (show_ctrls) |
| 426 | cgit_print_diff_ctrls(); |
| 427 | |
Johan Herland | c46e468 | 2010-06-10 01:09:31 +0200 | [diff] [blame] | 428 | cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix); |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 429 | |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 430 | if (use_ssdiff) { |
| 431 | html("<table summary='ssdiff' class='ssdiff'>"); |
| 432 | } else { |
| 433 | html("<table summary='diff' class='diff'>"); |
| 434 | html("<tr><td>"); |
| 435 | } |
Johan Herland | 2cc8b99 | 2010-06-24 17:52:57 +0200 | [diff] [blame] | 436 | cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix, |
| 437 | ctx.qry.ignorews); |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 438 | if (!use_ssdiff) |
| 439 | html("</td></tr>"); |
Ondrej Jirman | 0928d88 | 2007-05-26 01:14:25 +0200 | [diff] [blame] | 440 | html("</table>"); |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 441 | } |