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