blob: b67f5db013154ba9d1494582db23905d5b4a2656 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * 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"

#include <list.h>
#include <trailer.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.enable_plain_email) {
		html(" ");
		html_txt(email);
	}
	cgit_close_filter(ctx.repo->email_filter);
	html("</td><td class='right'><time datetime='");
	html_attr(show_date(date, tz, cgit_date_mode(DATE_ISO8601_STRICT)));
	html("'>");
	html_txt(show_date(date, tz, cgit_date_mode(DATE_ISO8601)));
	html("</time></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>\n");
			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>\n");
		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);
}

/*
 * A value shaped like an ident is written the way the author row is, through
 * the email filter. Anything else goes through the trailer filter when one is
 * set, and otherwise through the commit filter, so the links a site already
 * draws in message text keep appearing on trailer lines.
 */
static void print_trailer_value(const char *key, const char *value)
{
	struct ident_split ident;
	struct strbuf name = STRBUF_INIT, email = STRBUF_INIT;

	if (!split_ident_line(&ident, value, strlen(value)) &&
		memchr(ident.mail_begin, '@', ident.mail_end - ident.mail_begin)) {
		strbuf_add(&name, ident.name_begin, ident.name_end - ident.name_begin);
		strbuf_addch(&email, '<');
		strbuf_add(&email, ident.mail_begin, ident.mail_end - ident.mail_begin);
		strbuf_addch(&email, '>');
		cgit_open_filter(ctx.repo->email_filter, email.buf, "commit");
		html_txt(name.buf);
		if (ctx.cfg.enable_plain_email) {
			html(" ");
			html_txt(email.buf);
		}
		cgit_close_filter(ctx.repo->email_filter);
		strbuf_release(&name);
		strbuf_release(&email);
	} else if (ctx.repo->trailer_filter) {
		cgit_open_filter(ctx.repo->trailer_filter, key, "commit");
		html_txt(value);
		cgit_close_filter(ctx.repo->trailer_filter);
	} else
		print_filtered_text(value);
}

/*
 * One row per trailer, except that a run of trailers sharing a key becomes a
 * single row listing every value. A line git kept in the block without being
 * able to split it, such as a cherry-pick note, gets a row of its own.
 */
static void print_trailers(const char *msg)
{
	struct trailer_iterator iter;
	struct strbuf key = STRBUF_INIT;
	int open = 0;

	html("<table class='commit-trailers'>\n");
	trailer_iterator_init(&iter, msg);
	while (trailer_iterator_advance(&iter)) {
		if (!iter.val.len) {
			if (open)
				html("</td></tr>\n");
			open = 0;
			html("<tr><td colspan='2'>");
			print_filtered_text(iter.key.buf);
			html("</td></tr>\n");
			continue;
		}
		if (open && !strcasecmp(key.buf, iter.key.buf))
			html("<br>");
		else {
			if (open)
				html("</td></tr>\n");
			strbuf_reset(&key);
			strbuf_addbuf(&key, &iter.key);
			html("<tr><th>");
			html_txt(key.buf);
			html("</th><td>");
			open = 1;
		}
		print_trailer_value(key.buf, iter.val.buf);
	}
	if (open)
		html("</td></tr>\n");
	html("</table>\n");
	trailer_iterator_release(&iter);
	strbuf_release(&key);
}

/*
 * Git finds the trailer block by looking at the whole message, title included,
 * so the subject goes back in front of the body before asking, and the offsets
 * that come back are shifted by that prefix. The blank line that separated the
 * body from its block is dropped along with the block.
 */
static void print_message(struct commitinfo *info)
{
	struct strbuf full = STRBUF_INIT;
	struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
	struct trailer_block *block;
	LIST_HEAD(items);
	size_t prefix, start, end;
	char *body = NULL;

	if (ctx.repo->enable_trailers) {
		strbuf_addf(&full, "%s\n\n%s", info->subject, info->msg);
		prefix = full.len - strlen(info->msg);
		opts.no_divider = 1;
		block = parse_trailers(&opts, full.buf, &items);
		start = trailer_block_start(block);
		end = trailer_block_end(block);
		free_trailers(&items);
		trailer_block_release(block);
		if (start >= prefix && start < end) {
			size_t len = start - prefix;

			while (len > 0 && info->msg[len - 1] == '\n')
				len--;
			body = xmemdupz(info->msg, len);
		}
	}

	html("<div class='commit-msg'>");
	print_filtered_text(body ? body : info->msg);
	html("</div>\n");
	if (body)
		print_trailers(full.buf);

	free(body);
	strbuf_release(&full);
}

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 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");

	// The parent rows follow the commit row so the commit hashes sit
	// together, leaving the tree beside the download links.
	parents = print_parent_rows(commit, hex, prefix);

	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");

	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>\n");
	}
	html("</table>\n");

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

	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();
}