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