]> gitweb.ps.run Git - ps-cgit/blob - ui-commit.c
8019e3608f0fd35911024d2b8294e61e864831a2
[ps-cgit] / 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 #include "html.h"
11 #include "ui-shared.h"
12
13 static int files, slots;
14 static int total_adds, total_rems, max_changes;
15 static int lines_added, lines_removed;
16 static char *curr_rev;
17
18 static struct fileinfo {
19         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
31 void print_fileinfo(struct fileinfo *info)
32 {
33         char *class;
34
35         switch (info->status) {
36         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:
61                 die("bug: unhandled diff status %c", info->status);
62         }
63
64         html("<tr>");
65         htmlf("<td class='mode'>");
66         if (is_null_sha1(info->new_sha1)) {
67                 cgit_print_filemode(info->old_mode);
68         } else {
69                 cgit_print_filemode(info->new_mode);
70         }
71
72         if (info->old_mode != info->new_mode &&
73             !is_null_sha1(info->old_sha1) &&
74             !is_null_sha1(info->new_sha1)) {
75                 html("<span class='modechange'>[");
76                 cgit_print_filemode(info->old_mode);
77                 html("]</span>");
78         }
79         htmlf("</td><td class='%s'>", class);
80         cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, curr_rev,
81                        NULL, info->new_path);
82         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);
86         html("</td><td class='right'>");
87         htmlf("%d", info->added + info->removed);
88         html("</td><td class='graph'>");
89         htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
90         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);
96         html("</tr></table></td></tr>\n");
97 }
98
99 void 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
109 void 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
138 void cgit_print_commit(char *hex)
139 {
140         struct commit *commit, *parent;
141         struct commitinfo *info;
142         struct commit_list *p;
143         unsigned char sha1[20];
144         char *tmp;
145         int i;
146
147         if (!hex)
148                 hex = ctx.qry.head;
149         curr_rev = hex;
150
151         if (get_sha1(hex, sha1)) {
152                 cgit_print_error(fmt("Bad object id: %s", hex));
153                 return;
154         }
155         commit = lookup_commit_reference(sha1);
156         if (!commit) {
157                 cgit_print_error(fmt("Bad commit reference: %s", hex));
158                 return;
159         }
160         info = cgit_parse_commit(commit);
161
162         html("<table summary='commit info' class='commit-info'>\n");
163         html("<tr><th>author</th><td>");
164         html_txt(info->author);
165         html(" ");
166         html_txt(info->author_email);
167         html("</td><td class='right'>");
168         cgit_print_date(info->author_date, FMT_LONGDATE);
169         html("</td></tr>\n");
170         html("<tr><th>committer</th><td>");
171         html_txt(info->committer);
172         html(" ");
173         html_txt(info->committer_email);
174         html("</td><td class='right'>");
175         cgit_print_date(info->committer_date, FMT_LONGDATE);
176         html("</td></tr>\n");
177         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,
180                        ctx.qry.head, tmp, NULL);
181         html("</td></tr>\n");
182         for (p = commit->parents; p ; p = p->next) {
183                 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                 }
190                 html("<tr><th>parent</th>"
191                      "<td colspan='2' class='sha1'>");
192                 cgit_commit_link(sha1_to_hex(p->item->object.sha1), NULL, NULL,
193                                  ctx.qry.head, sha1_to_hex(p->item->object.sha1));
194                 html(" (");
195                 cgit_diff_link("diff", NULL, NULL, ctx.qry.head, hex,
196                                sha1_to_hex(p->item->object.sha1), NULL);
197                 html(")</td></tr>");
198         }
199         if (ctx.repo->snapshots) {
200                 html("<tr><th>download</th><td colspan='2' class='sha1'>");
201                 cgit_print_snapshot_links(ctx.qry.repo, ctx.qry.head,
202                                           hex, ctx.repo->snapshots);
203                 html("</td></tr>");
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 summary='diffstat' 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, ctx.qry.head, hex,
224                                NULL, NULL);
225                 html(")</div>");
226         }
227         cgit_free_commitinfo(info);
228 }