blob: 5fde25d0da39c1a2617fb23224497e2c2a64531a (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
/*
 * The two kinds of raw text cgit reads before it can render a page, the path
 * of the incoming request and the body of a commit or a tag object. Reading
 * the request path settles which repository and which page the request works
 * on. Commit and tag objects arrive as header lines, a blank line, and then
 * the message, so the readers here walk the headers, copy out the fields the
 * pages need, and re-encode the message into the page encoding.
 */

#define USE_THE_REPOSITORY_VARIABLE

#include "cgit.h"
#include "parsing.h"
#include "shared.h"

#include <mailmap.h>

static char *substr(const char *start, const char *end)
{
	size_t len;
	char *buf;

	if (end < start)
		return xstrdup("");
	// start points into the object buffer, so strlcpy would measure the
	// whole rest of the object to copy a name off the front of it.
	len = end - start;
	buf = xmalloc(len + 1);
	memcpy(buf, start, len);
	buf[len] = '\0';
	return buf;
}

/*
 * The repository's mailmap, read once per request and applied to every ident
 * a page shows. Only the blob at HEAD is read, the way git reads a bare
 * repository, so a repository cannot point the reader at a file on the server.
 */
static struct string_list mailmap = STRING_LIST_INIT_DUP;
static int mailmap_read;

struct string_list *cgit_mailmap(void)
{
	if (!ctx.repo || !ctx.repo->enable_mailmap)
		return NULL;
	if (!mailmap_read) {
		mailmap_read = 1;
		read_mailmap_blob(the_repository, &mailmap, "HEAD:.mailmap");
	}
	return mailmap.nr ? &mailmap : NULL;
}

static void parse_user(const char *line, char **name, char **email, timestamp_t *date, int *tz)
{
	struct ident_split ident;
	struct strbuf address = STRBUF_INIT;
	struct string_list *map;
	const char *name_start, *mail_start;
	size_t name_len, mail_len;

	if (!split_ident_line(&ident, line, strchrnul(line, '\n') - line)) {
		name_start = ident.name_begin;
		name_len = ident.name_end - ident.name_begin;
		mail_start = ident.mail_begin;
		mail_len = ident.mail_end - ident.mail_begin;
		map = cgit_mailmap();
		if (map)
			map_user(map, &mail_start, &mail_len, &name_start, &name_len);
		*name = xmemdupz(name_start, name_len);

		strbuf_addch(&address, '<');
		strbuf_add(&address, mail_start, mail_len);
		strbuf_addch(&address, '>');
		*email = strbuf_detach(&address, NULL);

		if (ident.date_begin)
			*date = parse_timestamp(ident.date_begin, NULL, 10);
		if (ident.tz_begin)
			*tz = atoi(ident.tz_begin);
	}
}

static const char *next_header_line(const char *p)
{
	p = strchr(p, '\n');
	if (!p)
		return NULL;
	return p + 1;
}

static int end_of_header(const char *p)
{
	return !p || (*p == '\n');
}

/*
 * A git built without iconv still offers reencode_string, where it always
 * fails, so the field is left in whatever encoding it arrived in.
 */
static const char *reencode(char **text, const char *from, const char *to)
{
	char *converted;

	if (!*text || !from || !to)
		return *text;

	if (!strcasecmp(from, to))
		return *text;

	converted = reencode_string(*text, to, from);
	if (converted) {
		free(*text);
		*text = converted;
	}
	return *text;
}

/*
 * A repository url may itself contain slashes, so every slash-separated
 * prefix is looked up and the longest one that names a repository wins.
 */
void cgit_parse_url(const char *url)
{
	char *buf, *slash, *repo_end, *page_end;
	struct cgit_repo *repo;

	if (!url || url[0] == '\0')
		return;

	ctx.qry.page = NULL;
	ctx.repo = cgit_get_repoinfo(url);
	if (ctx.repo) {
		ctx.qry.repo = ctx.repo->url;
		return;
	}

	buf = xstrdup(url);
	repo_end = NULL;
	slash = strchr(buf, '/');
	while (slash) {
		slash[0] = '\0';
		repo = cgit_get_repoinfo(buf);
		if (repo) {
			ctx.repo = repo;
			repo_end = slash;
		}
		slash[0] = '/';
		slash = strchr(slash + 1, '/');
	}

	if (ctx.repo) {
		ctx.qry.repo = ctx.repo->url;
		page_end = strchr(repo_end + 1, '/');
		if (page_end) {
			page_end[0] = '\0';
			if (page_end[1])
				ctx.qry.path = cgit_trim_end(page_end + 1, '/');
		}
		if (repo_end[1])
			ctx.qry.page = xstrdup(repo_end + 1);
	}
	free(buf);
}

struct commitinfo *cgit_parse_commit(struct commit *commit)
{
	struct commitinfo *info;
	const char *p = repo_get_commit_buffer(the_repository, commit, NULL);
	const char *eol;

	info = xcalloc(1, sizeof(struct commitinfo));
	info->commit = commit;

	if (!p)
		return info;

	if (!skip_prefix(p, "tree ", &p))
		die("Bad commit: %s", oid_to_hex(&commit->object.oid));
	p += the_hash_algo->hexsz + 1;

	while (skip_prefix(p, "parent ", &p))
		p += the_hash_algo->hexsz + 1;

	if (skip_prefix(p, "author ", &p)) {
		parse_user(p, &info->author, &info->author_email, &info->author_date, &info->author_tz);
		p = next_header_line(p);
	}

	if (p && skip_prefix(p, "committer ", &p)) {
		parse_user(p, &info->committer, &info->committer_email, &info->committer_date, &info->committer_tz);
		p = next_header_line(p);
	}

	if (p && skip_prefix(p, "encoding ", &p)) {
		eol = strchr(p, '\n');
		if (eol) {
			info->msg_encoding = substr(p, eol);
			p = eol + 1;
		}
	}

	// Git only writes the header when the message is in something other
	// than UTF-8, so its absence means UTF-8.
	if (!info->msg_encoding)
		info->msg_encoding = xstrdup("UTF-8");

	while (!end_of_header(p))
		p = next_header_line(p);
	while (p && *p == '\n')
		p++;

	if (p) {
		eol = strchrnul(p, '\n');
		info->subject = substr(p, eol);
		while (*eol == '\n')
			eol++;
		info->msg = xstrdup(eol);
	} else {
		// An object truncated mid header leaves nothing after it,
		// and callers render subject and msg without NULL checks.
		info->subject = xstrdup("");
		info->msg = xstrdup("");
	}

	reencode(&info->author, info->msg_encoding, PAGE_ENCODING);
	reencode(&info->author_email, info->msg_encoding, PAGE_ENCODING);
	reencode(&info->committer, info->msg_encoding, PAGE_ENCODING);
	reencode(&info->committer_email, info->msg_encoding, PAGE_ENCODING);
	reencode(&info->subject, info->msg_encoding, PAGE_ENCODING);
	reencode(&info->msg, info->msg_encoding, PAGE_ENCODING);

	return info;
}

struct taginfo *cgit_parse_tag(struct tag *tag)
{
	void *data;
	enum object_type type;
	unsigned long size;
	const char *p;
	struct taginfo *info = NULL;

	data = odb_read_object(the_repository->objects, &tag->object.oid, &type, &size);
	if (!data || type != OBJ_TAG)
		goto cleanup;

	info = xcalloc(1, sizeof(struct taginfo));

	for (p = data; !end_of_header(p); p = next_header_line(p)) {
		if (skip_prefix(p, "tagger ", &p)) {
			parse_user(p, &info->tagger, &info->tagger_email, &info->tagger_date, &info->tagger_tz);
		}
	}

	while (p && *p == '\n')
		p++;

	if (p && *p)
		info->msg = xstrdup(p);

cleanup:
	free(data);
	return info;
}