blob: bd55a33827bca96978943f5f3e2b6310abc2a40a [file] [log] [blame]
Lars Hjemli6c14f5e2006-12-16 01:11:55 +01001/* 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
Lars Hjemli9a8f8862006-12-16 00:19:56 +01009#include "cgit.h"
10
Lars Hjemli9927e632007-05-14 23:58:29 +020011static int files, slots;
12static int total_adds, total_rems, max_changes;
13static int lines_added, lines_removed;
Lars Hjemli4a0be582007-06-17 18:12:03 +020014static char *curr_rev;
Lars Hjemli6cb326c2006-12-17 23:07:28 +010015
Lars Hjemli9927e632007-05-14 23:58:29 +020016static struct fileinfo {
Lars Hjemli8a3685b2007-05-13 22:25:14 +020017 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
29void print_fileinfo(struct fileinfo *info)
Lars Hjemli6cb326c2006-12-17 23:07:28 +010030{
Lars Hjemli6cb326c2006-12-17 23:07:28 +010031 char *class;
Lars Hjemliae4c1ee2007-05-13 11:26:23 +020032
Lars Hjemli8a3685b2007-05-13 22:25:14 +020033 switch (info->status) {
Lars Hjemli6cb326c2006-12-17 23:07:28 +010034 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:
Lars Hjemli8a3685b2007-05-13 22:25:14 +020059 die("bug: unhandled diff status %c", info->status);
Lars Hjemli6cb326c2006-12-17 23:07:28 +010060 }
61
62 html("<tr>");
63 htmlf("<td class='mode'>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +020064 if (is_null_sha1(info->new_sha1)) {
65 html_filemode(info->old_mode);
Lars Hjemlifb6e5862006-12-17 23:30:55 +010066 } else {
Lars Hjemli8a3685b2007-05-13 22:25:14 +020067 html_filemode(info->new_mode);
Lars Hjemlifb6e5862006-12-17 23:30:55 +010068 }
69
Lars Hjemli8a3685b2007-05-13 22:25:14 +020070 if (info->old_mode != info->new_mode &&
71 !is_null_sha1(info->old_sha1) &&
72 !is_null_sha1(info->new_sha1)) {
Lars Hjemli6cb326c2006-12-17 23:07:28 +010073 html("<span class='modechange'>[");
Lars Hjemli8a3685b2007-05-13 22:25:14 +020074 html_filemode(info->old_mode);
Lars Hjemli6cb326c2006-12-17 23:07:28 +010075 html("]</span>");
76 }
77 htmlf("</td><td class='%s'>", class);
Lars Hjemli0ec87912007-10-01 11:54:01 +020078 cgit_diff_link(info->new_path, NULL, NULL, cgit_query_head, curr_rev,
79 NULL, info->new_path);
Lars Hjemli4a0be582007-06-17 18:12:03 +020080 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);
Lars Hjemli8a3685b2007-05-13 22:25:14 +020084 html("</td><td class='right'>");
85 htmlf("%d", info->added + info->removed);
Lars Hjemli8a3685b2007-05-13 22:25:14 +020086 html("</td><td class='graph'>");
Lars Hjemli29154832007-11-11 13:04:28 +010087 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
Lars Hjemlie9030112007-05-15 02:13:11 +020088 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);
Ondrej Jirman0928d882007-05-26 01:14:25 +020094 html("</tr></table></td></tr>\n");
Lars Hjemli6cb326c2006-12-17 23:07:28 +010095}
96
Lars Hjemli8a3685b2007-05-13 22:25:14 +020097void 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
107void 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
Lars Hjemli4a0be582007-06-17 18:12:03 +0200136void cgit_print_commit(char *hex)
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100137{
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200138 struct commit *commit, *parent;
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100139 struct commitinfo *info;
140 struct commit_list *p;
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100141 unsigned char sha1[20];
Lars Hjemli44947bf2007-06-17 01:23:08 +0200142 char *tmp;
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200143 int i;
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100144
Lars Hjemli42a7eb92007-06-17 14:53:02 +0200145 if (!hex)
146 hex = cgit_query_head;
Lars Hjemli4a0be582007-06-17 18:12:03 +0200147 curr_rev = hex;
Lars Hjemli42a7eb92007-06-17 14:53:02 +0200148
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100149 if (get_sha1(hex, sha1)) {
150 cgit_print_error(fmt("Bad object id: %s", hex));
151 return;
152 }
Lars Hjemlifa82b032006-12-16 14:46:05 +0100153 commit = lookup_commit_reference(sha1);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100154 if (!commit) {
155 cgit_print_error(fmt("Bad commit reference: %s", hex));
156 return;
157 }
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100158 info = cgit_parse_commit(commit);
159
Lars Hjemli29154832007-11-11 13:04:28 +0100160 html("<table summary='commit info' class='commit-info'>\n");
Lars Hjemli8960d262006-12-16 14:28:26 +0100161 html("<tr><th>author</th><td>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100162 html_txt(info->author);
Lars Hjemli8960d262006-12-16 14:28:26 +0100163 html(" ");
164 html_txt(info->author_email);
165 html("</td><td class='right'>");
Lars Hjemli5db39172007-05-22 23:08:46 +0200166 cgit_print_date(info->author_date, FMT_LONGDATE);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100167 html("</td></tr>\n");
168 html("<tr><th>committer</th><td>");
169 html_txt(info->committer);
Lars Hjemli8960d262006-12-16 14:28:26 +0100170 html(" ");
171 html_txt(info->committer_email);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100172 html("</td><td class='right'>");
Lars Hjemli5db39172007-05-22 23:08:46 +0200173 cgit_print_date(info->committer_date, FMT_LONGDATE);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100174 html("</td></tr>\n");
Lars Hjemli44947bf2007-06-17 01:23:08 +0200175 html("<tr><th>tree</th><td colspan='2' class='sha1'>");
176 tmp = xstrdup(hex);
177 cgit_tree_link(sha1_to_hex(commit->tree->object.sha1), NULL, NULL,
178 cgit_query_head, tmp, NULL);
179 html("</td></tr>\n");
Lars Hjemli9c5229e2006-12-16 19:35:31 +0100180 for (p = commit->parents; p ; p = p->next) {
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200181 parent = lookup_commit_reference(p->item->object.sha1);
182 if (!parent) {
183 html("<tr><td colspan='3'>");
184 cgit_print_error("Error reading parent commit");
185 html("</td></tr>");
186 continue;
187 }
Lars Hjemli9c5229e2006-12-16 19:35:31 +0100188 html("<tr><th>parent</th>"
Lars Hjemlifaaca442007-06-17 15:44:22 +0200189 "<td colspan='2' class='sha1'>");
190 cgit_commit_link(sha1_to_hex(p->item->object.sha1), NULL, NULL,
191 cgit_query_head, sha1_to_hex(p->item->object.sha1));
Lars Hjemli4a0be582007-06-17 18:12:03 +0200192 html(" (");
193 cgit_diff_link("diff", NULL, NULL, cgit_query_head, hex,
194 sha1_to_hex(p->item->object.sha1), NULL);
195 html(")</td></tr>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100196 }
Lars Hjemliac70cb42007-02-08 14:47:56 +0100197 if (cgit_repo->snapshots) {
Michael Krelinf97c7072007-07-18 14:40:03 +0200198 html("<tr><th>download</th><td colspan='2' class='sha1'>");
Lars Hjemlieb453422007-07-23 00:11:15 +0200199 cgit_print_snapshot_links(cgit_query_repo, cgit_query_head,
200 hex, cgit_repo->snapshots);
Michael Krelinf97c7072007-07-18 14:40:03 +0200201 html("</td></tr>");
Lars Hjemliac70cb42007-02-08 14:47:56 +0100202 }
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100203 html("</table>\n");
204 html("<div class='commit-subject'>");
205 html_txt(info->subject);
206 html("</div>");
207 html("<div class='commit-msg'>");
208 html_txt(info->msg);
209 html("</div>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200210 if (!(commit->parents && commit->parents->next && commit->parents->next->next)) {
Lars Hjemlidff894d2007-05-16 01:16:56 +0200211 html("<div class='diffstat-header'>Diffstat</div>");
Lars Hjemli29154832007-11-11 13:04:28 +0100212 html("<table summary='diffstat' class='diffstat'>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200213 max_changes = 0;
214 cgit_diff_commit(commit, inspect_filepair);
215 for(i = 0; i<files; i++)
216 print_fileinfo(&items[i]);
217 html("</table>");
218 html("<div class='diffstat-summary'>");
Lars Hjemlidff894d2007-05-16 01:16:56 +0200219 htmlf("%d files changed, %d insertions, %d deletions (",
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200220 files, total_adds, total_rems);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200221 cgit_diff_link("show diff", NULL, NULL, cgit_query_head, hex,
222 NULL, NULL);
223 html(")</div>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200224 }
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100225 cgit_free_commitinfo(info);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100226}