Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 1 | /* ui-diff.c: show diff between two blobs |
| 2 | * |
Lukas Fleischer | f7f26f8 | 2014-01-08 15:10:49 +0100 | [diff] [blame] | 3 | * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 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 | |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 15 | struct object_id old_rev_oid[1]; |
| 16 | struct object_id new_rev_oid[1]; |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 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; |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 24 | struct object_id old_oid[1]; |
| 25 | struct object_id new_oid[1]; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 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; |
John Keeping | 12d3d4c | 2015-03-08 16:32:17 +0000 | [diff] [blame] | 34 | unsigned 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; |
John Keeping | 30304d8 | 2015-08-12 15:55:28 +0100 | [diff] [blame] | 39 | static const char *current_prefix; |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 40 | |
| 41 | struct diff_filespec *cgit_get_current_old_file(void) |
| 42 | { |
| 43 | return current_filepair->one; |
| 44 | } |
| 45 | |
| 46 | struct diff_filespec *cgit_get_current_new_file(void) |
| 47 | { |
| 48 | return current_filepair->two; |
| 49 | } |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 50 | |
| 51 | static void print_fileinfo(struct fileinfo *info) |
| 52 | { |
| 53 | char *class; |
| 54 | |
| 55 | switch (info->status) { |
| 56 | case DIFF_STATUS_ADDED: |
| 57 | class = "add"; |
| 58 | break; |
| 59 | case DIFF_STATUS_COPIED: |
| 60 | class = "cpy"; |
| 61 | break; |
| 62 | case DIFF_STATUS_DELETED: |
| 63 | class = "del"; |
| 64 | break; |
| 65 | case DIFF_STATUS_MODIFIED: |
| 66 | class = "upd"; |
| 67 | break; |
| 68 | case DIFF_STATUS_RENAMED: |
| 69 | class = "mov"; |
| 70 | break; |
| 71 | case DIFF_STATUS_TYPE_CHANGED: |
| 72 | class = "typ"; |
| 73 | break; |
| 74 | case DIFF_STATUS_UNKNOWN: |
| 75 | class = "unk"; |
| 76 | break; |
| 77 | case DIFF_STATUS_UNMERGED: |
| 78 | class = "stg"; |
| 79 | break; |
| 80 | default: |
| 81 | die("bug: unhandled diff status %c", info->status); |
| 82 | } |
| 83 | |
| 84 | html("<tr>"); |
| 85 | htmlf("<td class='mode'>"); |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 86 | if (is_null_oid(info->new_oid)) { |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 87 | cgit_print_filemode(info->old_mode); |
| 88 | } else { |
| 89 | cgit_print_filemode(info->new_mode); |
| 90 | } |
| 91 | |
| 92 | if (info->old_mode != info->new_mode && |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 93 | !is_null_oid(info->old_oid) && |
| 94 | !is_null_oid(info->new_oid)) { |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 95 | html("<span class='modechange'>["); |
| 96 | cgit_print_filemode(info->old_mode); |
| 97 | html("]</span>"); |
| 98 | } |
| 99 | htmlf("</td><td class='%s'>", class); |
Lars Hjemli | 04619c9 | 2008-09-23 17:47:26 +0200 | [diff] [blame] | 100 | cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
John Keeping | 03f537f | 2014-10-05 10:59:03 +0100 | [diff] [blame] | 101 | ctx.qry.sha2, info->new_path); |
Lukas Fleischer | bebe89d | 2011-07-22 13:47:19 +0200 | [diff] [blame] | 102 | if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) { |
| 103 | htmlf(" (%s from ", |
| 104 | info->status == DIFF_STATUS_COPIED ? "copied" : "renamed"); |
| 105 | html_txt(info->old_path); |
| 106 | html(")"); |
| 107 | } |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 108 | html("</td><td class='right'>"); |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 109 | if (info->binary) { |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 110 | htmlf("bin</td><td class='graph'>%ld -> %ld bytes", |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 111 | info->old_size, info->new_size); |
| 112 | return; |
| 113 | } |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 114 | htmlf("%d", info->added + info->removed); |
| 115 | html("</td><td class='graph'>"); |
| 116 | htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes)); |
| 117 | htmlf("<td class='add' style='width: %.1f%%;'/>", |
| 118 | info->added * 100.0 / max_changes); |
| 119 | htmlf("<td class='rem' style='width: %.1f%%;'/>", |
| 120 | info->removed * 100.0 / max_changes); |
| 121 | htmlf("<td class='none' style='width: %.1f%%;'/>", |
| 122 | (max_changes - info->removed - info->added) * 100.0 / max_changes); |
| 123 | html("</tr></table></td></tr>\n"); |
| 124 | } |
| 125 | |
| 126 | static void count_diff_lines(char *line, int len) |
| 127 | { |
| 128 | if (line && (len > 0)) { |
| 129 | if (line[0] == '+') |
| 130 | lines_added++; |
| 131 | else if (line[0] == '-') |
| 132 | lines_removed++; |
| 133 | } |
| 134 | } |
| 135 | |
John Keeping | 30304d8 | 2015-08-12 15:55:28 +0100 | [diff] [blame] | 136 | static int show_filepair(struct diff_filepair *pair) |
| 137 | { |
| 138 | /* Always show if we have no limiting prefix. */ |
| 139 | if (!current_prefix) |
| 140 | return 1; |
| 141 | |
| 142 | /* Show if either path in the pair begins with the prefix. */ |
| 143 | if (starts_with(pair->one->path, current_prefix) || |
| 144 | starts_with(pair->two->path, current_prefix)) |
| 145 | return 1; |
| 146 | |
| 147 | /* Otherwise we don't want to show this filepair. */ |
| 148 | return 0; |
| 149 | } |
| 150 | |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 151 | static void inspect_filepair(struct diff_filepair *pair) |
| 152 | { |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 153 | int binary = 0; |
| 154 | unsigned long old_size = 0; |
| 155 | unsigned long new_size = 0; |
John Keeping | 30304d8 | 2015-08-12 15:55:28 +0100 | [diff] [blame] | 156 | |
| 157 | if (!show_filepair(pair)) |
| 158 | return; |
| 159 | |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 160 | files++; |
| 161 | lines_added = 0; |
| 162 | lines_removed = 0; |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 163 | cgit_diff_files(&pair->one->oid, &pair->two->oid, &old_size, &new_size, |
Johan Herland | 2cc8b99 | 2010-06-24 17:52:57 +0200 | [diff] [blame] | 164 | &binary, 0, ctx.qry.ignorews, count_diff_lines); |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 165 | if (files >= slots) { |
| 166 | if (slots == 0) |
| 167 | slots = 4; |
| 168 | else |
| 169 | slots = slots * 2; |
| 170 | items = xrealloc(items, slots * sizeof(struct fileinfo)); |
| 171 | } |
| 172 | items[files-1].status = pair->status; |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 173 | oidcpy(items[files-1].old_oid, &pair->one->oid); |
| 174 | oidcpy(items[files-1].new_oid, &pair->two->oid); |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 175 | items[files-1].old_mode = pair->one->mode; |
| 176 | items[files-1].new_mode = pair->two->mode; |
| 177 | items[files-1].old_path = xstrdup(pair->one->path); |
| 178 | items[files-1].new_path = xstrdup(pair->two->path); |
| 179 | items[files-1].added = lines_added; |
| 180 | items[files-1].removed = lines_removed; |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 181 | items[files-1].old_size = old_size; |
| 182 | items[files-1].new_size = new_size; |
| 183 | items[files-1].binary = binary; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 184 | if (lines_added + lines_removed > max_changes) |
| 185 | max_changes = lines_added + lines_removed; |
| 186 | total_adds += lines_added; |
| 187 | total_rems += lines_removed; |
| 188 | } |
| 189 | |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 190 | static void cgit_print_diffstat(const struct object_id *old_oid, |
| 191 | const struct object_id *new_oid, |
Lukas Fleischer | bafab42 | 2013-03-04 08:52:33 +0100 | [diff] [blame] | 192 | const char *prefix) |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 193 | { |
Lars Hjemli | 962a248 | 2011-03-06 23:59:56 +0100 | [diff] [blame] | 194 | int i; |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 195 | |
Lars Hjemli | f82b194 | 2008-09-23 17:54:45 +0200 | [diff] [blame] | 196 | html("<div class='diffstat-header'>"); |
| 197 | cgit_diff_link("Diffstat", NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
John Keeping | 03f537f | 2014-10-05 10:59:03 +0100 | [diff] [blame] | 198 | ctx.qry.sha2, NULL); |
Lukasz Janyst | 7f3c6e0 | 2011-03-05 14:10:55 +0100 | [diff] [blame] | 199 | if (prefix) { |
| 200 | html(" (limited to '"); |
| 201 | html_txt(prefix); |
| 202 | html("')"); |
| 203 | } |
Lars Hjemli | f82b194 | 2008-09-23 17:54:45 +0200 | [diff] [blame] | 204 | html("</div>"); |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 205 | html("<table summary='diffstat' class='diffstat'>"); |
| 206 | max_changes = 0; |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 207 | cgit_diff_tree(old_oid, new_oid, inspect_filepair, prefix, |
Johan Herland | 2cc8b99 | 2010-06-24 17:52:57 +0200 | [diff] [blame] | 208 | ctx.qry.ignorews); |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 209 | for (i = 0; i<files; i++) |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 210 | print_fileinfo(&items[i]); |
| 211 | html("</table>"); |
| 212 | html("<div class='diffstat-summary'>"); |
| 213 | htmlf("%d files changed, %d insertions, %d deletions", |
| 214 | files, total_adds, total_rems); |
| 215 | html("</div>"); |
| 216 | } |
| 217 | |
| 218 | |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 219 | /* |
| 220 | * print a single line returned from xdiff |
| 221 | */ |
| 222 | static void print_line(char *line, int len) |
| 223 | { |
| 224 | char *class = "ctx"; |
| 225 | char c = line[len-1]; |
| 226 | |
| 227 | if (line[0] == '+') |
| 228 | class = "add"; |
| 229 | else if (line[0] == '-') |
| 230 | class = "del"; |
| 231 | else if (line[0] == '@') |
| 232 | class = "hunk"; |
| 233 | |
| 234 | htmlf("<div class='%s'>", class); |
| 235 | line[len-1] = '\0'; |
| 236 | html_txt(line); |
| 237 | html("</div>"); |
| 238 | line[len-1] = c; |
| 239 | } |
| 240 | |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 241 | static void header(const struct object_id *oid1, char *path1, int mode1, |
| 242 | const struct object_id *oid2, char *path2, int mode2) |
Lars Hjemli | a342ac6 | 2007-05-14 18:31:05 +0200 | [diff] [blame] | 243 | { |
| 244 | char *abbrev1, *abbrev2; |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 245 | int subproject; |
| 246 | |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 247 | subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); |
Lars Hjemli | a342ac6 | 2007-05-14 18:31:05 +0200 | [diff] [blame] | 248 | html("<div class='head'>"); |
| 249 | html("diff --git a/"); |
| 250 | html_txt(path1); |
| 251 | html(" b/"); |
| 252 | html_txt(path2); |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 253 | |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 254 | if (mode1 == 0) |
| 255 | htmlf("<br/>new file mode %.6o", mode2); |
| 256 | |
| 257 | if (mode2 == 0) |
| 258 | htmlf("<br/>deleted file mode %.6o", mode1); |
| 259 | |
| 260 | if (!subproject) { |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 261 | abbrev1 = xstrdup(find_unique_abbrev(oid1, DEFAULT_ABBREV)); |
| 262 | abbrev2 = xstrdup(find_unique_abbrev(oid2, DEFAULT_ABBREV)); |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 263 | htmlf("<br/>index %s..%s", abbrev1, abbrev2); |
| 264 | free(abbrev1); |
| 265 | free(abbrev2); |
| 266 | if (mode1 != 0 && mode2 != 0) { |
| 267 | htmlf(" %.6o", mode1); |
| 268 | if (mode2 != mode1) |
| 269 | htmlf("..%.6o", mode2); |
| 270 | } |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 271 | if (is_null_oid(oid1)) { |
Michael Halstead | 62a40c7 | 2012-11-14 12:41:01 -0800 | [diff] [blame] | 272 | path1 = "dev/null"; |
| 273 | html("<br/>--- /"); |
| 274 | } else |
| 275 | html("<br/>--- a/"); |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 276 | if (mode1 != 0) |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 277 | cgit_tree_link(path1, NULL, NULL, ctx.qry.head, |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 278 | oid_to_hex(old_rev_oid), path1); |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 279 | else |
| 280 | html_txt(path1); |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 281 | if (is_null_oid(oid2)) { |
Michael Halstead | 62a40c7 | 2012-11-14 12:41:01 -0800 | [diff] [blame] | 282 | path2 = "dev/null"; |
| 283 | html("<br/>+++ /"); |
| 284 | } else |
| 285 | html("<br/>+++ b/"); |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 286 | if (mode2 != 0) |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 287 | cgit_tree_link(path2, NULL, NULL, ctx.qry.head, |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 288 | oid_to_hex(new_rev_oid), path2); |
Lars Hjemli | e238ebe | 2007-10-01 12:30:29 +0200 | [diff] [blame] | 289 | else |
| 290 | html_txt(path2); |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 291 | } |
Lars Hjemli | a342ac6 | 2007-05-14 18:31:05 +0200 | [diff] [blame] | 292 | html("</div>"); |
| 293 | } |
| 294 | |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 295 | static void filepair_cb(struct diff_filepair *pair) |
| 296 | { |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 297 | unsigned long old_size = 0; |
| 298 | unsigned long new_size = 0; |
| 299 | int binary = 0; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 300 | linediff_fn print_line_fn = print_line; |
Lars Hjemli | c495cf0 | 2009-01-31 10:40:40 +0100 | [diff] [blame] | 301 | |
John Keeping | 30304d8 | 2015-08-12 15:55:28 +0100 | [diff] [blame] | 302 | if (!show_filepair(pair)) |
| 303 | return; |
| 304 | |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 305 | current_filepair = pair; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 306 | if (use_ssdiff) { |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 307 | cgit_ssdiff_header_begin(); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 308 | print_line_fn = cgit_ssdiff_line_cb; |
| 309 | } |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 310 | header(&pair->one->oid, pair->one->path, pair->one->mode, |
| 311 | &pair->two->oid, pair->two->path, pair->two->mode); |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 312 | if (use_ssdiff) |
| 313 | cgit_ssdiff_header_end(); |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 314 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { |
| 315 | if (S_ISGITLINK(pair->one->mode)) |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 316 | print_line_fn(fmt("-Subproject %s", oid_to_hex(&pair->one->oid)), 52); |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 317 | if (S_ISGITLINK(pair->two->mode)) |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 318 | print_line_fn(fmt("+Subproject %s", oid_to_hex(&pair->two->oid)), 52); |
Ragnar Ouchterlony | 4a198e4 | 2009-09-16 18:56:26 +0200 | [diff] [blame] | 319 | if (use_ssdiff) |
| 320 | cgit_ssdiff_footer(); |
Lars Hjemli | f4f1339 | 2007-05-16 04:21:06 +0200 | [diff] [blame] | 321 | return; |
| 322 | } |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 323 | if (cgit_diff_files(&pair->one->oid, &pair->two->oid, &old_size, |
Johan Herland | 2cc8b99 | 2010-06-24 17:52:57 +0200 | [diff] [blame] | 324 | &new_size, &binary, ctx.qry.context, |
| 325 | ctx.qry.ignorews, print_line_fn)) |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 326 | cgit_print_error("Error running diff"); |
Ragnar Ouchterlony | 4a198e4 | 2009-09-16 18:56:26 +0200 | [diff] [blame] | 327 | if (binary) { |
| 328 | if (use_ssdiff) |
| 329 | html("<tr><td colspan='4'>Binary files differ</td></tr>"); |
| 330 | else |
| 331 | html("Binary files differ"); |
| 332 | } |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 333 | if (use_ssdiff) |
| 334 | cgit_ssdiff_footer(); |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 335 | } |
| 336 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 337 | void cgit_print_diff_ctrls(void) |
Lars Hjemli | 962a248 | 2011-03-06 23:59:56 +0100 | [diff] [blame] | 338 | { |
| 339 | int i, curr; |
| 340 | |
| 341 | html("<div class='cgit-panel'>"); |
| 342 | html("<b>diff options</b>"); |
Jason A. Donenfeld | 21bf30b | 2016-05-12 21:38:59 +0200 | [diff] [blame] | 343 | html("<form method='get'>"); |
Lars Hjemli | 962a248 | 2011-03-06 23:59:56 +0100 | [diff] [blame] | 344 | cgit_add_hidden_formfields(1, 0, ctx.qry.page); |
| 345 | html("<table>"); |
| 346 | html("<tr><td colspan='2'/></tr>"); |
| 347 | html("<tr>"); |
| 348 | html("<td class='label'>context:</td>"); |
| 349 | html("<td class='ctrl'>"); |
| 350 | html("<select name='context' onchange='this.form.submit();'>"); |
| 351 | curr = ctx.qry.context; |
| 352 | if (!curr) |
| 353 | curr = 3; |
| 354 | for (i = 1; i <= 10; i++) |
| 355 | html_intoption(i, fmt("%d", i), curr); |
| 356 | for (i = 15; i <= 40; i += 5) |
| 357 | html_intoption(i, fmt("%d", i), curr); |
| 358 | html("</select>"); |
| 359 | html("</td>"); |
| 360 | html("</tr><tr>"); |
| 361 | html("<td class='label'>space:</td>"); |
| 362 | html("<td class='ctrl'>"); |
| 363 | html("<select name='ignorews' onchange='this.form.submit();'>"); |
| 364 | html_intoption(0, "include", ctx.qry.ignorews); |
| 365 | html_intoption(1, "ignore", ctx.qry.ignorews); |
| 366 | html("</select>"); |
| 367 | html("</td>"); |
| 368 | html("</tr><tr>"); |
| 369 | html("<td class='label'>mode:</td>"); |
| 370 | html("<td class='ctrl'>"); |
John Keeping | 1830271 | 2014-10-05 10:59:04 +0100 | [diff] [blame] | 371 | html("<select name='dt' onchange='this.form.submit();'>"); |
| 372 | curr = ctx.qry.has_difftype ? ctx.qry.difftype : ctx.cfg.difftype; |
Lars Hjemli | 962a248 | 2011-03-06 23:59:56 +0100 | [diff] [blame] | 373 | html_intoption(0, "unified", curr); |
| 374 | html_intoption(1, "ssdiff", curr); |
John Keeping | ddfaef6 | 2014-10-05 10:59:05 +0100 | [diff] [blame] | 375 | html_intoption(2, "stat only", curr); |
Lars Hjemli | 962a248 | 2011-03-06 23:59:56 +0100 | [diff] [blame] | 376 | html("</select></td></tr>"); |
| 377 | html("<tr><td/><td class='ctrl'>"); |
| 378 | html("<noscript><input type='submit' value='reload'/></noscript>"); |
| 379 | html("</td></tr></table>"); |
| 380 | html("</form>"); |
| 381 | html("</div>"); |
| 382 | } |
| 383 | |
| 384 | void cgit_print_diff(const char *new_rev, const char *old_rev, |
Lukas Fleischer | 9003cc1 | 2013-08-14 10:50:32 +0200 | [diff] [blame] | 385 | const char *prefix, int show_ctrls, int raw) |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 386 | { |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 387 | struct commit *commit, *commit2; |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 388 | const struct object_id *old_tree_oid, *new_tree_oid; |
John Keeping | 1830271 | 2014-10-05 10:59:04 +0100 | [diff] [blame] | 389 | diff_type difftype; |
Lars Hjemli | f9ff7df | 2007-05-16 00:58:35 +0200 | [diff] [blame] | 390 | |
John Keeping | 30304d8 | 2015-08-12 15:55:28 +0100 | [diff] [blame] | 391 | /* |
| 392 | * If "follow" is set then the diff machinery needs to examine the |
| 393 | * entire commit to detect renames so we must limit the paths in our |
| 394 | * own callbacks and not pass the prefix to the diff machinery. |
| 395 | */ |
| 396 | if (ctx.qry.follow && ctx.cfg.enable_follow_links) { |
| 397 | current_prefix = prefix; |
| 398 | prefix = ""; |
| 399 | } else { |
| 400 | current_prefix = NULL; |
| 401 | } |
| 402 | |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 403 | if (!new_rev) |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 404 | new_rev = ctx.qry.head; |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 405 | if (get_oid(new_rev, new_rev_oid)) { |
John Keeping | 3b220eb | 2015-08-14 12:47:14 +0100 | [diff] [blame] | 406 | cgit_print_error_page(404, "Not found", |
| 407 | "Bad object name: %s", new_rev); |
Lars Hjemli | 4a0be58 | 2007-06-17 18:12:03 +0200 | [diff] [blame] | 408 | return; |
| 409 | } |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 410 | commit = lookup_commit_reference(new_rev_oid); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 411 | if (!commit || parse_commit(commit)) { |
John Keeping | 3b220eb | 2015-08-14 12:47:14 +0100 | [diff] [blame] | 412 | cgit_print_error_page(404, "Not found", |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 413 | "Bad commit: %s", oid_to_hex(new_rev_oid)); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 414 | return; |
| 415 | } |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 416 | new_tree_oid = &commit->maybe_tree->object.oid; |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 417 | |
Lukas Fleischer | 01db083 | 2013-08-20 18:56:12 +0200 | [diff] [blame] | 418 | if (old_rev) { |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 419 | if (get_oid(old_rev, old_rev_oid)) { |
John Keeping | 3b220eb | 2015-08-14 12:47:14 +0100 | [diff] [blame] | 420 | cgit_print_error_page(404, "Not found", |
| 421 | "Bad object name: %s", old_rev); |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 422 | return; |
| 423 | } |
Lukas Fleischer | 01db083 | 2013-08-20 18:56:12 +0200 | [diff] [blame] | 424 | } else if (commit->parents && commit->parents->item) { |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 425 | oidcpy(old_rev_oid, &commit->parents->item->object.oid); |
Lukas Fleischer | 01db083 | 2013-08-20 18:56:12 +0200 | [diff] [blame] | 426 | } else { |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 427 | oidclr(old_rev_oid); |
Lukas Fleischer | 01db083 | 2013-08-20 18:56:12 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 430 | if (!is_null_oid(old_rev_oid)) { |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 431 | commit2 = lookup_commit_reference(old_rev_oid); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 432 | if (!commit2 || parse_commit(commit2)) { |
John Keeping | 3b220eb | 2015-08-14 12:47:14 +0100 | [diff] [blame] | 433 | cgit_print_error_page(404, "Not found", |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 434 | "Bad commit: %s", oid_to_hex(old_rev_oid)); |
Lukas Fleischer | 9afc883 | 2011-04-05 10:38:53 +0200 | [diff] [blame] | 435 | return; |
| 436 | } |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 437 | old_tree_oid = &commit2->maybe_tree->object.oid; |
Lukas Fleischer | 2da46fe | 2013-08-27 10:40:50 +0200 | [diff] [blame] | 438 | } else { |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 439 | old_tree_oid = NULL; |
Lars Hjemli | 6a8749d | 2007-05-13 23:13:12 +0200 | [diff] [blame] | 440 | } |
Ragnar Ouchterlony | c358aa3 | 2009-09-14 20:19:02 +0200 | [diff] [blame] | 441 | |
Lukas Fleischer | 9003cc1 | 2013-08-14 10:50:32 +0200 | [diff] [blame] | 442 | if (raw) { |
Lukas Fleischer | 2da46fe | 2013-08-27 10:40:50 +0200 | [diff] [blame] | 443 | struct diff_options diffopt; |
| 444 | |
| 445 | diff_setup(&diffopt); |
| 446 | diffopt.output_format = DIFF_FORMAT_PATCH; |
Christian Hesse | 1dd53e3 | 2018-01-18 09:19:31 +0100 | [diff] [blame] | 447 | diffopt.flags.recursive = 1; |
Lukas Fleischer | 2da46fe | 2013-08-27 10:40:50 +0200 | [diff] [blame] | 448 | diff_setup_done(&diffopt); |
| 449 | |
Lukas Fleischer | 9003cc1 | 2013-08-14 10:50:32 +0200 | [diff] [blame] | 450 | ctx.page.mimetype = "text/plain"; |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 451 | cgit_print_http_headers(); |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 452 | if (old_tree_oid) { |
| 453 | diff_tree_oid(old_tree_oid, new_tree_oid, "", |
Lukas Fleischer | 2da46fe | 2013-08-27 10:40:50 +0200 | [diff] [blame] | 454 | &diffopt); |
| 455 | } else { |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 456 | diff_root_tree_oid(new_tree_oid, "", &diffopt); |
Lukas Fleischer | 2da46fe | 2013-08-27 10:40:50 +0200 | [diff] [blame] | 457 | } |
| 458 | diffcore_std(&diffopt); |
| 459 | diff_flush(&diffopt); |
| 460 | |
Lukas Fleischer | 9003cc1 | 2013-08-14 10:50:32 +0200 | [diff] [blame] | 461 | return; |
| 462 | } |
| 463 | |
John Keeping | 1830271 | 2014-10-05 10:59:04 +0100 | [diff] [blame] | 464 | difftype = ctx.qry.has_difftype ? ctx.qry.difftype : ctx.cfg.difftype; |
| 465 | use_ssdiff = difftype == DIFF_SSDIFF; |
Ragnar Ouchterlony | c358aa3 | 2009-09-14 20:19:02 +0200 | [diff] [blame] | 466 | |
John Keeping | 3b220eb | 2015-08-14 12:47:14 +0100 | [diff] [blame] | 467 | if (show_ctrls) { |
| 468 | cgit_print_layout_start(); |
Lars Hjemli | 962a248 | 2011-03-06 23:59:56 +0100 | [diff] [blame] | 469 | cgit_print_diff_ctrls(); |
John Keeping | 3b220eb | 2015-08-14 12:47:14 +0100 | [diff] [blame] | 470 | } |
Lars Hjemli | 962a248 | 2011-03-06 23:59:56 +0100 | [diff] [blame] | 471 | |
John Keeping | d6c4050 | 2014-12-29 22:27:55 +0000 | [diff] [blame] | 472 | /* |
| 473 | * Clicking on a link to a file in the diff stat should show a diff |
| 474 | * of the file, showing the diff stat limited to a single file is |
| 475 | * pretty useless. All links from this point on will be to |
| 476 | * individual files, so we simply reset the difftype in the query |
| 477 | * here to avoid propagating DIFF_STATONLY to the individual files. |
| 478 | */ |
| 479 | if (difftype == DIFF_STATONLY) |
| 480 | ctx.qry.difftype = ctx.cfg.difftype; |
| 481 | |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 482 | cgit_print_diffstat(old_rev_oid, new_rev_oid, prefix); |
Lars Hjemli | fe1230d | 2008-04-24 23:32:02 +0200 | [diff] [blame] | 483 | |
John Keeping | ddfaef6 | 2014-10-05 10:59:05 +0100 | [diff] [blame] | 484 | if (difftype == DIFF_STATONLY) |
| 485 | return; |
| 486 | |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 487 | if (use_ssdiff) { |
| 488 | html("<table summary='ssdiff' class='ssdiff'>"); |
| 489 | } else { |
| 490 | html("<table summary='diff' class='diff'>"); |
| 491 | html("<tr><td>"); |
| 492 | } |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 493 | cgit_diff_tree(old_rev_oid, new_rev_oid, filepair_cb, prefix, |
Johan Herland | 2cc8b99 | 2010-06-24 17:52:57 +0200 | [diff] [blame] | 494 | ctx.qry.ignorews); |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 495 | if (!use_ssdiff) |
| 496 | html("</td></tr>"); |
Ondrej Jirman | 0928d88 | 2007-05-26 01:14:25 +0200 | [diff] [blame] | 497 | html("</table>"); |
John Keeping | 3b220eb | 2015-08-14 12:47:14 +0100 | [diff] [blame] | 498 | |
| 499 | if (show_ctrls) |
| 500 | cgit_print_layout_end(); |
Lars Hjemli | 36aba00 | 2006-12-20 22:48:27 +0100 | [diff] [blame] | 501 | } |