author | Lukasz Janyst <ljanyst@cern.ch> | 2011-03-05 13:10:55 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2011-03-05 13:13:06 (UTC) |
commit | 7f3c6e0ce9b41142cf2707af100992acdce059df (patch) (unidiff) | |
tree | 119a1920c85adcc65017afc8d9d95ab3e2bafef4 /ui-diff.c | |
parent | 1b09cbd303d889ec2636127584d57b7f1b70c25e (diff) | |
download | cgit-7f3c6e0ce9b41142cf2707af100992acdce059df.zip cgit-7f3c6e0ce9b41142cf2707af100992acdce059df.tar.gz cgit-7f3c6e0ce9b41142cf2707af100992acdce059df.tar.bz2 |
ui-diff.c: avoid html injection
When path-filtering was used in commit-view, the path filter was
included without proper html escaping. This patch closes the hole.
Signed-off-by: Lukasz Janyst <ljanyst@cern.ch>
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | ui-diff.c | 7 |
1 files changed, 5 insertions, 2 deletions
@@ -1,383 +1,386 @@ | |||
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 | ||
14 | unsigned char old_rev_sha1[20]; | 14 | unsigned char old_rev_sha1[20]; |
15 | unsigned char new_rev_sha1[20]; | 15 | unsigned char new_rev_sha1[20]; |
16 | 16 | ||
17 | static int files, slots; | 17 | static int files, slots; |
18 | static int total_adds, total_rems, max_changes; | 18 | static int total_adds, total_rems, max_changes; |
19 | static int lines_added, lines_removed; | 19 | static int lines_added, lines_removed; |
20 | 20 | ||
21 | static struct fileinfo { | 21 | static 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 | ||
36 | static int use_ssdiff = 0; | 36 | static int use_ssdiff = 0; |
37 | static struct diff_filepair *current_filepair; | 37 | static struct diff_filepair *current_filepair; |
38 | 38 | ||
39 | struct diff_filespec *cgit_get_current_old_file(void) | 39 | struct diff_filespec *cgit_get_current_old_file(void) |
40 | { | 40 | { |
41 | return current_filepair->one; | 41 | return current_filepair->one; |
42 | } | 42 | } |
43 | 43 | ||
44 | struct diff_filespec *cgit_get_current_new_file(void) | 44 | struct diff_filespec *cgit_get_current_new_file(void) |
45 | { | 45 | { |
46 | return current_filepair->two; | 46 | return current_filepair->two; |
47 | } | 47 | } |
48 | 48 | ||
49 | static void print_fileinfo(struct fileinfo *info) | 49 | static 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 | ||
122 | static void count_diff_lines(char *line, int len) | 122 | static 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 | ||
132 | static void inspect_filepair(struct diff_filepair *pair) | 132 | static 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 | ||
167 | void cgit_print_diffstat(const unsigned char *old_sha1, | 167 | void 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 | htmlf(" (limited to '%s')", prefix); | 176 | html(" (limited to '"); |
177 | html_txt(prefix); | ||
178 | html("')"); | ||
179 | } | ||
177 | html(" ("); | 180 | html(" ("); |
178 | ctx.qry.context = (save_context > 0 ? save_context : 3) << 1; | 181 | ctx.qry.context = (save_context > 0 ? save_context : 3) << 1; |
179 | cgit_self_link("more", NULL, NULL, &ctx); | 182 | cgit_self_link("more", NULL, NULL, &ctx); |
180 | html("/"); | 183 | html("/"); |
181 | ctx.qry.context = (save_context > 3 ? save_context : 3) >> 1; | 184 | ctx.qry.context = (save_context > 3 ? save_context : 3) >> 1; |
182 | cgit_self_link("less", NULL, NULL, &ctx); | 185 | cgit_self_link("less", NULL, NULL, &ctx); |
183 | ctx.qry.context = save_context; | 186 | ctx.qry.context = save_context; |
184 | html(" context)"); | 187 | html(" context)"); |
185 | html(" ("); | 188 | html(" ("); |
186 | ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2; | 189 | ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2; |
187 | cgit_self_link(ctx.qry.ignorews ? "ignore" : "show", NULL, NULL, &ctx); | 190 | cgit_self_link(ctx.qry.ignorews ? "ignore" : "show", NULL, NULL, &ctx); |
188 | ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2; | 191 | ctx.qry.ignorews = (ctx.qry.ignorews + 1) % 2; |
189 | html(" whitespace changes)"); | 192 | html(" whitespace changes)"); |
190 | html("</div>"); | 193 | html("</div>"); |
191 | html("<table summary='diffstat' class='diffstat'>"); | 194 | html("<table summary='diffstat' class='diffstat'>"); |
192 | max_changes = 0; | 195 | max_changes = 0; |
193 | cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, prefix, | 196 | cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, prefix, |
194 | ctx.qry.ignorews); | 197 | ctx.qry.ignorews); |
195 | for(i = 0; i<files; i++) | 198 | for(i = 0; i<files; i++) |
196 | print_fileinfo(&items[i]); | 199 | print_fileinfo(&items[i]); |
197 | html("</table>"); | 200 | html("</table>"); |
198 | html("<div class='diffstat-summary'>"); | 201 | html("<div class='diffstat-summary'>"); |
199 | htmlf("%d files changed, %d insertions, %d deletions", | 202 | htmlf("%d files changed, %d insertions, %d deletions", |
200 | files, total_adds, total_rems); | 203 | files, total_adds, total_rems); |
201 | html("</div>"); | 204 | html("</div>"); |
202 | } | 205 | } |
203 | 206 | ||
204 | 207 | ||
205 | /* | 208 | /* |
206 | * print a single line returned from xdiff | 209 | * print a single line returned from xdiff |
207 | */ | 210 | */ |
208 | static void print_line(char *line, int len) | 211 | static void print_line(char *line, int len) |
209 | { | 212 | { |
210 | char *class = "ctx"; | 213 | char *class = "ctx"; |
211 | char c = line[len-1]; | 214 | char c = line[len-1]; |
212 | 215 | ||
213 | if (line[0] == '+') | 216 | if (line[0] == '+') |
214 | class = "add"; | 217 | class = "add"; |
215 | else if (line[0] == '-') | 218 | else if (line[0] == '-') |
216 | class = "del"; | 219 | class = "del"; |
217 | else if (line[0] == '@') | 220 | else if (line[0] == '@') |
218 | class = "hunk"; | 221 | class = "hunk"; |
219 | 222 | ||
220 | htmlf("<div class='%s'>", class); | 223 | htmlf("<div class='%s'>", class); |
221 | line[len-1] = '\0'; | 224 | line[len-1] = '\0'; |
222 | html_txt(line); | 225 | html_txt(line); |
223 | html("</div>"); | 226 | html("</div>"); |
224 | line[len-1] = c; | 227 | line[len-1] = c; |
225 | } | 228 | } |
226 | 229 | ||
227 | static void header(unsigned char *sha1, char *path1, int mode1, | 230 | static void header(unsigned char *sha1, char *path1, int mode1, |
228 | unsigned char *sha2, char *path2, int mode2) | 231 | unsigned char *sha2, char *path2, int mode2) |
229 | { | 232 | { |
230 | char *abbrev1, *abbrev2; | 233 | char *abbrev1, *abbrev2; |
231 | int subproject; | 234 | int subproject; |
232 | 235 | ||
233 | subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); | 236 | subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); |
234 | html("<div class='head'>"); | 237 | html("<div class='head'>"); |
235 | html("diff --git a/"); | 238 | html("diff --git a/"); |
236 | html_txt(path1); | 239 | html_txt(path1); |
237 | html(" b/"); | 240 | html(" b/"); |
238 | html_txt(path2); | 241 | html_txt(path2); |
239 | 242 | ||
240 | if (is_null_sha1(sha1)) | 243 | if (is_null_sha1(sha1)) |
241 | path1 = "dev/null"; | 244 | path1 = "dev/null"; |
242 | if (is_null_sha1(sha2)) | 245 | if (is_null_sha1(sha2)) |
243 | path2 = "dev/null"; | 246 | path2 = "dev/null"; |
244 | 247 | ||
245 | if (mode1 == 0) | 248 | if (mode1 == 0) |
246 | htmlf("<br/>new file mode %.6o", mode2); | 249 | htmlf("<br/>new file mode %.6o", mode2); |
247 | 250 | ||
248 | if (mode2 == 0) | 251 | if (mode2 == 0) |
249 | htmlf("<br/>deleted file mode %.6o", mode1); | 252 | htmlf("<br/>deleted file mode %.6o", mode1); |
250 | 253 | ||
251 | if (!subproject) { | 254 | if (!subproject) { |
252 | abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); | 255 | abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); |
253 | abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); | 256 | abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); |
254 | htmlf("<br/>index %s..%s", abbrev1, abbrev2); | 257 | htmlf("<br/>index %s..%s", abbrev1, abbrev2); |
255 | free(abbrev1); | 258 | free(abbrev1); |
256 | free(abbrev2); | 259 | free(abbrev2); |
257 | if (mode1 != 0 && mode2 != 0) { | 260 | if (mode1 != 0 && mode2 != 0) { |
258 | htmlf(" %.6o", mode1); | 261 | htmlf(" %.6o", mode1); |
259 | if (mode2 != mode1) | 262 | if (mode2 != mode1) |
260 | htmlf("..%.6o", mode2); | 263 | htmlf("..%.6o", mode2); |
261 | } | 264 | } |
262 | html("<br/>--- a/"); | 265 | html("<br/>--- a/"); |
263 | if (mode1 != 0) | 266 | if (mode1 != 0) |
264 | cgit_tree_link(path1, NULL, NULL, ctx.qry.head, | 267 | cgit_tree_link(path1, NULL, NULL, ctx.qry.head, |
265 | sha1_to_hex(old_rev_sha1), path1); | 268 | sha1_to_hex(old_rev_sha1), path1); |
266 | else | 269 | else |
267 | html_txt(path1); | 270 | html_txt(path1); |
268 | html("<br/>+++ b/"); | 271 | html("<br/>+++ b/"); |
269 | if (mode2 != 0) | 272 | if (mode2 != 0) |
270 | cgit_tree_link(path2, NULL, NULL, ctx.qry.head, | 273 | cgit_tree_link(path2, NULL, NULL, ctx.qry.head, |
271 | sha1_to_hex(new_rev_sha1), path2); | 274 | sha1_to_hex(new_rev_sha1), path2); |
272 | else | 275 | else |
273 | html_txt(path2); | 276 | html_txt(path2); |
274 | } | 277 | } |
275 | html("</div>"); | 278 | html("</div>"); |
276 | } | 279 | } |
277 | 280 | ||
278 | static void print_ssdiff_link() | 281 | static void print_ssdiff_link() |
279 | { | 282 | { |
280 | if (!strcmp(ctx.qry.page, "diff")) { | 283 | if (!strcmp(ctx.qry.page, "diff")) { |
281 | if (use_ssdiff) | 284 | if (use_ssdiff) |
282 | cgit_diff_link("Unidiff", NULL, NULL, ctx.qry.head, | 285 | cgit_diff_link("Unidiff", NULL, NULL, ctx.qry.head, |
283 | ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1); | 286 | ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1); |
284 | else | 287 | else |
285 | cgit_diff_link("Side-by-side diff", NULL, NULL, | 288 | cgit_diff_link("Side-by-side diff", NULL, NULL, |
286 | ctx.qry.head, ctx.qry.sha1, | 289 | ctx.qry.head, ctx.qry.sha1, |
287 | ctx.qry.sha2, ctx.qry.path, 1); | 290 | ctx.qry.sha2, ctx.qry.path, 1); |
288 | } | 291 | } |
289 | } | 292 | } |
290 | 293 | ||
291 | static void filepair_cb(struct diff_filepair *pair) | 294 | static void filepair_cb(struct diff_filepair *pair) |
292 | { | 295 | { |
293 | unsigned long old_size = 0; | 296 | unsigned long old_size = 0; |
294 | unsigned long new_size = 0; | 297 | unsigned long new_size = 0; |
295 | int binary = 0; | 298 | int binary = 0; |
296 | linediff_fn print_line_fn = print_line; | 299 | linediff_fn print_line_fn = print_line; |
297 | 300 | ||
298 | current_filepair = pair; | 301 | current_filepair = pair; |
299 | if (use_ssdiff) { | 302 | if (use_ssdiff) { |
300 | cgit_ssdiff_header_begin(); | 303 | cgit_ssdiff_header_begin(); |
301 | print_line_fn = cgit_ssdiff_line_cb; | 304 | print_line_fn = cgit_ssdiff_line_cb; |
302 | } | 305 | } |
303 | header(pair->one->sha1, pair->one->path, pair->one->mode, | 306 | header(pair->one->sha1, pair->one->path, pair->one->mode, |
304 | pair->two->sha1, pair->two->path, pair->two->mode); | 307 | pair->two->sha1, pair->two->path, pair->two->mode); |
305 | if (use_ssdiff) | 308 | if (use_ssdiff) |
306 | cgit_ssdiff_header_end(); | 309 | cgit_ssdiff_header_end(); |
307 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { | 310 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { |
308 | if (S_ISGITLINK(pair->one->mode)) | 311 | if (S_ISGITLINK(pair->one->mode)) |
309 | 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); |
310 | if (S_ISGITLINK(pair->two->mode)) | 313 | if (S_ISGITLINK(pair->two->mode)) |
311 | 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); |
312 | if (use_ssdiff) | 315 | if (use_ssdiff) |
313 | cgit_ssdiff_footer(); | 316 | cgit_ssdiff_footer(); |
314 | return; | 317 | return; |
315 | } | 318 | } |
316 | 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, |
317 | &new_size, &binary, ctx.qry.context, | 320 | &new_size, &binary, ctx.qry.context, |
318 | ctx.qry.ignorews, print_line_fn)) | 321 | ctx.qry.ignorews, print_line_fn)) |
319 | cgit_print_error("Error running diff"); | 322 | cgit_print_error("Error running diff"); |
320 | if (binary) { | 323 | if (binary) { |
321 | if (use_ssdiff) | 324 | if (use_ssdiff) |
322 | html("<tr><td colspan='4'>Binary files differ</td></tr>"); | 325 | html("<tr><td colspan='4'>Binary files differ</td></tr>"); |
323 | else | 326 | else |
324 | html("Binary files differ"); | 327 | html("Binary files differ"); |
325 | } | 328 | } |
326 | if (use_ssdiff) | 329 | if (use_ssdiff) |
327 | cgit_ssdiff_footer(); | 330 | cgit_ssdiff_footer(); |
328 | } | 331 | } |
329 | 332 | ||
330 | void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) | 333 | void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) |
331 | { | 334 | { |
332 | enum object_type type; | 335 | enum object_type type; |
333 | unsigned long size; | 336 | unsigned long size; |
334 | struct commit *commit, *commit2; | 337 | struct commit *commit, *commit2; |
335 | 338 | ||
336 | if (!new_rev) | 339 | if (!new_rev) |
337 | new_rev = ctx.qry.head; | 340 | new_rev = ctx.qry.head; |
338 | get_sha1(new_rev, new_rev_sha1); | 341 | get_sha1(new_rev, new_rev_sha1); |
339 | type = sha1_object_info(new_rev_sha1, &size); | 342 | type = sha1_object_info(new_rev_sha1, &size); |
340 | if (type == OBJ_BAD) { | 343 | if (type == OBJ_BAD) { |
341 | cgit_print_error(fmt("Bad object name: %s", new_rev)); | 344 | cgit_print_error(fmt("Bad object name: %s", new_rev)); |
342 | return; | 345 | return; |
343 | } | 346 | } |
344 | commit = lookup_commit_reference(new_rev_sha1); | 347 | commit = lookup_commit_reference(new_rev_sha1); |
345 | if (!commit || parse_commit(commit)) | 348 | if (!commit || parse_commit(commit)) |
346 | 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))); |
347 | 350 | ||
348 | if (old_rev) | 351 | if (old_rev) |
349 | get_sha1(old_rev, old_rev_sha1); | 352 | get_sha1(old_rev, old_rev_sha1); |
350 | else if (commit->parents && commit->parents->item) | 353 | else if (commit->parents && commit->parents->item) |
351 | hashcpy(old_rev_sha1, commit->parents->item->object.sha1); | 354 | hashcpy(old_rev_sha1, commit->parents->item->object.sha1); |
352 | else | 355 | else |
353 | hashclr(old_rev_sha1); | 356 | hashclr(old_rev_sha1); |
354 | 357 | ||
355 | if (!is_null_sha1(old_rev_sha1)) { | 358 | if (!is_null_sha1(old_rev_sha1)) { |
356 | type = sha1_object_info(old_rev_sha1, &size); | 359 | type = sha1_object_info(old_rev_sha1, &size); |
357 | if (type == OBJ_BAD) { | 360 | if (type == OBJ_BAD) { |
358 | cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1))); | 361 | cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1))); |
359 | return; | 362 | return; |
360 | } | 363 | } |
361 | commit2 = lookup_commit_reference(old_rev_sha1); | 364 | commit2 = lookup_commit_reference(old_rev_sha1); |
362 | if (!commit2 || parse_commit(commit2)) | 365 | if (!commit2 || parse_commit(commit2)) |
363 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1))); | 366 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1))); |
364 | } | 367 | } |
365 | 368 | ||
366 | if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff)) | 369 | if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff)) |
367 | use_ssdiff = 1; | 370 | use_ssdiff = 1; |
368 | 371 | ||
369 | print_ssdiff_link(); | 372 | print_ssdiff_link(); |
370 | cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix); | 373 | cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix); |
371 | 374 | ||
372 | if (use_ssdiff) { | 375 | if (use_ssdiff) { |
373 | html("<table summary='ssdiff' class='ssdiff'>"); | 376 | html("<table summary='ssdiff' class='ssdiff'>"); |
374 | } else { | 377 | } else { |
375 | html("<table summary='diff' class='diff'>"); | 378 | html("<table summary='diff' class='diff'>"); |
376 | html("<tr><td>"); | 379 | html("<tr><td>"); |
377 | } | 380 | } |
378 | cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix, | 381 | cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix, |
379 | ctx.qry.ignorews); | 382 | ctx.qry.ignorews); |
380 | if (!use_ssdiff) | 383 | if (!use_ssdiff) |
381 | html("</td></tr>"); | 384 | html("</td></tr>"); |
382 | html("</table>"); | 385 | html("</table>"); |
383 | } | 386 | } |