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)
12 unsigned char old_rev_sha1[20];
13 unsigned char new_rev_sha1[20];
16 * print a single line returned from xdiff
18 static void print_line(char *line, int len)
25 else if (line[0] == '-')
27 else if (line[0] == '@')
30 htmlf("<div class='%s'>", class);
37 static void header(unsigned char *sha1, char *path1, int mode1,
38 unsigned char *sha2, char *path2, int mode2)
40 char *abbrev1, *abbrev2;
43 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
44 html("<div class='head'>");
45 html("diff --git a/");
50 if (is_null_sha1(sha1))
52 if (is_null_sha1(sha2))
56 htmlf("<br/>new file mode %.6o", mode2);
59 htmlf("<br/>deleted file mode %.6o", mode1);
62 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
63 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
64 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
67 if (mode1 != 0 && mode2 != 0) {
68 htmlf(" %.6o", mode1);
70 htmlf("..%.6o", mode2);
74 cgit_tree_link(path1, NULL, NULL, cgit_query_head,
75 sha1_to_hex(old_rev_sha1), path1);
80 cgit_tree_link(path2, NULL, NULL, cgit_query_head,
81 sha1_to_hex(new_rev_sha1), path2);
88 static void filepair_cb(struct diff_filepair *pair)
90 header(pair->one->sha1, pair->one->path, pair->one->mode,
91 pair->two->sha1, pair->two->path, pair->two->mode);
92 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
93 if (S_ISGITLINK(pair->one->mode))
94 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
95 if (S_ISGITLINK(pair->two->mode))
96 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
99 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line))
100 cgit_print_error("Error running diff");
103 void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix)
105 enum object_type type;
107 struct commit *commit, *commit2;
110 new_rev = cgit_query_head;
111 get_sha1(new_rev, new_rev_sha1);
112 type = sha1_object_info(new_rev_sha1, &size);
113 if (type == OBJ_BAD) {
114 cgit_print_error(fmt("Bad object name: %s", new_rev));
117 if (type != OBJ_COMMIT) {
118 cgit_print_error(fmt("Unhandled object type: %s",
123 commit = lookup_commit_reference(new_rev_sha1);
124 if (!commit || parse_commit(commit))
125 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
128 get_sha1(old_rev, old_rev_sha1);
129 else if (commit->parents && commit->parents->item)
130 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
132 hashclr(old_rev_sha1);
134 if (!is_null_sha1(old_rev_sha1)) {
135 type = sha1_object_info(old_rev_sha1, &size);
136 if (type == OBJ_BAD) {
137 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
140 commit2 = lookup_commit_reference(old_rev_sha1);
141 if (!commit2 || parse_commit(commit2))
142 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
144 html("<table class='diff'>");
146 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix);