blob: ed258243f17c1254e3fcc1b853a5930d307055d8 [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"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010010#include "html.h"
Lars Hjemli9a8f8862006-12-16 00:19:56 +010011
Lars Hjemli9927e632007-05-14 23:58:29 +020012static int files, slots;
13static int total_adds, total_rems, max_changes;
14static int lines_added, lines_removed;
Lars Hjemli4a0be582007-06-17 18:12:03 +020015static char *curr_rev;
Lars Hjemli6cb326c2006-12-17 23:07:28 +010016
Lars Hjemli9927e632007-05-14 23:58:29 +020017static struct fileinfo {
Lars Hjemli8a3685b2007-05-13 22:25:14 +020018 char status;
19 unsigned char old_sha1[20];
20 unsigned char new_sha1[20];
21 unsigned short old_mode;
22 unsigned short new_mode;
23 char *old_path;
24 char *new_path;
25 unsigned int added;
26 unsigned int removed;
27} *items;
28
29
30void print_fileinfo(struct fileinfo *info)
Lars Hjemli6cb326c2006-12-17 23:07:28 +010031{
Lars Hjemli6cb326c2006-12-17 23:07:28 +010032 char *class;
Lars Hjemliae4c1ee2007-05-13 11:26:23 +020033
Lars Hjemli8a3685b2007-05-13 22:25:14 +020034 switch (info->status) {
Lars Hjemli6cb326c2006-12-17 23:07:28 +010035 case DIFF_STATUS_ADDED:
36 class = "add";
37 break;
38 case DIFF_STATUS_COPIED:
39 class = "cpy";
40 break;
41 case DIFF_STATUS_DELETED:
42 class = "del";
43 break;
44 case DIFF_STATUS_MODIFIED:
45 class = "upd";
46 break;
47 case DIFF_STATUS_RENAMED:
48 class = "mov";
49 break;
50 case DIFF_STATUS_TYPE_CHANGED:
51 class = "typ";
52 break;
53 case DIFF_STATUS_UNKNOWN:
54 class = "unk";
55 break;
56 case DIFF_STATUS_UNMERGED:
57 class = "stg";
58 break;
59 default:
Lars Hjemli8a3685b2007-05-13 22:25:14 +020060 die("bug: unhandled diff status %c", info->status);
Lars Hjemli6cb326c2006-12-17 23:07:28 +010061 }
62
63 html("<tr>");
64 htmlf("<td class='mode'>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +020065 if (is_null_sha1(info->new_sha1)) {
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010066 cgit_print_filemode(info->old_mode);
Lars Hjemlifb6e5862006-12-17 23:30:55 +010067 } else {
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010068 cgit_print_filemode(info->new_mode);
Lars Hjemlifb6e5862006-12-17 23:30:55 +010069 }
70
Lars Hjemli8a3685b2007-05-13 22:25:14 +020071 if (info->old_mode != info->new_mode &&
72 !is_null_sha1(info->old_sha1) &&
73 !is_null_sha1(info->new_sha1)) {
Lars Hjemli6cb326c2006-12-17 23:07:28 +010074 html("<span class='modechange'>[");
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010075 cgit_print_filemode(info->old_mode);
Lars Hjemli6cb326c2006-12-17 23:07:28 +010076 html("]</span>");
77 }
78 htmlf("</td><td class='%s'>", class);
Lars Hjemlid14d77f2008-02-16 11:53:40 +010079 cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, curr_rev,
Lars Hjemli0ec87912007-10-01 11:54:01 +020080 NULL, info->new_path);
Lars Hjemli4a0be582007-06-17 18:12:03 +020081 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED)
82 htmlf(" (%s from %s)",
83 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed",
84 info->old_path);
Lars Hjemli8a3685b2007-05-13 22:25:14 +020085 html("</td><td class='right'>");
86 htmlf("%d", info->added + info->removed);
Lars Hjemli8a3685b2007-05-13 22:25:14 +020087 html("</td><td class='graph'>");
Lars Hjemli29154832007-11-11 13:04:28 +010088 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
Lars Hjemlie9030112007-05-15 02:13:11 +020089 htmlf("<td class='add' style='width: %.1f%%;'/>",
90 info->added * 100.0 / max_changes);
91 htmlf("<td class='rem' style='width: %.1f%%;'/>",
92 info->removed * 100.0 / max_changes);
93 htmlf("<td class='none' style='width: %.1f%%;'/>",
94 (max_changes - info->removed - info->added) * 100.0 / max_changes);
Ondrej Jirman0928d882007-05-26 01:14:25 +020095 html("</tr></table></td></tr>\n");
Lars Hjemli6cb326c2006-12-17 23:07:28 +010096}
97
Lars Hjemli8a3685b2007-05-13 22:25:14 +020098void cgit_count_diff_lines(char *line, int len)
99{
100 if (line && (len > 0)) {
101 if (line[0] == '+')
102 lines_added++;
103 else if (line[0] == '-')
104 lines_removed++;
105 }
106}
107
108void inspect_filepair(struct diff_filepair *pair)
109{
110 files++;
111 lines_added = 0;
112 lines_removed = 0;
113 cgit_diff_files(pair->one->sha1, pair->two->sha1, cgit_count_diff_lines);
114 if (files >= slots) {
115 if (slots == 0)
116 slots = 4;
117 else
118 slots = slots * 2;
119 items = xrealloc(items, slots * sizeof(struct fileinfo));
120 }
121 items[files-1].status = pair->status;
122 hashcpy(items[files-1].old_sha1, pair->one->sha1);
123 hashcpy(items[files-1].new_sha1, pair->two->sha1);
124 items[files-1].old_mode = pair->one->mode;
125 items[files-1].new_mode = pair->two->mode;
126 items[files-1].old_path = xstrdup(pair->one->path);
127 items[files-1].new_path = xstrdup(pair->two->path);
128 items[files-1].added = lines_added;
129 items[files-1].removed = lines_removed;
130 if (lines_added + lines_removed > max_changes)
131 max_changes = lines_added + lines_removed;
132 total_adds += lines_added;
133 total_rems += lines_removed;
134}
135
136
Lars Hjemli4a0be582007-06-17 18:12:03 +0200137void cgit_print_commit(char *hex)
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100138{
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200139 struct commit *commit, *parent;
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100140 struct commitinfo *info;
141 struct commit_list *p;
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100142 unsigned char sha1[20];
Lars Hjemli44947bf2007-06-17 01:23:08 +0200143 char *tmp;
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200144 int i;
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100145
Lars Hjemli42a7eb92007-06-17 14:53:02 +0200146 if (!hex)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100147 hex = ctx.qry.head;
Lars Hjemli4a0be582007-06-17 18:12:03 +0200148 curr_rev = hex;
Lars Hjemli42a7eb92007-06-17 14:53:02 +0200149
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100150 if (get_sha1(hex, sha1)) {
151 cgit_print_error(fmt("Bad object id: %s", hex));
152 return;
153 }
Lars Hjemlifa82b032006-12-16 14:46:05 +0100154 commit = lookup_commit_reference(sha1);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100155 if (!commit) {
156 cgit_print_error(fmt("Bad commit reference: %s", hex));
157 return;
158 }
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100159 info = cgit_parse_commit(commit);
160
Lars Hjemli29154832007-11-11 13:04:28 +0100161 html("<table summary='commit info' class='commit-info'>\n");
Lars Hjemli8960d262006-12-16 14:28:26 +0100162 html("<tr><th>author</th><td>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100163 html_txt(info->author);
Lars Hjemli8960d262006-12-16 14:28:26 +0100164 html(" ");
165 html_txt(info->author_email);
166 html("</td><td class='right'>");
Lars Hjemli5db39172007-05-22 23:08:46 +0200167 cgit_print_date(info->author_date, FMT_LONGDATE);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100168 html("</td></tr>\n");
169 html("<tr><th>committer</th><td>");
170 html_txt(info->committer);
Lars Hjemli8960d262006-12-16 14:28:26 +0100171 html(" ");
172 html_txt(info->committer_email);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100173 html("</td><td class='right'>");
Lars Hjemli5db39172007-05-22 23:08:46 +0200174 cgit_print_date(info->committer_date, FMT_LONGDATE);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100175 html("</td></tr>\n");
Lars Hjemli44947bf2007-06-17 01:23:08 +0200176 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,
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100179 ctx.qry.head, tmp, NULL);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200180 html("</td></tr>\n");
Lars Hjemli9c5229e2006-12-16 19:35:31 +0100181 for (p = commit->parents; p ; p = p->next) {
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200182 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 }
Lars Hjemli9c5229e2006-12-16 19:35:31 +0100189 html("<tr><th>parent</th>"
Lars Hjemlifaaca442007-06-17 15:44:22 +0200190 "<td colspan='2' class='sha1'>");
191 cgit_commit_link(sha1_to_hex(p->item->object.sha1), NULL, NULL,
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100192 ctx.qry.head, sha1_to_hex(p->item->object.sha1));
Lars Hjemli4a0be582007-06-17 18:12:03 +0200193 html(" (");
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100194 cgit_diff_link("diff", NULL, NULL, ctx.qry.head, hex,
Lars Hjemli4a0be582007-06-17 18:12:03 +0200195 sha1_to_hex(p->item->object.sha1), NULL);
196 html(")</td></tr>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100197 }
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100198 if (ctx.repo->snapshots) {
Michael Krelinf97c7072007-07-18 14:40:03 +0200199 html("<tr><th>download</th><td colspan='2' class='sha1'>");
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100200 cgit_print_snapshot_links(ctx.qry.repo, ctx.qry.head,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100201 hex, ctx.repo->snapshots);
Michael Krelinf97c7072007-07-18 14:40:03 +0200202 html("</td></tr>");
Lars Hjemliac70cb42007-02-08 14:47:56 +0100203 }
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100204 html("</table>\n");
205 html("<div class='commit-subject'>");
206 html_txt(info->subject);
207 html("</div>");
208 html("<div class='commit-msg'>");
209 html_txt(info->msg);
210 html("</div>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200211 if (!(commit->parents && commit->parents->next && commit->parents->next->next)) {
Lars Hjemlidff894d2007-05-16 01:16:56 +0200212 html("<div class='diffstat-header'>Diffstat</div>");
Lars Hjemli29154832007-11-11 13:04:28 +0100213 html("<table summary='diffstat' class='diffstat'>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200214 max_changes = 0;
215 cgit_diff_commit(commit, inspect_filepair);
216 for(i = 0; i<files; i++)
217 print_fileinfo(&items[i]);
218 html("</table>");
219 html("<div class='diffstat-summary'>");
Lars Hjemlidff894d2007-05-16 01:16:56 +0200220 htmlf("%d files changed, %d insertions, %d deletions (",
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200221 files, total_adds, total_rems);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100222 cgit_diff_link("show diff", NULL, NULL, ctx.qry.head, hex,
Lars Hjemli4a0be582007-06-17 18:12:03 +0200223 NULL, NULL);
224 html(")</div>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200225 }
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100226 cgit_free_commitinfo(info);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100227}