]> gitweb.ps.run Git - ps-cgit/blob - ui-commit.c
ui-refs.c: Refactor print_tag()
[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 "ui-commit.h"
11 #include "html.h"
12 #include "ui-shared.h"
13 #include "ui-diff.h"
14 #include "ui-log.h"
15
16 void cgit_print_commit(char *hex, const char *prefix)
17 {
18         struct commit *commit, *parent;
19         struct commitinfo *info, *parent_info;
20         struct commit_list *p;
21         struct strbuf notes = STRBUF_INIT;
22         unsigned char sha1[20];
23         char *tmp, *tmp2;
24         int parents = 0;
25
26         if (!hex)
27                 hex = ctx.qry.head;
28
29         if (get_sha1(hex, sha1)) {
30                 cgit_print_error(fmt("Bad object id: %s", hex));
31                 return;
32         }
33         commit = lookup_commit_reference(sha1);
34         if (!commit) {
35                 cgit_print_error(fmt("Bad commit reference: %s", hex));
36                 return;
37         }
38         info = cgit_parse_commit(commit);
39
40         init_display_notes(NULL);
41         format_display_notes(sha1, &notes, PAGE_ENCODING, 0);
42
43         load_ref_decorations(DECORATE_FULL_REFS);
44
45         cgit_print_diff_ctrls();
46         html("<table summary='commit info' class='commit-info'>\n");
47         html("<tr><th>author</th><td>");
48         html_txt(info->author);
49         if (!ctx.cfg.noplainemail) {
50                 html(" ");
51                 html_txt(info->author_email);
52         }
53         html("</td><td class='right'>");
54         cgit_print_date(info->author_date, FMT_LONGDATE, ctx.cfg.local_time);
55         html("</td></tr>\n");
56         html("<tr><th>committer</th><td>");
57         html_txt(info->committer);
58         if (!ctx.cfg.noplainemail) {
59                 html(" ");
60                 html_txt(info->committer_email);
61         }
62         html("</td><td class='right'>");
63         cgit_print_date(info->committer_date, FMT_LONGDATE, ctx.cfg.local_time);
64         html("</td></tr>\n");
65         html("<tr><th>commit</th><td colspan='2' class='sha1'>");
66         tmp = sha1_to_hex(commit->object.sha1);
67         cgit_commit_link(tmp, NULL, NULL, ctx.qry.head, tmp, prefix, 0);
68         html(" (");
69         cgit_patch_link("patch", NULL, NULL, NULL, tmp, prefix);
70         html(")</td></tr>\n");
71         html("<tr><th>tree</th><td colspan='2' class='sha1'>");
72         tmp = xstrdup(hex);
73         cgit_tree_link(sha1_to_hex(commit->tree->object.sha1), NULL, NULL,
74                        ctx.qry.head, tmp, NULL);
75         if (prefix) {
76                 html(" /");
77                 cgit_tree_link(prefix, NULL, NULL, ctx.qry.head, tmp, prefix);
78         }
79         free(tmp);
80         html("</td></tr>\n");
81         for (p = commit->parents; p; p = p->next) {
82                 parent = lookup_commit_reference(p->item->object.sha1);
83                 if (!parent) {
84                         html("<tr><td colspan='3'>");
85                         cgit_print_error("Error reading parent commit");
86                         html("</td></tr>");
87                         continue;
88                 }
89                 html("<tr><th>parent</th>"
90                      "<td colspan='2' class='sha1'>");
91                 tmp = tmp2 = sha1_to_hex(p->item->object.sha1);
92                 if (ctx.repo->enable_subject_links) {
93                         parent_info = cgit_parse_commit(parent);
94                         tmp2 = parent_info->subject;
95                 }
96                 cgit_commit_link(tmp2, NULL, NULL, ctx.qry.head, tmp, prefix, 0);
97                 html(" (");
98                 cgit_diff_link("diff", NULL, NULL, ctx.qry.head, hex,
99                                sha1_to_hex(p->item->object.sha1), prefix, 0);
100                 html(")</td></tr>");
101                 parents++;
102         }
103         if (ctx.repo->snapshots) {
104                 html("<tr><th>download</th><td colspan='2' class='sha1'>");
105                 cgit_print_snapshot_links(ctx.qry.repo, ctx.qry.head,
106                                           hex, ctx.repo->snapshots);
107                 html("</td></tr>");
108         }
109         html("</table>\n");
110         html("<div class='commit-subject'>");
111         if (ctx.repo->commit_filter)
112                 cgit_open_filter(ctx.repo->commit_filter);
113         html_txt(info->subject);
114         if (ctx.repo->commit_filter)
115                 cgit_close_filter(ctx.repo->commit_filter);
116         show_commit_decorations(commit);
117         html("</div>");
118         html("<div class='commit-msg'>");
119         if (ctx.repo->commit_filter)
120                 cgit_open_filter(ctx.repo->commit_filter);
121         html_txt(info->msg);
122         if (ctx.repo->commit_filter)
123                 cgit_close_filter(ctx.repo->commit_filter);
124         html("</div>");
125         if (notes.len != 0) {
126                 html("<div class='notes-header'>Notes</div>");
127                 html("<div class='notes'>");
128                 if (ctx.repo->commit_filter)
129                         cgit_open_filter(ctx.repo->commit_filter);
130                 html_txt(notes.buf);
131                 if (ctx.repo->commit_filter)
132                         cgit_close_filter(ctx.repo->commit_filter);
133                 html("</div>");
134                 html("<div class='notes-footer'></div>");
135         }
136         if (parents < 3) {
137                 if (parents)
138                         tmp = sha1_to_hex(commit->parents->item->object.sha1);
139                 else
140                         tmp = NULL;
141                 cgit_print_diff(ctx.qry.sha1, tmp, prefix, 0);
142         }
143         strbuf_release(&notes);
144         cgit_free_commitinfo(info);
145 }