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