summaryrefslogtreecommitdiffabout
authorLukas Fleischer <cgit@cryptocrack.de>2011-04-05 08:38:53 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2011-05-23 20:58:35 (UTC)
commit9afc883297b0d0943e9b358d2299950f33e8e5ed (patch) (unidiff)
tree27e81428c0a6ad4bbdf5633fc95b946b4a631d30
parenta0bf375a1a9b74056a913f3687c6f5b42ad4acf6 (diff)
downloadcgit-9afc883297b0d0943e9b358d2299950f33e8e5ed.zip
cgit-9afc883297b0d0943e9b358d2299950f33e8e5ed.tar.gz
cgit-9afc883297b0d0943e9b358d2299950f33e8e5ed.tar.bz2
Avoid null pointer dereference in cgit_print_diff().
When calling cgit_print_diff() with a bad new_rev and a NULL old_rev, checking for new_rev's parent commit will result in a null pointer dereference. Returning on an invalid commit before dereferencing fixes this. Spotted with clang-analyzer. Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de> Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--ui-diff.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/ui-diff.c b/ui-diff.c
index a7bc667..d21541b 100644
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -1,386 +1,390 @@
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; 37static struct diff_filepair *current_filepair;
38 38
39struct diff_filespec *cgit_get_current_old_file(void) 39struct diff_filespec *cgit_get_current_old_file(void)
40{ 40{
41 return current_filepair->one; 41 return current_filepair->one;
42} 42}
43 43
44struct diff_filespec *cgit_get_current_new_file(void) 44struct diff_filespec *cgit_get_current_new_file(void)
45{ 45{
46 return current_filepair->two; 46 return current_filepair->two;
47} 47}
48 48
49static void print_fileinfo(struct fileinfo *info) 49static void print_fileinfo(struct fileinfo *info)
50{ 50{
51 char *class; 51 char *class;
52 52
53 switch (info->status) { 53 switch (info->status) {
54 case DIFF_STATUS_ADDED: 54 case DIFF_STATUS_ADDED:
55 class = "add"; 55 class = "add";
56 break; 56 break;
57 case DIFF_STATUS_COPIED: 57 case DIFF_STATUS_COPIED:
58 class = "cpy"; 58 class = "cpy";
59 break; 59 break;
60 case DIFF_STATUS_DELETED: 60 case DIFF_STATUS_DELETED:
61 class = "del"; 61 class = "del";
62 break; 62 break;
63 case DIFF_STATUS_MODIFIED: 63 case DIFF_STATUS_MODIFIED:
64 class = "upd"; 64 class = "upd";
65 break; 65 break;
66 case DIFF_STATUS_RENAMED: 66 case DIFF_STATUS_RENAMED:
67 class = "mov"; 67 class = "mov";
68 break; 68 break;
69 case DIFF_STATUS_TYPE_CHANGED: 69 case DIFF_STATUS_TYPE_CHANGED:
70 class = "typ"; 70 class = "typ";
71 break; 71 break;
72 case DIFF_STATUS_UNKNOWN: 72 case DIFF_STATUS_UNKNOWN:
73 class = "unk"; 73 class = "unk";
74 break; 74 break;
75 case DIFF_STATUS_UNMERGED: 75 case DIFF_STATUS_UNMERGED:
76 class = "stg"; 76 class = "stg";
77 break; 77 break;
78 default: 78 default:
79 die("bug: unhandled diff status %c", info->status); 79 die("bug: unhandled diff status %c", info->status);
80 } 80 }
81 81
82 html("<tr>"); 82 html("<tr>");
83 htmlf("<td class='mode'>"); 83 htmlf("<td class='mode'>");
84 if (is_null_sha1(info->new_sha1)) { 84 if (is_null_sha1(info->new_sha1)) {
85 cgit_print_filemode(info->old_mode); 85 cgit_print_filemode(info->old_mode);
86 } else { 86 } else {
87 cgit_print_filemode(info->new_mode); 87 cgit_print_filemode(info->new_mode);
88 } 88 }
89 89
90 if (info->old_mode != info->new_mode && 90 if (info->old_mode != info->new_mode &&
91 !is_null_sha1(info->old_sha1) && 91 !is_null_sha1(info->old_sha1) &&
92 !is_null_sha1(info->new_sha1)) { 92 !is_null_sha1(info->new_sha1)) {
93 html("<span class='modechange'>["); 93 html("<span class='modechange'>[");
94 cgit_print_filemode(info->old_mode); 94 cgit_print_filemode(info->old_mode);
95 html("]</span>"); 95 html("]</span>");
96 } 96 }
97 htmlf("</td><td class='%s'>", class); 97 htmlf("</td><td class='%s'>", class);
98 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,
99 ctx.qry.sha2, info->new_path, 0); 99 ctx.qry.sha2, info->new_path, 0);
100 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) 100 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED)
101 htmlf(" (%s from %s)", 101 htmlf(" (%s from %s)",
102 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed", 102 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed",
103 info->old_path); 103 info->old_path);
104 html("</td><td class='right'>"); 104 html("</td><td class='right'>");
105 if (info->binary) { 105 if (info->binary) {
106 htmlf("bin</td><td class='graph'>%ld -> %ld bytes", 106 htmlf("bin</td><td class='graph'>%ld -> %ld bytes",
107 info->old_size, info->new_size); 107 info->old_size, info->new_size);
108 return; 108 return;
109 } 109 }
110 htmlf("%d", info->added + info->removed); 110 htmlf("%d", info->added + info->removed);
111 html("</td><td class='graph'>"); 111 html("</td><td class='graph'>");
112 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));
113 htmlf("<td class='add' style='width: %.1f%%;'/>", 113 htmlf("<td class='add' style='width: %.1f%%;'/>",
114 info->added * 100.0 / max_changes); 114 info->added * 100.0 / max_changes);
115 htmlf("<td class='rem' style='width: %.1f%%;'/>", 115 htmlf("<td class='rem' style='width: %.1f%%;'/>",
116 info->removed * 100.0 / max_changes); 116 info->removed * 100.0 / max_changes);
117 htmlf("<td class='none' style='width: %.1f%%;'/>", 117 htmlf("<td class='none' style='width: %.1f%%;'/>",
118 (max_changes - info->removed - info->added) * 100.0 / max_changes); 118 (max_changes - info->removed - info->added) * 100.0 / max_changes);
119 html("</tr></table></td></tr>\n"); 119 html("</tr></table></td></tr>\n");
120} 120}
121 121
122static void count_diff_lines(char *line, int len) 122static void count_diff_lines(char *line, int len)
123{ 123{
124 if (line && (len > 0)) { 124 if (line && (len > 0)) {
125 if (line[0] == '+') 125 if (line[0] == '+')
126 lines_added++; 126 lines_added++;
127 else if (line[0] == '-') 127 else if (line[0] == '-')
128 lines_removed++; 128 lines_removed++;
129 } 129 }
130} 130}
131 131
132static void inspect_filepair(struct diff_filepair *pair) 132static void inspect_filepair(struct diff_filepair *pair)
133{ 133{
134 int binary = 0; 134 int binary = 0;
135 unsigned long old_size = 0; 135 unsigned long old_size = 0;
136 unsigned long new_size = 0; 136 unsigned long new_size = 0;
137 files++; 137 files++;
138 lines_added = 0; 138 lines_added = 0;
139 lines_removed = 0; 139 lines_removed = 0;
140 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,
141 &binary, 0, ctx.qry.ignorews, count_diff_lines); 141 &binary, 0, ctx.qry.ignorews, count_diff_lines);
142 if (files >= slots) { 142 if (files >= slots) {
143 if (slots == 0) 143 if (slots == 0)
144 slots = 4; 144 slots = 4;
145 else 145 else
146 slots = slots * 2; 146 slots = slots * 2;
147 items = xrealloc(items, slots * sizeof(struct fileinfo)); 147 items = xrealloc(items, slots * sizeof(struct fileinfo));
148 } 148 }
149 items[files-1].status = pair->status; 149 items[files-1].status = pair->status;
150 hashcpy(items[files-1].old_sha1, pair->one->sha1); 150 hashcpy(items[files-1].old_sha1, pair->one->sha1);
151 hashcpy(items[files-1].new_sha1, pair->two->sha1); 151 hashcpy(items[files-1].new_sha1, pair->two->sha1);
152 items[files-1].old_mode = pair->one->mode; 152 items[files-1].old_mode = pair->one->mode;
153 items[files-1].new_mode = pair->two->mode; 153 items[files-1].new_mode = pair->two->mode;
154 items[files-1].old_path = xstrdup(pair->one->path); 154 items[files-1].old_path = xstrdup(pair->one->path);
155 items[files-1].new_path = xstrdup(pair->two->path); 155 items[files-1].new_path = xstrdup(pair->two->path);
156 items[files-1].added = lines_added; 156 items[files-1].added = lines_added;
157 items[files-1].removed = lines_removed; 157 items[files-1].removed = lines_removed;
158 items[files-1].old_size = old_size; 158 items[files-1].old_size = old_size;
159 items[files-1].new_size = new_size; 159 items[files-1].new_size = new_size;
160 items[files-1].binary = binary; 160 items[files-1].binary = binary;
161 if (lines_added + lines_removed > max_changes) 161 if (lines_added + lines_removed > max_changes)
162 max_changes = lines_added + lines_removed; 162 max_changes = lines_added + lines_removed;
163 total_adds += lines_added; 163 total_adds += lines_added;
164 total_rems += lines_removed; 164 total_rems += lines_removed;
165} 165}
166 166
167void cgit_print_diffstat(const unsigned char *old_sha1, 167void cgit_print_diffstat(const unsigned char *old_sha1,
168 const unsigned char *new_sha1, const char *prefix) 168 const unsigned char *new_sha1, const char *prefix)
169{ 169{
170 int i, save_context = ctx.qry.context; 170 int i, save_context = ctx.qry.context;
171 171
172 html("<div class='diffstat-header'>"); 172 html("<div class='diffstat-header'>");
173 cgit_diff_link("Diffstat", NULL, NULL, ctx.qry.head, ctx.qry.sha1, 173 cgit_diff_link("Diffstat", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
174 ctx.qry.sha2, NULL, 0); 174 ctx.qry.sha2, NULL, 0);
175 if (prefix) { 175 if (prefix) {
176 html(" (limited to '"); 176 html(" (limited to '");
177 html_txt(prefix); 177 html_txt(prefix);
178 html("')"); 178 html("')");
179 } 179 }
180 html(" ("); 180 html(" (");
181 ctx.qry.context = (save_context > 0 ? save_context : 3) << 1; 181 ctx.qry.context = (save_context > 0 ? save_context : 3) << 1;
182 cgit_self_link("more", NULL, NULL, &ctx); 182 cgit_self_link("more", NULL, NULL, &ctx);
183 html("/"); 183 html("/");
184 ctx.qry.context = (save_context > 3 ? save_context : 3) >> 1; 184 ctx.qry.context = (save_context > 3 ? save_context : 3) >> 1;
185 cgit_self_link("less", NULL, NULL, &ctx); 185 cgit_self_link("less", NULL, NULL, &ctx);
186 ctx.qry.context = save_context; 186 ctx.qry.context = save_context;
187 html(" context)"); 187 html(" context)");
188 html(" ("); 188 html(" (");
189 ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2; 189 ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2;
190 cgit_self_link(ctx.qry.ignorews ? "ignore" : "show", NULL, NULL, &ctx); 190 cgit_self_link(ctx.qry.ignorews ? "ignore" : "show", NULL, NULL, &ctx);
191 ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2; 191 ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2;
192 html(" whitespace changes)"); 192 html(" whitespace changes)");
193 html("</div>"); 193 html("</div>");
194 html("<table summary='diffstat' class='diffstat'>"); 194 html("<table summary='diffstat' class='diffstat'>");
195 max_changes = 0; 195 max_changes = 0;
196 cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, prefix, 196 cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, prefix,
197 ctx.qry.ignorews); 197 ctx.qry.ignorews);
198 for(i = 0; i<files; i++) 198 for(i = 0; i<files; i++)
199 print_fileinfo(&items[i]); 199 print_fileinfo(&items[i]);
200 html("</table>"); 200 html("</table>");
201 html("<div class='diffstat-summary'>"); 201 html("<div class='diffstat-summary'>");
202 htmlf("%d files changed, %d insertions, %d deletions", 202 htmlf("%d files changed, %d insertions, %d deletions",
203 files, total_adds, total_rems); 203 files, total_adds, total_rems);
204 html("</div>"); 204 html("</div>");
205} 205}
206 206
207 207
208/* 208/*
209 * print a single line returned from xdiff 209 * print a single line returned from xdiff
210 */ 210 */
211static void print_line(char *line, int len) 211static void print_line(char *line, int len)
212{ 212{
213 char *class = "ctx"; 213 char *class = "ctx";
214 char c = line[len-1]; 214 char c = line[len-1];
215 215
216 if (line[0] == '+') 216 if (line[0] == '+')
217 class = "add"; 217 class = "add";
218 else if (line[0] == '-') 218 else if (line[0] == '-')
219 class = "del"; 219 class = "del";
220 else if (line[0] == '@') 220 else if (line[0] == '@')
221 class = "hunk"; 221 class = "hunk";
222 222
223 htmlf("<div class='%s'>", class); 223 htmlf("<div class='%s'>", class);
224 line[len-1] = '\0'; 224 line[len-1] = '\0';
225 html_txt(line); 225 html_txt(line);
226 html("</div>"); 226 html("</div>");
227 line[len-1] = c; 227 line[len-1] = c;
228} 228}
229 229
230static void header(unsigned char *sha1, char *path1, int mode1, 230static void header(unsigned char *sha1, char *path1, int mode1,
231 unsigned char *sha2, char *path2, int mode2) 231 unsigned char *sha2, char *path2, int mode2)
232{ 232{
233 char *abbrev1, *abbrev2; 233 char *abbrev1, *abbrev2;
234 int subproject; 234 int subproject;
235 235
236 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); 236 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
237 html("<div class='head'>"); 237 html("<div class='head'>");
238 html("diff --git a/"); 238 html("diff --git a/");
239 html_txt(path1); 239 html_txt(path1);
240 html(" b/"); 240 html(" b/");
241 html_txt(path2); 241 html_txt(path2);
242 242
243 if (is_null_sha1(sha1)) 243 if (is_null_sha1(sha1))
244 path1 = "dev/null"; 244 path1 = "dev/null";
245 if (is_null_sha1(sha2)) 245 if (is_null_sha1(sha2))
246 path2 = "dev/null"; 246 path2 = "dev/null";
247 247
248 if (mode1 == 0) 248 if (mode1 == 0)
249 htmlf("<br/>new file mode %.6o", mode2); 249 htmlf("<br/>new file mode %.6o", mode2);
250 250
251 if (mode2 == 0) 251 if (mode2 == 0)
252 htmlf("<br/>deleted file mode %.6o", mode1); 252 htmlf("<br/>deleted file mode %.6o", mode1);
253 253
254 if (!subproject) { 254 if (!subproject) {
255 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); 255 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
256 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); 256 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
257 htmlf("<br/>index %s..%s", abbrev1, abbrev2); 257 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
258 free(abbrev1); 258 free(abbrev1);
259 free(abbrev2); 259 free(abbrev2);
260 if (mode1 != 0 && mode2 != 0) { 260 if (mode1 != 0 && mode2 != 0) {
261 htmlf(" %.6o", mode1); 261 htmlf(" %.6o", mode1);
262 if (mode2 != mode1) 262 if (mode2 != mode1)
263 htmlf("..%.6o", mode2); 263 htmlf("..%.6o", mode2);
264 } 264 }
265 html("<br/>--- a/"); 265 html("<br/>--- a/");
266 if (mode1 != 0) 266 if (mode1 != 0)
267 cgit_tree_link(path1, NULL, NULL, ctx.qry.head, 267 cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
268 sha1_to_hex(old_rev_sha1), path1); 268 sha1_to_hex(old_rev_sha1), path1);
269 else 269 else
270 html_txt(path1); 270 html_txt(path1);
271 html("<br/>+++ b/"); 271 html("<br/>+++ b/");
272 if (mode2 != 0) 272 if (mode2 != 0)
273 cgit_tree_link(path2, NULL, NULL, ctx.qry.head, 273 cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
274 sha1_to_hex(new_rev_sha1), path2); 274 sha1_to_hex(new_rev_sha1), path2);
275 else 275 else
276 html_txt(path2); 276 html_txt(path2);
277 } 277 }
278 html("</div>"); 278 html("</div>");
279} 279}
280 280
281static void print_ssdiff_link() 281static void print_ssdiff_link()
282{ 282{
283 if (!strcmp(ctx.qry.page, "diff")) { 283 if (!strcmp(ctx.qry.page, "diff")) {
284 if (use_ssdiff) 284 if (use_ssdiff)
285 cgit_diff_link("Unidiff", NULL, NULL, ctx.qry.head, 285 cgit_diff_link("Unidiff", NULL, NULL, ctx.qry.head,
286 ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1); 286 ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1);
287 else 287 else
288 cgit_diff_link("Side-by-side diff", NULL, NULL, 288 cgit_diff_link("Side-by-side diff", NULL, NULL,
289 ctx.qry.head, ctx.qry.sha1, 289 ctx.qry.head, ctx.qry.sha1,
290 ctx.qry.sha2, ctx.qry.path, 1); 290 ctx.qry.sha2, ctx.qry.path, 1);
291 } 291 }
292} 292}
293 293
294static void filepair_cb(struct diff_filepair *pair) 294static void filepair_cb(struct diff_filepair *pair)
295{ 295{
296 unsigned long old_size = 0; 296 unsigned long old_size = 0;
297 unsigned long new_size = 0; 297 unsigned long new_size = 0;
298 int binary = 0; 298 int binary = 0;
299 linediff_fn print_line_fn = print_line; 299 linediff_fn print_line_fn = print_line;
300 300
301 current_filepair = pair; 301 current_filepair = pair;
302 if (use_ssdiff) { 302 if (use_ssdiff) {
303 cgit_ssdiff_header_begin(); 303 cgit_ssdiff_header_begin();
304 print_line_fn = cgit_ssdiff_line_cb; 304 print_line_fn = cgit_ssdiff_line_cb;
305 } 305 }
306 header(pair->one->sha1, pair->one->path, pair->one->mode, 306 header(pair->one->sha1, pair->one->path, pair->one->mode,
307 pair->two->sha1, pair->two->path, pair->two->mode); 307 pair->two->sha1, pair->two->path, pair->two->mode);
308 if (use_ssdiff) 308 if (use_ssdiff)
309 cgit_ssdiff_header_end(); 309 cgit_ssdiff_header_end();
310 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { 310 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
311 if (S_ISGITLINK(pair->one->mode)) 311 if (S_ISGITLINK(pair->one->mode))
312 print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); 312 print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
313 if (S_ISGITLINK(pair->two->mode)) 313 if (S_ISGITLINK(pair->two->mode))
314 print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); 314 print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
315 if (use_ssdiff) 315 if (use_ssdiff)
316 cgit_ssdiff_footer(); 316 cgit_ssdiff_footer();
317 return; 317 return;
318 } 318 }
319 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, 319 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
320 &new_size, &binary, ctx.qry.context, 320 &new_size, &binary, ctx.qry.context,
321 ctx.qry.ignorews, print_line_fn)) 321 ctx.qry.ignorews, print_line_fn))
322 cgit_print_error("Error running diff"); 322 cgit_print_error("Error running diff");
323 if (binary) { 323 if (binary) {
324 if (use_ssdiff) 324 if (use_ssdiff)
325 html("<tr><td colspan='4'>Binary files differ</td></tr>"); 325 html("<tr><td colspan='4'>Binary files differ</td></tr>");
326 else 326 else
327 html("Binary files differ"); 327 html("Binary files differ");
328 } 328 }
329 if (use_ssdiff) 329 if (use_ssdiff)
330 cgit_ssdiff_footer(); 330 cgit_ssdiff_footer();
331} 331}
332 332
333void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) 333void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix)
334{ 334{
335 enum object_type type; 335 enum object_type type;
336 unsigned long size; 336 unsigned long size;
337 struct commit *commit, *commit2; 337 struct commit *commit, *commit2;
338 338
339 if (!new_rev) 339 if (!new_rev)
340 new_rev = ctx.qry.head; 340 new_rev = ctx.qry.head;
341 get_sha1(new_rev, new_rev_sha1); 341 get_sha1(new_rev, new_rev_sha1);
342 type = sha1_object_info(new_rev_sha1, &size); 342 type = sha1_object_info(new_rev_sha1, &size);
343 if (type == OBJ_BAD) { 343 if (type == OBJ_BAD) {
344 cgit_print_error(fmt("Bad object name: %s", new_rev)); 344 cgit_print_error(fmt("Bad object name: %s", new_rev));
345 return; 345 return;
346 } 346 }
347 commit = lookup_commit_reference(new_rev_sha1); 347 commit = lookup_commit_reference(new_rev_sha1);
348 if (!commit || parse_commit(commit)) 348 if (!commit || parse_commit(commit)) {
349 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1))); 349 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
350 return;
351 }
350 352
351 if (old_rev) 353 if (old_rev)
352 get_sha1(old_rev, old_rev_sha1); 354 get_sha1(old_rev, old_rev_sha1);
353 else if (commit->parents && commit->parents->item) 355 else if (commit->parents && commit->parents->item)
354 hashcpy(old_rev_sha1, commit->parents->item->object.sha1); 356 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
355 else 357 else
356 hashclr(old_rev_sha1); 358 hashclr(old_rev_sha1);
357 359
358 if (!is_null_sha1(old_rev_sha1)) { 360 if (!is_null_sha1(old_rev_sha1)) {
359 type = sha1_object_info(old_rev_sha1, &size); 361 type = sha1_object_info(old_rev_sha1, &size);
360 if (type == OBJ_BAD) { 362 if (type == OBJ_BAD) {
361 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1))); 363 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
362 return; 364 return;
363 } 365 }
364 commit2 = lookup_commit_reference(old_rev_sha1); 366 commit2 = lookup_commit_reference(old_rev_sha1);
365 if (!commit2 || parse_commit(commit2)) 367 if (!commit2 || parse_commit(commit2)) {
366 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1))); 368 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
369 return;
370 }
367 } 371 }
368 372
369 if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff)) 373 if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff))
370 use_ssdiff = 1; 374 use_ssdiff = 1;
371 375
372 print_ssdiff_link(); 376 print_ssdiff_link();
373 cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix); 377 cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix);
374 378
375 if (use_ssdiff) { 379 if (use_ssdiff) {
376 html("<table summary='ssdiff' class='ssdiff'>"); 380 html("<table summary='ssdiff' class='ssdiff'>");
377 } else { 381 } else {
378 html("<table summary='diff' class='diff'>"); 382 html("<table summary='diff' class='diff'>");
379 html("<tr><td>"); 383 html("<tr><td>");
380 } 384 }
381 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix, 385 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix,
382 ctx.qry.ignorews); 386 ctx.qry.ignorews);
383 if (!use_ssdiff) 387 if (!use_ssdiff)
384 html("</td></tr>"); 388 html("</td></tr>");
385 html("</table>"); 389 html("</table>");
386} 390}