blob: f9051c110ec7b134c5536be80ede3af75122bfb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * The commit page, which shows one commit on its own and is where the log and
 * the ref listings link. It renders the idents, the object ids, the message
 * and any note, then hands off to ui-diff.c for the diff against the first
 * parent. Free text and addresses are written through the repository's commit
 * and email filters, so a site can rewrite either on the way out.
 */

#define USE_THE_REPOSITORY_VARIABLE

#include "cgit.h"
#include "filter.h"
#include "html.h"
#include "parsing.h"
#include "shared.h"
#include "ui-commit.h"
#include "ui-diff.h"
#include "ui-log.h"
#include "ui-shared.h"

// The diff below the message is always taken against the first parent alone,
// which says little about a merge of many branches, so a commit with this
// many parents or more is shown without a diff at all.
#define OCTOPUS_PARENTS 3

static void print_ident_row(const char *role, const char *name,
			    const char *email, timestamp_t date, int tz)
{
	htmlf("<tr><th>%s</th><td>", role);
	cgit_open_filter(ctx.repo->email_filter, email, "commit");
	html_txt(name);
	if (!ctx.cfg.noplainemail) {
		html(" ");
		html_txt(email);
	}
	cgit_close_filter(ctx.repo->email_filter);
	html("</td><td class='right'>");
	html_txt(show_date(date, tz, cgit_date_mode(DATE_ISO8601)));
	html("</td></tr>\n");
}

static int print_parent_rows(struct commit *commit, const char *rev,
			     const char *prefix)
{
	struct commit_list *p;
	struct commit *parent;
	const char *parent_hex, *label;
	int parents = 0;

	for (p = commit->parents; p; p = p->next) {
		parent = lookup_commit_reference(the_repository,
						 &p->item->object.oid);
		if (!parent) {
			html("<tr><td colspan='3'>");
			cgit_print_error("Error reading parent commit");
			html("</td></tr>");
			continue;
		}
		html("<tr><th>parent</th>"
		     "<td colspan='2' class='oid'>");
		parent_hex = label = oid_to_hex(&p->item->object.oid);
		if (ctx.repo->enable_subject_links)
			label = cgit_parse_commit(parent)->subject;
		cgit_commit_link(label, NULL, NULL, ctx.qry.head, parent_hex,
				 prefix);
		html(" (");
		cgit_diff_link("diff", NULL, NULL, ctx.qry.head, rev,
			       oid_to_hex(&p->item->object.oid), prefix);
		html(")</td></tr>");
		parents++;
	}
	return parents;
}

static void print_filtered_text(const char *text)
{
	cgit_open_filter(ctx.repo->commit_filter);
	html_txt(text);
	cgit_close_filter(ctx.repo->commit_filter);
}

void cgit_print_commit(char *hex, const char *prefix)
{
	struct commit *commit;
	struct commitinfo *info;
	struct strbuf notes = STRBUF_INIT;
	struct object_id oid;
	const char *commit_hex, *first_parent;
	char *tree_rev;
	int parents;

	if (!hex)
		hex = ctx.qry.head;

	if (repo_get_oid(the_repository, hex, &oid)) {
		cgit_print_error_page(400, "Bad request",
				"Bad object id: %s", hex);
		return;
	}
	commit = lookup_commit_reference(the_repository, &oid);
	if (!commit) {
		cgit_print_error_page(404, "Not found",
				"Bad commit reference: %s", hex);
		return;
	}
	info = cgit_parse_commit(commit);

	format_display_notes(&oid, &notes, PAGE_ENCODING, 1);

	load_ref_decorations(NULL, DECORATE_FULL_REFS);

	ctx.page.title = cgit_fmtalloc("%s - %s", info->subject, ctx.page.title);
	cgit_print_layout_start();
	cgit_print_diff_ctrls();

	html("<table summary='commit info' class='commit-info'>\n");
	print_ident_row("author", info->author, info->author_email,
			info->author_date, info->author_tz);
	print_ident_row("committer", info->committer, info->committer_email,
			info->committer_date, info->committer_tz);

	html("<tr><th>commit</th><td colspan='2' class='oid'>");
	commit_hex = oid_to_hex(&commit->object.oid);
	cgit_commit_link(commit_hex, NULL, NULL, ctx.qry.head, commit_hex,
			 prefix);
	html(" (");
	cgit_patch_link("patch", NULL, NULL, NULL, commit_hex, prefix);
	html(")</td></tr>\n");

	html("<tr><th>tree</th><td colspan='2' class='oid'>");
	tree_rev = xstrdup(hex);
	cgit_tree_link(oid_to_hex(get_commit_tree_oid(commit)), NULL, NULL,
		       ctx.qry.head, tree_rev, NULL);
	if (prefix) {
		html(" /");
		cgit_tree_link(prefix, NULL, NULL, ctx.qry.head, tree_rev,
			       prefix);
	}
	free(tree_rev);
	html("</td></tr>\n");

	parents = print_parent_rows(commit, hex, prefix);

	if (ctx.repo->snapshots) {
		html("<tr><th>download</th><td colspan='2' class='oid'>");
		cgit_print_snapshot_links(ctx.repo, hex, "<br/>");
		html("</td></tr>");
	}
	html("</table>\n");

	html("<div class='commit-subject'>");
	print_filtered_text(info->subject);
	cgit_print_commit_decorations(commit);
	html("</div>");
	html("<div class='commit-msg'>");
	print_filtered_text(info->msg);
	html("</div>");
	if (notes.len != 0) {
		html("<div class='notes-header'>Notes</div>");
		html("<div class='notes'>");
		print_filtered_text(notes.buf);
		html("</div>");
		html("<div class='notes-footer'></div>");
	}

	if (parents < OCTOPUS_PARENTS) {
		if (parents)
			first_parent =
				oid_to_hex(&commit->parents->item->object.oid);
		else
			first_parent = NULL;
		cgit_print_diff(ctx.qry.oid, first_parent, prefix, 0, 0);
	}

	strbuf_release(&notes);
	cgit_free_commitinfo(info);
	cgit_print_layout_end();
}