]> gitweb.ps.run Git - ps-cgit/blob - ui-diff.c
Merge branch 'lh/clone-url'
[ps-cgit] / ui-diff.c
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"
10 #include "html.h"
11 #include "ui-shared.h"
12 #include "ui-ssdiff.h"
13
14 unsigned char old_rev_sha1[20];
15 unsigned char new_rev_sha1[20];
16
17 static int files, slots;
18 static int total_adds, total_rems, max_changes;
19 static int lines_added, lines_removed;
20
21 static struct fileinfo {
22         char status;
23         unsigned char old_sha1[20];
24         unsigned char new_sha1[20];
25         unsigned short old_mode;
26         unsigned short new_mode;
27         char *old_path;
28         char *new_path;
29         unsigned int added;
30         unsigned int removed;
31         unsigned long old_size;
32         unsigned long new_size;
33         int binary:1;
34 } *items;
35
36 static int use_ssdiff = 0;
37 static struct diff_filepair *current_filepair;
38
39 struct diff_filespec *cgit_get_current_old_file(void)
40 {
41         return current_filepair->one;
42 }
43
44 struct diff_filespec *cgit_get_current_new_file(void)
45 {
46         return current_filepair->two;
47 }
48
49 static void print_fileinfo(struct fileinfo *info)
50 {
51         char *class;
52
53         switch (info->status) {
54         case DIFF_STATUS_ADDED:
55                 class = "add";
56                 break;
57         case DIFF_STATUS_COPIED:
58                 class = "cpy";
59                 break;
60         case DIFF_STATUS_DELETED:
61                 class = "del";
62                 break;
63         case DIFF_STATUS_MODIFIED:
64                 class = "upd";
65                 break;
66         case DIFF_STATUS_RENAMED:
67                 class = "mov";
68                 break;
69         case DIFF_STATUS_TYPE_CHANGED:
70                 class = "typ";
71                 break;
72         case DIFF_STATUS_UNKNOWN:
73                 class = "unk";
74                 break;
75         case DIFF_STATUS_UNMERGED:
76                 class = "stg";
77                 break;
78         default:
79                 die("bug: unhandled diff status %c", info->status);
80         }
81
82         html("<tr>");
83         htmlf("<td class='mode'>");
84         if (is_null_sha1(info->new_sha1)) {
85                 cgit_print_filemode(info->old_mode);
86         } else {
87                 cgit_print_filemode(info->new_mode);
88         }
89
90         if (info->old_mode != info->new_mode &&
91             !is_null_sha1(info->old_sha1) &&
92             !is_null_sha1(info->new_sha1)) {
93                 html("<span class='modechange'>[");
94                 cgit_print_filemode(info->old_mode);
95                 html("]</span>");
96         }
97         htmlf("</td><td class='%s'>", class);
98         cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
99                        ctx.qry.sha2, info->new_path, 0);
100         if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED)
101                 htmlf(" (%s from %s)",
102                       info->status == DIFF_STATUS_COPIED ? "copied" : "renamed",
103                       info->old_path);
104         html("</td><td class='right'>");
105         if (info->binary) {
106                 htmlf("bin</td><td class='graph'>%ld -> %ld bytes",
107                       info->old_size, info->new_size);
108                 return;
109         }
110         htmlf("%d", info->added + info->removed);
111         html("</td><td class='graph'>");
112         htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
113         htmlf("<td class='add' style='width: %.1f%%;'/>",
114               info->added * 100.0 / max_changes);
115         htmlf("<td class='rem' style='width: %.1f%%;'/>",
116               info->removed * 100.0 / max_changes);
117         htmlf("<td class='none' style='width: %.1f%%;'/>",
118               (max_changes - info->removed - info->added) * 100.0 / max_changes);
119         html("</tr></table></td></tr>\n");
120 }
121
122 static void count_diff_lines(char *line, int len)
123 {
124         if (line && (len > 0)) {
125                 if (line[0] == '+')
126                         lines_added++;
127                 else if (line[0] == '-')
128                         lines_removed++;
129         }
130 }
131
132 static void inspect_filepair(struct diff_filepair *pair)
133 {
134         int binary = 0;
135         unsigned long old_size = 0;
136         unsigned long new_size = 0;
137         files++;
138         lines_added = 0;
139         lines_removed = 0;
140         cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, &new_size,
141                         &binary, 0, ctx.qry.ignorews, count_diff_lines);
142         if (files >= slots) {
143                 if (slots == 0)
144                         slots = 4;
145                 else
146                         slots = slots * 2;
147                 items = xrealloc(items, slots * sizeof(struct fileinfo));
148         }
149         items[files-1].status = pair->status;
150         hashcpy(items[files-1].old_sha1, pair->one->sha1);
151         hashcpy(items[files-1].new_sha1, pair->two->sha1);
152         items[files-1].old_mode = pair->one->mode;
153         items[files-1].new_mode = pair->two->mode;
154         items[files-1].old_path = xstrdup(pair->one->path);
155         items[files-1].new_path = xstrdup(pair->two->path);
156         items[files-1].added = lines_added;
157         items[files-1].removed = lines_removed;
158         items[files-1].old_size = old_size;
159         items[files-1].new_size = new_size;
160         items[files-1].binary = binary;
161         if (lines_added + lines_removed > max_changes)
162                 max_changes = lines_added + lines_removed;
163         total_adds += lines_added;
164         total_rems += lines_removed;
165 }
166
167 void cgit_print_diffstat(const unsigned char *old_sha1,
168                          const unsigned char *new_sha1, const char *prefix)
169 {
170         int i;
171
172         html("<div class='diffstat-header'>");
173         cgit_diff_link("Diffstat", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
174                        ctx.qry.sha2, NULL, 0);
175         if (prefix) {
176                 html(" (limited to '");
177                 html_txt(prefix);
178                 html("')");
179         }
180         html("</div>");
181         html("<table summary='diffstat' class='diffstat'>");
182         max_changes = 0;
183         cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, prefix,
184                        ctx.qry.ignorews);
185         for(i = 0; i<files; i++)
186                 print_fileinfo(&items[i]);
187         html("</table>");
188         html("<div class='diffstat-summary'>");
189         htmlf("%d files changed, %d insertions, %d deletions",
190               files, total_adds, total_rems);
191         html("</div>");
192 }
193
194
195 /*
196  * print a single line returned from xdiff
197  */
198 static void print_line(char *line, int len)
199 {
200         char *class = "ctx";
201         char c = line[len-1];
202
203         if (line[0] == '+')
204                 class = "add";
205         else if (line[0] == '-')
206                 class = "del";
207         else if (line[0] == '@')
208                 class = "hunk";
209
210         htmlf("<div class='%s'>", class);
211         line[len-1] = '\0';
212         html_txt(line);
213         html("</div>");
214         line[len-1] = c;
215 }
216
217 static void header(unsigned char *sha1, char *path1, int mode1,
218                    unsigned char *sha2, char *path2, int mode2)
219 {
220         char *abbrev1, *abbrev2;
221         int subproject;
222
223         subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
224         html("<div class='head'>");
225         html("diff --git a/");
226         html_txt(path1);
227         html(" b/");
228         html_txt(path2);
229
230         if (is_null_sha1(sha1))
231                 path1 = "dev/null";
232         if (is_null_sha1(sha2))
233                 path2 = "dev/null";
234
235         if (mode1 == 0)
236                 htmlf("<br/>new file mode %.6o", mode2);
237
238         if (mode2 == 0)
239                 htmlf("<br/>deleted file mode %.6o", mode1);
240
241         if (!subproject) {
242                 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
243                 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
244                 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
245                 free(abbrev1);
246                 free(abbrev2);
247                 if (mode1 != 0 && mode2 != 0) {
248                         htmlf(" %.6o", mode1);
249                         if (mode2 != mode1)
250                                 htmlf("..%.6o", mode2);
251                 }
252                 html("<br/>--- a/");
253                 if (mode1 != 0)
254                         cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
255                                        sha1_to_hex(old_rev_sha1), path1);
256                 else
257                         html_txt(path1);
258                 html("<br/>+++ b/");
259                 if (mode2 != 0)
260                         cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
261                                        sha1_to_hex(new_rev_sha1), path2);
262                 else
263                         html_txt(path2);
264         }
265         html("</div>");
266 }
267
268 static void filepair_cb(struct diff_filepair *pair)
269 {
270         unsigned long old_size = 0;
271         unsigned long new_size = 0;
272         int binary = 0;
273         linediff_fn print_line_fn = print_line;
274
275         current_filepair = pair;
276         if (use_ssdiff) {
277                 cgit_ssdiff_header_begin();
278                 print_line_fn = cgit_ssdiff_line_cb;
279         }
280         header(pair->one->sha1, pair->one->path, pair->one->mode,
281                pair->two->sha1, pair->two->path, pair->two->mode);
282         if (use_ssdiff)
283                 cgit_ssdiff_header_end();
284         if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
285                 if (S_ISGITLINK(pair->one->mode))
286                         print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
287                 if (S_ISGITLINK(pair->two->mode))
288                         print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
289                 if (use_ssdiff)
290                         cgit_ssdiff_footer();
291                 return;
292         }
293         if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
294                             &new_size, &binary, ctx.qry.context,
295                             ctx.qry.ignorews, print_line_fn))
296                 cgit_print_error("Error running diff");
297         if (binary) {
298                 if (use_ssdiff)
299                         html("<tr><td colspan='4'>Binary files differ</td></tr>");
300                 else
301                         html("Binary files differ");
302         }
303         if (use_ssdiff)
304                 cgit_ssdiff_footer();
305 }
306
307 void cgit_print_diff_ctrls()
308 {
309         int i, curr;
310
311         html("<div class='cgit-panel'>");
312         html("<b>diff options</b>");
313         html("<form method='get' action='.'>");
314         cgit_add_hidden_formfields(1, 0, ctx.qry.page);
315         html("<table>");
316         html("<tr><td colspan='2'/></tr>");
317         html("<tr>");
318         html("<td class='label'>context:</td>");
319         html("<td class='ctrl'>");
320         html("<select name='context' onchange='this.form.submit();'>");
321         curr = ctx.qry.context;
322         if (!curr)
323                 curr = 3;
324         for (i = 1; i <= 10; i++)
325                 html_intoption(i, fmt("%d", i), curr);
326         for (i = 15; i <= 40; i += 5)
327                 html_intoption(i, fmt("%d", i), curr);
328         html("</select>");
329         html("</td>");
330         html("</tr><tr>");
331         html("<td class='label'>space:</td>");
332         html("<td class='ctrl'>");
333         html("<select name='ignorews' onchange='this.form.submit();'>");
334         html_intoption(0, "include", ctx.qry.ignorews);
335         html_intoption(1, "ignore", ctx.qry.ignorews);
336         html("</select>");
337         html("</td>");
338         html("</tr><tr>");
339         html("<td class='label'>mode:</td>");
340         html("<td class='ctrl'>");
341         html("<select name='ss' onchange='this.form.submit();'>");
342         curr = ctx.qry.ssdiff;
343         if (!curr && ctx.cfg.ssdiff)
344                 curr = 1;
345         html_intoption(0, "unified", curr);
346         html_intoption(1, "ssdiff", curr);
347         html("</select></td></tr>");
348         html("<tr><td/><td class='ctrl'>");
349         html("<noscript><input type='submit' value='reload'/></noscript>");
350         html("</td></tr></table>");
351         html("</form>");
352         html("</div>");
353 }
354
355 void cgit_print_diff(const char *new_rev, const char *old_rev,
356                      const char *prefix, int show_ctrls)
357 {
358         enum object_type type;
359         unsigned long size;
360         struct commit *commit, *commit2;
361
362         if (!new_rev)
363                 new_rev = ctx.qry.head;
364         get_sha1(new_rev, new_rev_sha1);
365         type = sha1_object_info(new_rev_sha1, &size);
366         if (type == OBJ_BAD) {
367                 cgit_print_error(fmt("Bad object name: %s", new_rev));
368                 return;
369         }
370         commit = lookup_commit_reference(new_rev_sha1);
371         if (!commit || parse_commit(commit)) {
372                 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
373                 return;
374         }
375
376         if (old_rev)
377                 get_sha1(old_rev, old_rev_sha1);
378         else if (commit->parents && commit->parents->item)
379                 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
380         else
381                 hashclr(old_rev_sha1);
382
383         if (!is_null_sha1(old_rev_sha1)) {
384                 type = sha1_object_info(old_rev_sha1, &size);
385                 if (type == OBJ_BAD) {
386                         cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
387                         return;
388                 }
389                 commit2 = lookup_commit_reference(old_rev_sha1);
390                 if (!commit2 || parse_commit(commit2)) {
391                         cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
392                         return;
393                 }
394         }
395
396         if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff))
397                 use_ssdiff = 1;
398
399         if (show_ctrls)
400                 cgit_print_diff_ctrls();
401
402         cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix);
403
404         if (use_ssdiff) {
405                 html("<table summary='ssdiff' class='ssdiff'>");
406         } else {
407                 html("<table summary='diff' class='diff'>");
408                 html("<tr><td>");
409         }
410         cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix,
411                        ctx.qry.ignorews);
412         if (!use_ssdiff)
413                 html("</td></tr>");
414         html("</table>");
415 }