summaryrefslogtreecommitdiffabout
path: root/ui-diff.c
Unidiff
Diffstat (limited to 'ui-diff.c') (more/less context) (ignore whitespace changes)
-rw-r--r--ui-diff.c42
1 files changed, 26 insertions, 16 deletions
diff --git a/ui-diff.c b/ui-diff.c
index ba0030f..ac9a3fa 100644
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -1,139 +1,149 @@
1/* ui-diff.c: show diff between two blobs 1/* ui-diff.c: show diff between two blobs
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11 11
12unsigned char old_rev_sha1[20];
13unsigned char new_rev_sha1[20];
14
12/* 15/*
13 * print a single line returned from xdiff 16 * print a single line returned from xdiff
14 */ 17 */
15static void print_line(char *line, int len) 18static void print_line(char *line, int len)
16{ 19{
17 char *class = "ctx"; 20 char *class = "ctx";
18 char c = line[len-1]; 21 char c = line[len-1];
19 22
20 if (line[0] == '+') 23 if (line[0] == '+')
21 class = "add"; 24 class = "add";
22 else if (line[0] == '-') 25 else if (line[0] == '-')
23 class = "del"; 26 class = "del";
24 else if (line[0] == '@') 27 else if (line[0] == '@')
25 class = "hunk"; 28 class = "hunk";
26 29
27 htmlf("<div class='%s'>", class); 30 htmlf("<div class='%s'>", class);
28 line[len-1] = '\0'; 31 line[len-1] = '\0';
29 html_txt(line); 32 html_txt(line);
30 html("</div>"); 33 html("</div>");
31 line[len-1] = c; 34 line[len-1] = c;
32} 35}
33 36
34static void header(unsigned char *sha1, char *path1, int mode1, 37static void header(unsigned char *sha1, char *path1, int mode1,
35 unsigned char *sha2, char *path2, int mode2) 38 unsigned char *sha2, char *path2, int mode2)
36{ 39{
37 char *abbrev1, *abbrev2; 40 char *abbrev1, *abbrev2;
38 int subproject; 41 int subproject;
39 42
40 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); 43 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
41 html("<div class='head'>"); 44 html("<div class='head'>");
42 html("diff --git a/"); 45 html("diff --git a/");
43 html_txt(path1); 46 html_txt(path1);
44 html(" b/"); 47 html(" b/");
45 html_txt(path2); 48 html_txt(path2);
46 49
47 if (is_null_sha1(sha1)) 50 if (is_null_sha1(sha1))
48 path1 = "dev/null"; 51 path1 = "dev/null";
49 if (is_null_sha1(sha2)) 52 if (is_null_sha1(sha2))
50 path2 = "dev/null"; 53 path2 = "dev/null";
51 54
52 if (mode1 == 0) 55 if (mode1 == 0)
53 htmlf("<br/>new file mode %.6o", mode2); 56 htmlf("<br/>new file mode %.6o", mode2);
54 57
55 if (mode2 == 0) 58 if (mode2 == 0)
56 htmlf("<br/>deleted file mode %.6o", mode1); 59 htmlf("<br/>deleted file mode %.6o", mode1);
57 60
58 if (!subproject) { 61 if (!subproject) {
59 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); 62 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
60 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); 63 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
61 htmlf("<br/>index %s..%s", abbrev1, abbrev2); 64 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
62 free(abbrev1); 65 free(abbrev1);
63 free(abbrev2); 66 free(abbrev2);
64 if (mode1 != 0 && mode2 != 0) { 67 if (mode1 != 0 && mode2 != 0) {
65 htmlf(" %.6o", mode1); 68 htmlf(" %.6o", mode1);
66 if (mode2 != mode1) 69 if (mode2 != mode1)
67 htmlf("..%.6o", mode2); 70 htmlf("..%.6o", mode2);
68 } 71 }
69 html("<br/>--- a/"); 72 html("<br/>--- a/");
70 html_txt(path1); 73 if (mode1 != 0)
74 cgit_tree_link(path1, NULL, NULL, cgit_query_head,
75 sha1_to_hex(old_rev_sha1), path1);
76 else
77 html_txt(path1);
71 html("<br/>+++ b/"); 78 html("<br/>+++ b/");
72 html_txt(path2); 79 if (mode2 != 0)
80 cgit_tree_link(path2, NULL, NULL, cgit_query_head,
81 sha1_to_hex(new_rev_sha1), path2);
82 else
83 html_txt(path2);
73 } 84 }
74 html("</div>"); 85 html("</div>");
75} 86}
76 87
77static void filepair_cb(struct diff_filepair *pair) 88static void filepair_cb(struct diff_filepair *pair)
78{ 89{
79 header(pair->one->sha1, pair->one->path, pair->one->mode, 90 header(pair->one->sha1, pair->one->path, pair->one->mode,
80 pair->two->sha1, pair->two->path, pair->two->mode); 91 pair->two->sha1, pair->two->path, pair->two->mode);
81 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { 92 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
82 if (S_ISGITLINK(pair->one->mode)) 93 if (S_ISGITLINK(pair->one->mode))
83 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); 94 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
84 if (S_ISGITLINK(pair->two->mode)) 95 if (S_ISGITLINK(pair->two->mode))
85 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); 96 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
86 return; 97 return;
87 } 98 }
88 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) 99 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line))
89 cgit_print_error("Error running diff"); 100 cgit_print_error("Error running diff");
90} 101}
91 102
92void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) 103void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix)
93{ 104{
94 unsigned char sha1[20], sha2[20];
95 enum object_type type; 105 enum object_type type;
96 unsigned long size; 106 unsigned long size;
97 struct commit *commit, *commit2; 107 struct commit *commit, *commit2;
98 108
99 if (!new_rev) 109 if (!new_rev)
100 new_rev = cgit_query_head; 110 new_rev = cgit_query_head;
101 get_sha1(new_rev, sha1); 111 get_sha1(new_rev, new_rev_sha1);
102 type = sha1_object_info(sha1, &size); 112 type = sha1_object_info(new_rev_sha1, &size);
103 if (type == OBJ_BAD) { 113 if (type == OBJ_BAD) {
104 cgit_print_error(fmt("Bad object name: %s", new_rev)); 114 cgit_print_error(fmt("Bad object name: %s", new_rev));
105 return; 115 return;
106 } 116 }
107 if (type != OBJ_COMMIT) { 117 if (type != OBJ_COMMIT) {
108 cgit_print_error(fmt("Unhandled object type: %s", 118 cgit_print_error(fmt("Unhandled object type: %s",
109 typename(type))); 119 typename(type)));
110 return; 120 return;
111 } 121 }
112 122
113 commit = lookup_commit_reference(sha1); 123 commit = lookup_commit_reference(new_rev_sha1);
114 if (!commit || parse_commit(commit)) 124 if (!commit || parse_commit(commit))
115 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(sha1))); 125 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
116 126
117 if (old_rev) 127 if (old_rev)
118 get_sha1(old_rev, sha2); 128 get_sha1(old_rev, old_rev_sha1);
119 else if (commit->parents && commit->parents->item) 129 else if (commit->parents && commit->parents->item)
120 hashcpy(sha2, commit->parents->item->object.sha1); 130 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
121 else 131 else
122 hashclr(sha2); 132 hashclr(old_rev_sha1);
123 133
124 if (!is_null_sha1(sha2)) { 134 if (!is_null_sha1(old_rev_sha1)) {
125 type = sha1_object_info(sha2, &size); 135 type = sha1_object_info(old_rev_sha1, &size);
126 if (type == OBJ_BAD) { 136 if (type == OBJ_BAD) {
127 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(sha2))); 137 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
128 return; 138 return;
129 } 139 }
130 commit2 = lookup_commit_reference(sha2); 140 commit2 = lookup_commit_reference(old_rev_sha1);
131 if (!commit2 || parse_commit(commit2)) 141 if (!commit2 || parse_commit(commit2))
132 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(sha2))); 142 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
133 } 143 }
134 html("<table class='diff'>"); 144 html("<table class='diff'>");
135 html("<tr><td>"); 145 html("<tr><td>");
136 cgit_diff_tree(sha2, sha1, filepair_cb, prefix); 146 cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix);
137 html("</td></tr>"); 147 html("</td></tr>");
138 html("</table>"); 148 html("</table>");
139} 149}