blob: 2a2200900044d9fb66a80048c9f3f07aa3d45d62 [file] [log] [blame]
Lars Hjemli36aba002006-12-20 22:48:27 +01001/* 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"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010010#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010011#include "ui-shared.h"
Lars Hjemli36aba002006-12-20 22:48:27 +010012
Lars Hjemlie238ebe2007-10-01 12:30:29 +020013unsigned char old_rev_sha1[20];
14unsigned char new_rev_sha1[20];
15
Lars Hjemli36aba002006-12-20 22:48:27 +010016/*
17 * print a single line returned from xdiff
18 */
19static void print_line(char *line, int len)
20{
21 char *class = "ctx";
22 char c = line[len-1];
23
24 if (line[0] == '+')
25 class = "add";
26 else if (line[0] == '-')
27 class = "del";
28 else if (line[0] == '@')
29 class = "hunk";
30
31 htmlf("<div class='%s'>", class);
32 line[len-1] = '\0';
33 html_txt(line);
34 html("</div>");
35 line[len-1] = c;
36}
37
Lars Hjemlif4f13392007-05-16 04:21:06 +020038static void header(unsigned char *sha1, char *path1, int mode1,
39 unsigned char *sha2, char *path2, int mode2)
Lars Hjemlia342ac62007-05-14 18:31:05 +020040{
41 char *abbrev1, *abbrev2;
Lars Hjemlif4f13392007-05-16 04:21:06 +020042 int subproject;
43
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -050044 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
Lars Hjemlia342ac62007-05-14 18:31:05 +020045 html("<div class='head'>");
46 html("diff --git a/");
47 html_txt(path1);
48 html(" b/");
49 html_txt(path2);
Lars Hjemlif4f13392007-05-16 04:21:06 +020050
51 if (is_null_sha1(sha1))
52 path1 = "dev/null";
53 if (is_null_sha1(sha2))
54 path2 = "dev/null";
55
56 if (mode1 == 0)
57 htmlf("<br/>new file mode %.6o", mode2);
58
59 if (mode2 == 0)
60 htmlf("<br/>deleted file mode %.6o", mode1);
61
62 if (!subproject) {
63 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
64 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
65 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
66 free(abbrev1);
67 free(abbrev2);
68 if (mode1 != 0 && mode2 != 0) {
69 htmlf(" %.6o", mode1);
70 if (mode2 != mode1)
71 htmlf("..%.6o", mode2);
72 }
73 html("<br/>--- a/");
Lars Hjemlie238ebe2007-10-01 12:30:29 +020074 if (mode1 != 0)
Lars Hjemlid14d77f2008-02-16 11:53:40 +010075 cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
Lars Hjemlie238ebe2007-10-01 12:30:29 +020076 sha1_to_hex(old_rev_sha1), path1);
77 else
78 html_txt(path1);
Lars Hjemlif4f13392007-05-16 04:21:06 +020079 html("<br/>+++ b/");
Lars Hjemlie238ebe2007-10-01 12:30:29 +020080 if (mode2 != 0)
Lars Hjemlid14d77f2008-02-16 11:53:40 +010081 cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
Lars Hjemlie238ebe2007-10-01 12:30:29 +020082 sha1_to_hex(new_rev_sha1), path2);
83 else
84 html_txt(path2);
Lars Hjemlif4f13392007-05-16 04:21:06 +020085 }
Lars Hjemlia342ac62007-05-14 18:31:05 +020086 html("</div>");
87}
88
Lars Hjemli6a8749d2007-05-13 23:13:12 +020089static void filepair_cb(struct diff_filepair *pair)
90{
Lars Hjemlif4f13392007-05-16 04:21:06 +020091 header(pair->one->sha1, pair->one->path, pair->one->mode,
92 pair->two->sha1, pair->two->path, pair->two->mode);
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -050093 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
94 if (S_ISGITLINK(pair->one->mode))
Lars Hjemlif4f13392007-05-16 04:21:06 +020095 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -050096 if (S_ISGITLINK(pair->two->mode))
Lars Hjemlif4f13392007-05-16 04:21:06 +020097 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
98 return;
99 }
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200100 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line))
101 cgit_print_error("Error running diff");
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200102}
103
Lars Hjemli1a6025b2007-10-01 11:46:38 +0200104void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix)
Lars Hjemli36aba002006-12-20 22:48:27 +0100105{
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200106 enum object_type type;
107 unsigned long size;
Lars Hjemli4a0be582007-06-17 18:12:03 +0200108 struct commit *commit, *commit2;
Lars Hjemlif9ff7df2007-05-16 00:58:35 +0200109
Lars Hjemli4a0be582007-06-17 18:12:03 +0200110 if (!new_rev)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100111 new_rev = ctx.qry.head;
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200112 get_sha1(new_rev, new_rev_sha1);
113 type = sha1_object_info(new_rev_sha1, &size);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200114 if (type == OBJ_BAD) {
115 cgit_print_error(fmt("Bad object name: %s", new_rev));
116 return;
117 }
118 if (type != OBJ_COMMIT) {
119 cgit_print_error(fmt("Unhandled object type: %s",
120 typename(type)));
Lars Hjemlif9ff7df2007-05-16 00:58:35 +0200121 return;
122 }
Lars Hjemli36aba002006-12-20 22:48:27 +0100123
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200124 commit = lookup_commit_reference(new_rev_sha1);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200125 if (!commit || parse_commit(commit))
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200126 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
Lars Hjemli36aba002006-12-20 22:48:27 +0100127
Lars Hjemli4a0be582007-06-17 18:12:03 +0200128 if (old_rev)
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200129 get_sha1(old_rev, old_rev_sha1);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200130 else if (commit->parents && commit->parents->item)
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200131 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200132 else
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200133 hashclr(old_rev_sha1);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200134
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200135 if (!is_null_sha1(old_rev_sha1)) {
136 type = sha1_object_info(old_rev_sha1, &size);
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200137 if (type == OBJ_BAD) {
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200138 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200139 return;
140 }
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200141 commit2 = lookup_commit_reference(old_rev_sha1);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200142 if (!commit2 || parse_commit(commit2))
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200143 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200144 }
Lars Hjemli29154832007-11-11 13:04:28 +0100145 html("<table summary='diff' class='diff'>");
Lars Hjemli4a0be582007-06-17 18:12:03 +0200146 html("<tr><td>");
Lars Hjemlie238ebe2007-10-01 12:30:29 +0200147 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix);
Lars Hjemli4a6201e2007-06-05 11:44:47 +0200148 html("</td></tr>");
Ondrej Jirman0928d882007-05-26 01:14:25 +0200149 html("</table>");
Lars Hjemli36aba002006-12-20 22:48:27 +0100150}