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];
17 * print a single line returned from xdiff
19 static void print_line(char *line, int len)
26 else if (line[0] == '-')
28 else if (line[0] == '@')
31 htmlf("<div class='%s'>", class);
38 static void header(unsigned char *sha1, char *path1, int mode1,
39 unsigned char *sha2, char *path2, int mode2)
41 char *abbrev1, *abbrev2;
44 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
45 html("<div class='head'>");
46 html("diff --git a/");
51 if (is_null_sha1(sha1))
53 if (is_null_sha1(sha2))
57 htmlf("<br/>new file mode %.6o", mode2);
60 htmlf("<br/>deleted file mode %.6o", mode1);
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);
68 if (mode1 != 0 && mode2 != 0) {
69 htmlf(" %.6o", mode1);
71 htmlf("..%.6o", mode2);
75 cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
76 sha1_to_hex(old_rev_sha1), path1);
81 cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
82 sha1_to_hex(new_rev_sha1), path2);
89 static void filepair_cb(struct diff_filepair *pair)
91 header(pair->one->sha1, pair->one->path, pair->one->mode,
92 pair->two->sha1, pair->two->path, pair->two->mode);
93 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
94 if (S_ISGITLINK(pair->one->mode))
95 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
96 if (S_ISGITLINK(pair->two->mode))
97 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
100 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line))
101 cgit_print_error("Error running diff");
104 void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix)
106 enum object_type type;
108 struct commit *commit, *commit2;
111 new_rev = ctx.qry.head;
112 get_sha1(new_rev, new_rev_sha1);
113 type = sha1_object_info(new_rev_sha1, &size);
114 if (type == OBJ_BAD) {
115 cgit_print_error(fmt("Bad object name: %s", new_rev));
118 if (type != OBJ_COMMIT) {
119 cgit_print_error(fmt("Unhandled object type: %s",
124 commit = lookup_commit_reference(new_rev_sha1);
125 if (!commit || parse_commit(commit))
126 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
129 get_sha1(old_rev, old_rev_sha1);
130 else if (commit->parents && commit->parents->item)
131 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
133 hashclr(old_rev_sha1);
135 if (!is_null_sha1(old_rev_sha1)) {
136 type = sha1_object_info(old_rev_sha1, &size);
137 if (type == OBJ_BAD) {
138 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
141 commit2 = lookup_commit_reference(old_rev_sha1);
142 if (!commit2 || parse_commit(commit2))
143 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
145 html("<table summary='diff' class='diff'>");
147 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix);