]> git.cameronkatri.com Git - cgit.git/blob - ui-commit.c
Add cgit_diff_link()
[cgit.git] / ui-commit.c
1 /* ui-commit.c: generate commit view
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"
10
11 static int files, slots;
12 static int total_adds, total_rems, max_changes;
13 static int lines_added, lines_removed;
14 static char *curr_rev;
15
16 static struct fileinfo {
17 char status;
18 unsigned char old_sha1[20];
19 unsigned char new_sha1[20];
20 unsigned short old_mode;
21 unsigned short new_mode;
22 char *old_path;
23 char *new_path;
24 unsigned int added;
25 unsigned int removed;
26 } *items;
27
28
29 void print_fileinfo(struct fileinfo *info)
30 {
31 char *class;
32
33 switch (info->status) {
34 case DIFF_STATUS_ADDED:
35 class = "add";
36 break;
37 case DIFF_STATUS_COPIED:
38 class = "cpy";
39 break;
40 case DIFF_STATUS_DELETED:
41 class = "del";
42 break;
43 case DIFF_STATUS_MODIFIED:
44 class = "upd";
45 break;
46 case DIFF_STATUS_RENAMED:
47 class = "mov";
48 break;
49 case DIFF_STATUS_TYPE_CHANGED:
50 class = "typ";
51 break;
52 case DIFF_STATUS_UNKNOWN:
53 class = "unk";
54 break;
55 case DIFF_STATUS_UNMERGED:
56 class = "stg";
57 break;
58 default:
59 die("bug: unhandled diff status %c", info->status);
60 }
61
62 html("<tr>");
63 htmlf("<td class='mode'>");
64 if (is_null_sha1(info->new_sha1)) {
65 html_filemode(info->old_mode);
66 } else {
67 html_filemode(info->new_mode);
68 }
69
70 if (info->old_mode != info->new_mode &&
71 !is_null_sha1(info->old_sha1) &&
72 !is_null_sha1(info->new_sha1)) {
73 html("<span class='modechange'>[");
74 html_filemode(info->old_mode);
75 html("]</span>");
76 }
77 htmlf("</td><td class='%s'>", class);
78 cgit_tree_link(info->new_path, NULL, NULL, cgit_query_head, curr_rev,
79 info->new_path);
80 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED)
81 htmlf(" (%s from %s)",
82 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed",
83 info->old_path);
84 html("</td><td class='right'>");
85 htmlf("%d", info->added + info->removed);
86 html("</td><td class='graph'>");
87 htmlf("<table width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
88 htmlf("<td class='add' style='width: %.1f%%;'/>",
89 info->added * 100.0 / max_changes);
90 htmlf("<td class='rem' style='width: %.1f%%;'/>",
91 info->removed * 100.0 / max_changes);
92 htmlf("<td class='none' style='width: %.1f%%;'/>",
93 (max_changes - info->removed - info->added) * 100.0 / max_changes);
94 html("</tr></table></td></tr>\n");
95 }
96
97 void cgit_count_diff_lines(char *line, int len)
98 {
99 if (line && (len > 0)) {
100 if (line[0] == '+')
101 lines_added++;
102 else if (line[0] == '-')
103 lines_removed++;
104 }
105 }
106
107 void inspect_filepair(struct diff_filepair *pair)
108 {
109 files++;
110 lines_added = 0;
111 lines_removed = 0;
112 cgit_diff_files(pair->one->sha1, pair->two->sha1, cgit_count_diff_lines);
113 if (files >= slots) {
114 if (slots == 0)
115 slots = 4;
116 else
117 slots = slots * 2;
118 items = xrealloc(items, slots * sizeof(struct fileinfo));
119 }
120 items[files-1].status = pair->status;
121 hashcpy(items[files-1].old_sha1, pair->one->sha1);
122 hashcpy(items[files-1].new_sha1, pair->two->sha1);
123 items[files-1].old_mode = pair->one->mode;
124 items[files-1].new_mode = pair->two->mode;
125 items[files-1].old_path = xstrdup(pair->one->path);
126 items[files-1].new_path = xstrdup(pair->two->path);
127 items[files-1].added = lines_added;
128 items[files-1].removed = lines_removed;
129 if (lines_added + lines_removed > max_changes)
130 max_changes = lines_added + lines_removed;
131 total_adds += lines_added;
132 total_rems += lines_removed;
133 }
134
135
136 void cgit_print_commit(char *hex)
137 {
138 struct commit *commit, *parent;
139 struct commitinfo *info;
140 struct commit_list *p;
141 unsigned char sha1[20];
142 char *filename;
143 char *tmp;
144 int i;
145
146 if (!hex)
147 hex = cgit_query_head;
148 curr_rev = hex;
149
150 if (get_sha1(hex, sha1)) {
151 cgit_print_error(fmt("Bad object id: %s", hex));
152 return;
153 }
154 commit = lookup_commit_reference(sha1);
155 if (!commit) {
156 cgit_print_error(fmt("Bad commit reference: %s", hex));
157 return;
158 }
159 info = cgit_parse_commit(commit);
160
161 html("<table class='commit-info'>\n");
162 html("<tr><th>author</th><td>");
163 html_txt(info->author);
164 html(" ");
165 html_txt(info->author_email);
166 html("</td><td class='right'>");
167 cgit_print_date(info->author_date, FMT_LONGDATE);
168 html("</td></tr>\n");
169 html("<tr><th>committer</th><td>");
170 html_txt(info->committer);
171 html(" ");
172 html_txt(info->committer_email);
173 html("</td><td class='right'>");
174 cgit_print_date(info->committer_date, FMT_LONGDATE);
175 html("</td></tr>\n");
176 html("<tr><th>tree</th><td colspan='2' class='sha1'>");
177 tmp = xstrdup(hex);
178 cgit_tree_link(sha1_to_hex(commit->tree->object.sha1), NULL, NULL,
179 cgit_query_head, tmp, NULL);
180 html("</td></tr>\n");
181 for (p = commit->parents; p ; p = p->next) {
182 parent = lookup_commit_reference(p->item->object.sha1);
183 if (!parent) {
184 html("<tr><td colspan='3'>");
185 cgit_print_error("Error reading parent commit");
186 html("</td></tr>");
187 continue;
188 }
189 html("<tr><th>parent</th>"
190 "<td colspan='2' class='sha1'>");
191 cgit_commit_link(sha1_to_hex(p->item->object.sha1), NULL, NULL,
192 cgit_query_head, sha1_to_hex(p->item->object.sha1));
193 html(" (");
194 cgit_diff_link("diff", NULL, NULL, cgit_query_head, hex,
195 sha1_to_hex(p->item->object.sha1), NULL);
196 html(")</td></tr>");
197 }
198 if (cgit_repo->snapshots) {
199 htmlf("<tr><th>download</th><td colspan='2' class='sha1'><a href='");
200 filename = fmt("%s-%s.zip", cgit_query_repo, hex);
201 html_attr(cgit_pageurl(cgit_query_repo, "snapshot",
202 fmt("id=%s&amp;name=%s", hex, filename)));
203 htmlf("'>%s</a></td></tr>", filename);
204 }
205 html("</table>\n");
206 html("<div class='commit-subject'>");
207 html_txt(info->subject);
208 html("</div>");
209 html("<div class='commit-msg'>");
210 html_txt(info->msg);
211 html("</div>");
212 if (!(commit->parents && commit->parents->next && commit->parents->next->next)) {
213 html("<div class='diffstat-header'>Diffstat</div>");
214 html("<table class='diffstat'>");
215 max_changes = 0;
216 cgit_diff_commit(commit, inspect_filepair);
217 for(i = 0; i<files; i++)
218 print_fileinfo(&items[i]);
219 html("</table>");
220 html("<div class='diffstat-summary'>");
221 htmlf("%d files changed, %d insertions, %d deletions (",
222 files, total_adds, total_rems);
223 cgit_diff_link("show diff", NULL, NULL, cgit_query_head, hex,
224 NULL, NULL);
225 html(")</div>");
226 }
227 cgit_free_commitinfo(info);
228 }