blob: 8019e3608f0fd35911024d2b8294e61e864831a2 [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 Hjemlia4d1ca12008-03-24 16:50:57 +010011#include "ui-shared.h"
Lars Hjemli9a8f8862006-12-16 00:19:56 +010012
Lars Hjemli9927e632007-05-14 23:58:29 +020013static int files, slots;
14static int total_adds, total_rems, max_changes;
15static int lines_added, lines_removed;
Lars Hjemli4a0be582007-06-17 18:12:03 +020016static char *curr_rev;
Lars Hjemli6cb326c2006-12-17 23:07:28 +010017
Lars Hjemli9927e632007-05-14 23:58:29 +020018static struct fileinfo {
Lars Hjemli8a3685b2007-05-13 22:25:14 +020019 char status;
20 unsigned char old_sha1[20];
21 unsigned char new_sha1[20];
22 unsigned short old_mode;
23 unsigned short new_mode;
24 char *old_path;
25 char *new_path;
26 unsigned int added;
27 unsigned int removed;
28} *items;
29
30
31void print_fileinfo(struct fileinfo *info)
Lars Hjemli6cb326c2006-12-17 23:07:28 +010032{
Lars Hjemli6cb326c2006-12-17 23:07:28 +010033 char *class;
Lars Hjemliae4c1ee2007-05-13 11:26:23 +020034
Lars Hjemli8a3685b2007-05-13 22:25:14 +020035 switch (info->status) {
Lars Hjemli6cb326c2006-12-17 23:07:28 +010036 case DIFF_STATUS_ADDED:
37 class = "add";
38 break;
39 case DIFF_STATUS_COPIED:
40 class = "cpy";
41 break;
42 case DIFF_STATUS_DELETED:
43 class = "del";
44 break;
45 case DIFF_STATUS_MODIFIED:
46 class = "upd";
47 break;
48 case DIFF_STATUS_RENAMED:
49 class = "mov";
50 break;
51 case DIFF_STATUS_TYPE_CHANGED:
52 class = "typ";
53 break;
54 case DIFF_STATUS_UNKNOWN:
55 class = "unk";
56 break;
57 case DIFF_STATUS_UNMERGED:
58 class = "stg";
59 break;
60 default:
Lars Hjemli8a3685b2007-05-13 22:25:14 +020061 die("bug: unhandled diff status %c", info->status);
Lars Hjemli6cb326c2006-12-17 23:07:28 +010062 }
63
64 html("<tr>");
65 htmlf("<td class='mode'>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +020066 if (is_null_sha1(info->new_sha1)) {
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010067 cgit_print_filemode(info->old_mode);
Lars Hjemlifb6e5862006-12-17 23:30:55 +010068 } else {
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010069 cgit_print_filemode(info->new_mode);
Lars Hjemlifb6e5862006-12-17 23:30:55 +010070 }
71
Lars Hjemli8a3685b2007-05-13 22:25:14 +020072 if (info->old_mode != info->new_mode &&
73 !is_null_sha1(info->old_sha1) &&
74 !is_null_sha1(info->new_sha1)) {
Lars Hjemli6cb326c2006-12-17 23:07:28 +010075 html("<span class='modechange'>[");
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010076 cgit_print_filemode(info->old_mode);
Lars Hjemli6cb326c2006-12-17 23:07:28 +010077 html("]</span>");
78 }
79 htmlf("</td><td class='%s'>", class);
Lars Hjemlid14d77f2008-02-16 11:53:40 +010080 cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, curr_rev,
Lars Hjemli0ec87912007-10-01 11:54:01 +020081 NULL, info->new_path);
Lars Hjemli4a0be582007-06-17 18:12:03 +020082 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED)
83 htmlf(" (%s from %s)",
84 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed",
85 info->old_path);
Lars Hjemli8a3685b2007-05-13 22:25:14 +020086 html("</td><td class='right'>");
87 htmlf("%d", info->added + info->removed);
Lars Hjemli8a3685b2007-05-13 22:25:14 +020088 html("</td><td class='graph'>");
Lars Hjemli29154832007-11-11 13:04:28 +010089 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
Lars Hjemlie9030112007-05-15 02:13:11 +020090 htmlf("<td class='add' style='width: %.1f%%;'/>",
91 info->added * 100.0 / max_changes);
92 htmlf("<td class='rem' style='width: %.1f%%;'/>",
93 info->removed * 100.0 / max_changes);
94 htmlf("<td class='none' style='width: %.1f%%;'/>",
95 (max_changes - info->removed - info->added) * 100.0 / max_changes);
Ondrej Jirman0928d882007-05-26 01:14:25 +020096 html("</tr></table></td></tr>\n");
Lars Hjemli6cb326c2006-12-17 23:07:28 +010097}
98
Lars Hjemli8a3685b2007-05-13 22:25:14 +020099void cgit_count_diff_lines(char *line, int len)
100{
101 if (line && (len > 0)) {
102 if (line[0] == '+')
103 lines_added++;
104 else if (line[0] == '-')
105 lines_removed++;
106 }
107}
108
109void inspect_filepair(struct diff_filepair *pair)
110{
111 files++;
112 lines_added = 0;
113 lines_removed = 0;
114 cgit_diff_files(pair->one->sha1, pair->two->sha1, cgit_count_diff_lines);
115 if (files >= slots) {
116 if (slots == 0)
117 slots = 4;
118 else
119 slots = slots * 2;
120 items = xrealloc(items, slots * sizeof(struct fileinfo));
121 }
122 items[files-1].status = pair->status;
123 hashcpy(items[files-1].old_sha1, pair->one->sha1);
124 hashcpy(items[files-1].new_sha1, pair->two->sha1);
125 items[files-1].old_mode = pair->one->mode;
126 items[files-1].new_mode = pair->two->mode;
127 items[files-1].old_path = xstrdup(pair->one->path);
128 items[files-1].new_path = xstrdup(pair->two->path);
129 items[files-1].added = lines_added;
130 items[files-1].removed = lines_removed;
131 if (lines_added + lines_removed > max_changes)
132 max_changes = lines_added + lines_removed;
133 total_adds += lines_added;
134 total_rems += lines_removed;
135}
136
137
Lars Hjemli4a0be582007-06-17 18:12:03 +0200138void cgit_print_commit(char *hex)
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100139{
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200140 struct commit *commit, *parent;
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100141 struct commitinfo *info;
142 struct commit_list *p;
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100143 unsigned char sha1[20];
Lars Hjemli44947bf2007-06-17 01:23:08 +0200144 char *tmp;
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200145 int i;
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100146
Lars Hjemli42a7eb92007-06-17 14:53:02 +0200147 if (!hex)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100148 hex = ctx.qry.head;
Lars Hjemli4a0be582007-06-17 18:12:03 +0200149 curr_rev = hex;
Lars Hjemli42a7eb92007-06-17 14:53:02 +0200150
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100151 if (get_sha1(hex, sha1)) {
152 cgit_print_error(fmt("Bad object id: %s", hex));
153 return;
154 }
Lars Hjemlifa82b032006-12-16 14:46:05 +0100155 commit = lookup_commit_reference(sha1);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100156 if (!commit) {
157 cgit_print_error(fmt("Bad commit reference: %s", hex));
158 return;
159 }
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100160 info = cgit_parse_commit(commit);
161
Lars Hjemli29154832007-11-11 13:04:28 +0100162 html("<table summary='commit info' class='commit-info'>\n");
Lars Hjemli8960d262006-12-16 14:28:26 +0100163 html("<tr><th>author</th><td>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100164 html_txt(info->author);
Lars Hjemli8960d262006-12-16 14:28:26 +0100165 html(" ");
166 html_txt(info->author_email);
167 html("</td><td class='right'>");
Lars Hjemli5db39172007-05-22 23:08:46 +0200168 cgit_print_date(info->author_date, FMT_LONGDATE);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100169 html("</td></tr>\n");
170 html("<tr><th>committer</th><td>");
171 html_txt(info->committer);
Lars Hjemli8960d262006-12-16 14:28:26 +0100172 html(" ");
173 html_txt(info->committer_email);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100174 html("</td><td class='right'>");
Lars Hjemli5db39172007-05-22 23:08:46 +0200175 cgit_print_date(info->committer_date, FMT_LONGDATE);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100176 html("</td></tr>\n");
Lars Hjemli44947bf2007-06-17 01:23:08 +0200177 html("<tr><th>tree</th><td colspan='2' class='sha1'>");
178 tmp = xstrdup(hex);
179 cgit_tree_link(sha1_to_hex(commit->tree->object.sha1), NULL, NULL,
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100180 ctx.qry.head, tmp, NULL);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200181 html("</td></tr>\n");
Lars Hjemli9c5229e2006-12-16 19:35:31 +0100182 for (p = commit->parents; p ; p = p->next) {
Lars Hjemli6a8749d2007-05-13 23:13:12 +0200183 parent = lookup_commit_reference(p->item->object.sha1);
184 if (!parent) {
185 html("<tr><td colspan='3'>");
186 cgit_print_error("Error reading parent commit");
187 html("</td></tr>");
188 continue;
189 }
Lars Hjemli9c5229e2006-12-16 19:35:31 +0100190 html("<tr><th>parent</th>"
Lars Hjemlifaaca442007-06-17 15:44:22 +0200191 "<td colspan='2' class='sha1'>");
192 cgit_commit_link(sha1_to_hex(p->item->object.sha1), NULL, NULL,
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100193 ctx.qry.head, sha1_to_hex(p->item->object.sha1));
Lars Hjemli4a0be582007-06-17 18:12:03 +0200194 html(" (");
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100195 cgit_diff_link("diff", NULL, NULL, ctx.qry.head, hex,
Lars Hjemli4a0be582007-06-17 18:12:03 +0200196 sha1_to_hex(p->item->object.sha1), NULL);
197 html(")</td></tr>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100198 }
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100199 if (ctx.repo->snapshots) {
Michael Krelinf97c7072007-07-18 14:40:03 +0200200 html("<tr><th>download</th><td colspan='2' class='sha1'>");
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100201 cgit_print_snapshot_links(ctx.qry.repo, ctx.qry.head,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100202 hex, ctx.repo->snapshots);
Michael Krelinf97c7072007-07-18 14:40:03 +0200203 html("</td></tr>");
Lars Hjemliac70cb42007-02-08 14:47:56 +0100204 }
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100205 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>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200212 if (!(commit->parents && commit->parents->next && commit->parents->next->next)) {
Lars Hjemlidff894d2007-05-16 01:16:56 +0200213 html("<div class='diffstat-header'>Diffstat</div>");
Lars Hjemli29154832007-11-11 13:04:28 +0100214 html("<table summary='diffstat' class='diffstat'>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200215 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'>");
Lars Hjemlidff894d2007-05-16 01:16:56 +0200221 htmlf("%d files changed, %d insertions, %d deletions (",
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200222 files, total_adds, total_rems);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100223 cgit_diff_link("show diff", NULL, NULL, ctx.qry.head, hex,
Lars Hjemli4a0be582007-06-17 18:12:03 +0200224 NULL, NULL);
225 html(")</div>");
Lars Hjemli8a3685b2007-05-13 22:25:14 +0200226 }
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100227 cgit_free_commitinfo(info);
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100228}