1 /* ui-diff.c: show diff between two blobs
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 #include "ui-shared.h"
13 unsigned char old_rev_sha1[20];
14 unsigned char new_rev_sha1[20];
16 static int files, slots;
17 static int total_adds, total_rems, max_changes;
18 static int lines_added, lines_removed;
19 static char *curr_rev;
21 static struct fileinfo {
23 unsigned char old_sha1[20];
24 unsigned char new_sha1[20];
25 unsigned short old_mode;
26 unsigned short new_mode;
34 static void print_fileinfo(struct fileinfo *info)
38 switch (info->status) {
39 case DIFF_STATUS_ADDED:
42 case DIFF_STATUS_COPIED:
45 case DIFF_STATUS_DELETED:
48 case DIFF_STATUS_MODIFIED:
51 case DIFF_STATUS_RENAMED:
54 case DIFF_STATUS_TYPE_CHANGED:
57 case DIFF_STATUS_UNKNOWN:
60 case DIFF_STATUS_UNMERGED:
64 die("bug: unhandled diff status %c", info->status);
68 htmlf("<td class='mode'>");
69 if (is_null_sha1(info->new_sha1)) {
70 cgit_print_filemode(info->old_mode);
72 cgit_print_filemode(info->new_mode);
75 if (info->old_mode != info->new_mode &&
76 !is_null_sha1(info->old_sha1) &&
77 !is_null_sha1(info->new_sha1)) {
78 html("<span class='modechange'>[");
79 cgit_print_filemode(info->old_mode);
82 htmlf("</td><td class='%s'>", class);
83 cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, curr_rev,
84 NULL, info->new_path);
85 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED)
86 htmlf(" (%s from %s)",
87 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed",
89 html("</td><td class='right'>");
90 htmlf("%d", info->added + info->removed);
91 html("</td><td class='graph'>");
92 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
93 htmlf("<td class='add' style='width: %.1f%%;'/>",
94 info->added * 100.0 / max_changes);
95 htmlf("<td class='rem' style='width: %.1f%%;'/>",
96 info->removed * 100.0 / max_changes);
97 htmlf("<td class='none' style='width: %.1f%%;'/>",
98 (max_changes - info->removed - info->added) * 100.0 / max_changes);
99 html("</tr></table></td></tr>\n");
102 static void count_diff_lines(char *line, int len)
104 if (line && (len > 0)) {
107 else if (line[0] == '-')
112 static void inspect_filepair(struct diff_filepair *pair)
117 cgit_diff_files(pair->one->sha1, pair->two->sha1, count_diff_lines);
118 if (files >= slots) {
123 items = xrealloc(items, slots * sizeof(struct fileinfo));
125 items[files-1].status = pair->status;
126 hashcpy(items[files-1].old_sha1, pair->one->sha1);
127 hashcpy(items[files-1].new_sha1, pair->two->sha1);
128 items[files-1].old_mode = pair->one->mode;
129 items[files-1].new_mode = pair->two->mode;
130 items[files-1].old_path = xstrdup(pair->one->path);
131 items[files-1].new_path = xstrdup(pair->two->path);
132 items[files-1].added = lines_added;
133 items[files-1].removed = lines_removed;
134 if (lines_added + lines_removed > max_changes)
135 max_changes = lines_added + lines_removed;
136 total_adds += lines_added;
137 total_rems += lines_removed;
140 void cgit_print_diffstat(const unsigned char *old_sha1,
141 const unsigned char *new_sha1)
145 html("<div class='diffstat-header'>Diffstat</div>");
146 html("<table summary='diffstat' class='diffstat'>");
148 cgit_diff_tree(old_sha1, new_sha1, inspect_filepair, NULL);
149 for(i = 0; i<files; i++)
150 print_fileinfo(&items[i]);
152 html("<div class='diffstat-summary'>");
153 htmlf("%d files changed, %d insertions, %d deletions",
154 files, total_adds, total_rems);
160 * print a single line returned from xdiff
162 static void print_line(char *line, int len)
165 char c = line[len-1];
169 else if (line[0] == '-')
171 else if (line[0] == '@')
174 htmlf("<div class='%s'>", class);
181 static void header(unsigned char *sha1, char *path1, int mode1,
182 unsigned char *sha2, char *path2, int mode2)
184 char *abbrev1, *abbrev2;
187 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
188 html("<div class='head'>");
189 html("diff --git a/");
194 if (is_null_sha1(sha1))
196 if (is_null_sha1(sha2))
200 htmlf("<br/>new file mode %.6o", mode2);
203 htmlf("<br/>deleted file mode %.6o", mode1);
206 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
207 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
208 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
211 if (mode1 != 0 && mode2 != 0) {
212 htmlf(" %.6o", mode1);
214 htmlf("..%.6o", mode2);
218 cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
219 sha1_to_hex(old_rev_sha1), path1);
224 cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
225 sha1_to_hex(new_rev_sha1), path2);
232 static void filepair_cb(struct diff_filepair *pair)
234 header(pair->one->sha1, pair->one->path, pair->one->mode,
235 pair->two->sha1, pair->two->path, pair->two->mode);
236 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
237 if (S_ISGITLINK(pair->one->mode))
238 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
239 if (S_ISGITLINK(pair->two->mode))
240 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
243 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line))
244 cgit_print_error("Error running diff");
247 void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix)
249 enum object_type type;
251 struct commit *commit, *commit2;
254 new_rev = ctx.qry.head;
255 get_sha1(new_rev, new_rev_sha1);
256 type = sha1_object_info(new_rev_sha1, &size);
257 if (type == OBJ_BAD) {
258 cgit_print_error(fmt("Bad object name: %s", new_rev));
261 if (type != OBJ_COMMIT) {
262 cgit_print_error(fmt("Unhandled object type: %s",
267 commit = lookup_commit_reference(new_rev_sha1);
268 if (!commit || parse_commit(commit))
269 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
272 get_sha1(old_rev, old_rev_sha1);
273 else if (commit->parents && commit->parents->item)
274 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
276 hashclr(old_rev_sha1);
278 if (!is_null_sha1(old_rev_sha1)) {
279 type = sha1_object_info(old_rev_sha1, &size);
280 if (type == OBJ_BAD) {
281 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
284 commit2 = lookup_commit_reference(old_rev_sha1);
285 if (!commit2 || parse_commit(commit2))
286 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
288 cgit_print_diffstat(old_rev_sha1, new_rev_sha1);
290 html("<table summary='diff' class='diff'>");
292 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix);