blob: c81d8225c493793ebef5acb68478872fc85829e1 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * The repository index, which is the page a cgit site opens on, and the site
 * readme the about page falls back to when a request names no repository.
 * Every repository cgitrc registered is a candidate row, cut down to those
 * matching the search terms and the url prefix the request carried, ordered
 * either by the column the reader asked for or by section, and split into
 * pages of max-repo-count rows.
 */

#include "cgit.h"
#include "filter.h"
#include "html.h"
#include "shared.h"
#include "ui-repolist.h"
#include "ui-shared.h"

// A section heading spans the whole table, so this has to stay in step with
// the headings print_header_row emits. Name, description and idle are always
// there, owner and links are added when cgitrc asks for them.
#define BASE_COLUMNS 3

struct sort_column {
	const char *name;
	int (*cmp)(const void *a, const void *b);
};

static time_t read_agefile(const char *path)
{
	time_t result;
	size_t size;
	char *buf = NULL;
	struct strbuf date = STRBUF_INIT;

	if (cgit_read_first_line(path, &buf, &size)) {
		free(buf);
		return 0;
	}

	// parse_date writes the timestamp followed by its timezone offset, so
	// only the seconds at the front are wanted here.
	if (parse_date(buf, &date) == 0)
		result = strtoul(date.buf, NULL, 10);
	else
		result = 0;
	free(buf);
	strbuf_release(&date);
	return result;
}

/*
 * The returned name can point into head, which the caller has to keep alive
 * for as long as it uses it.
 */
static const char *tip_branch(const struct cgit_repo *repo, struct strbuf *head)
{
	struct strbuf path = STRBUF_INIT;
	const char *branch = repo->defbranch;

	if (branch)
		return branch;

	strbuf_addf(&path, "%s/HEAD", repo->path);
	if (strbuf_read_file(head, path.buf, 0) > 0) {
		strbuf_rtrim(head);
		if (!skip_prefix(head->buf, "ref: refs/heads/", &branch))
			branch = NULL;
		// HEAD belongs to the repository, so a crafted target such as
		// "ref: refs/heads/../../.." must not let the caller's stat
		// walk outside it. Git forbids ".." in a ref name anyway.
		if (branch && strstr(branch, ".."))
			branch = NULL;
	}
	strbuf_release(&path);

	return branch ? branch : "master";
}

static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
{
	struct strbuf path = STRBUF_INIT;
	struct strbuf head = STRBUF_INIT;
	struct stat st;
	const char *branch;
	// The comparators below are handed const repositories, but the answer
	// is kept in the repository itself so one page stats it only once.
	struct cgit_repo *writable = (struct cgit_repo *)repo;

	if (repo->mtime != -1) {
		*mtime = repo->mtime;
		return 1;
	}
	strbuf_addf(&path, "%s/%s", repo->path, ctx.cfg.agefile);
	if (stat(path.buf, &st) == 0) {
		*mtime = read_agefile(path.buf);
		if (*mtime) {
			writable->mtime = *mtime;
			goto end;
		}
	}

	branch = tip_branch(repo, &head);
	strbuf_reset(&path);
	strbuf_addf(&path, "%s/refs/heads/%s", repo->path, branch);
	if (stat(path.buf, &st) == 0) {
		*mtime = st.st_mtime;
		writable->mtime = *mtime;
		goto end;
	}

	strbuf_reset(&path);
	strbuf_addf(&path, "%s/packed-refs", repo->path);
	if (stat(path.buf, &st) == 0) {
		*mtime = st.st_mtime;
		writable->mtime = *mtime;
		goto end;
	}

	*mtime = 0;
	writable->mtime = *mtime;
end:
	strbuf_release(&path);
	strbuf_release(&head);
	return (writable->mtime != 0);
}

static void print_modtime(struct cgit_repo *repo)
{
	time_t mtime;

	if (get_repo_modtime(repo, &mtime))
		cgit_print_age(mtime, 0, -1);
}

static int matches_search(struct cgit_repo *repo)
{
	if (!ctx.qry.search)
		return 1;
	if (repo->url && strcasestr(repo->url, ctx.qry.search))
		return 1;
	if (repo->name && strcasestr(repo->name, ctx.qry.search))
		return 1;
	if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
		return 1;
	if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
		return 1;
	return 0;
}

static int matches_url(struct cgit_repo *repo)
{
	if (!ctx.qry.url)
		return 1;
	if (repo->url && starts_with(repo->url, ctx.qry.url))
		return 1;
	return 0;
}

static int is_visible(struct cgit_repo *repo)
{
	if (repo->hide || repo->ignore)
		return 0;
	if (!(matches_search(repo) && matches_url(repo)))
		return 0;
	return 1;
}

static int any_repos_visible(void)
{
	int i;

	for (i = 0; i < cgit_repolist.count; i++) {
		if (is_visible(&cgit_repolist.repos[i]))
			return 1;
	}
	return 0;
}

// currenturl is passed in because it is the same for every heading and every
// row, and working it out here would mean an allocation and a free per cell.
static void print_column_header(const char *title, const char *column,
				const char *currenturl)
{
	htmlf("<th class='left col-%s'><a href='", column);
	html_attr(currenturl);
	htmlf("?s=%s", column);
	if (ctx.qry.search) {
		html("&amp;q=");
		html_url_arg(ctx.qry.search);
	}
	htmlf("'>%s</a></th>", title);
}

static void print_header_row(const char *currenturl)
{
	html("<tr class='nohover'>");
	print_column_header("Name", "name", currenturl);
	print_column_header("Description", "desc", currenturl);
	if (ctx.cfg.enable_index_owner)
		print_column_header("Owner", "owner", currenturl);
	print_column_header("Idle", "idle", currenturl);
	if (ctx.cfg.enable_index_links)
		html("<th class='left col-links'>Links</th>");
	html("</tr>\n");
}

static int section_changed(const char *section, const char *last)
{
	if (!section && !last)
		return 0;
	if (!section || !last)
		return 1;
	return strcmp(section, last) != 0;
}

static void print_section_row(const char *section, int columns)
{
	htmlf("<tr class='nohover-highlight'><td colspan='%d' class='reposection'>",
	      columns);
	html_txt(section);
	html("</td></tr>\n");
}

static void print_repo_row(const char *currenturl, int sublevel)
{
	char *repourl;

	htmlf("<tr><td class='col-name %s'>",
	      sublevel ? "sublevel-repo" : "toplevel-repo");
	cgit_summary_link(ctx.repo->name, NULL, NULL, NULL);
	html("</td><td class='col-desc'>");
	repourl = cgit_repourl(ctx.repo->url);
	html_link_open(repourl, NULL, NULL);
	free(repourl);
	if (html_ntxt(ctx.repo->desc, ctx.cfg.max_repodesc_len) < 0)
		html("...");
	html_link_close();
	html("</td>");
	if (ctx.cfg.enable_index_owner) {
		html("<td class='col-owner'>");
		// A repo without an owner gets an empty cell, since a link
		// with no text cannot be clicked or read out.
		if (ctx.repo->owner && *ctx.repo->owner) {
			html("<a href='");
			html_attr(currenturl);
			html("?q=");
			html_url_arg(ctx.repo->owner);
			html("'>");
			html_txt(ctx.repo->owner);
			html("</a>");
		}
		html("</td>");
	}
	html("<td class='col-idle'>");
	print_modtime(ctx.repo);
	html("</td>");
	if (ctx.cfg.enable_index_links) {
		html("<td class='col-links'>");
		cgit_summary_link("summary", NULL, "button", NULL);
		cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
			      0, NULL, NULL, ctx.qry.showmsg, 0);
		cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
		html("</td>");
	}
	html("</tr>\n");
}

static void print_pager(int total, int pagelen, char *search, char *sort)
{
	int i, ofs;
	char *class = NULL;

	html("<ul class='pager'>\n");
	for (i = 0, ofs = 0; ofs < total; i++, ofs = i * pagelen) {
		class = (ctx.qry.ofs == ofs) ? "current" : NULL;
		html("<li>");
		cgit_index_link(cgit_fmt("[%d]", i + 1), cgit_fmt("Page %d", i + 1),
				class, search, sort, ofs, 0);
		html("</li>");
	}
	html("</ul>\n");
}

static int cmp_str(const char *s1, const char *s2)
{
	if (s1 && s2) {
		if (ctx.cfg.case_sensitive_sort)
			return strcmp(s1, s2);
		else
			return strcasecmp(s1, s2);
	}
	if (s1 && !s2)
		return -1;
	if (s2 && !s1)
		return 1;
	return 0;
}

static int cmp_name(const void *a, const void *b)
{
	const struct cgit_repo *r1 = a;
	const struct cgit_repo *r2 = b;

	return cmp_str(r1->name, r2->name);
}

static int cmp_desc(const void *a, const void *b)
{
	const struct cgit_repo *r1 = a;
	const struct cgit_repo *r2 = b;

	return cmp_str(r1->desc, r2->desc);
}

static int cmp_owner(const void *a, const void *b)
{
	const struct cgit_repo *r1 = a;
	const struct cgit_repo *r2 = b;

	return cmp_str(r1->owner, r2->owner);
}

static int cmp_idle(const void *a, const void *b)
{
	const struct cgit_repo *r1 = a;
	const struct cgit_repo *r2 = b;
	time_t t1, t2;

	t1 = t2 = 0;
	get_repo_modtime(r1, &t1);
	get_repo_modtime(r2, &t2);
	// Only the sign is returned, because a 64-bit difference truncated
	// into an int could come back with the wrong sign and leave the
	// ordering inconsistent.
	if (t2 > t1)
		return 1;
	if (t2 < t1)
		return -1;
	return 0;
}

static int cmp_section(const void *a, const void *b)
{
	const struct cgit_repo *r1 = a;
	const struct cgit_repo *r2 = b;
	int result;

	result = cmp_str(r1->section, r2->section);
	if (!result) {
		if (!strcmp(ctx.cfg.repository_sort, "age"))
			result = cmp_idle(r1, r2);
		if (!result)
			result = cmp_str(r1->name, r2->name);
	}
	return result;
}

/*
 * get_repo_modtime caches into the repository it is handed, but qsort moves
 * those structs around as it works, so a comparator left to fill the cache
 * loses most of what it stored and stats the same repository over and over.
 */
static void resolve_modtimes(void)
{
	time_t t;
	int i;

	for (i = 0; i < cgit_repolist.count; i++)
		get_repo_modtime(&cgit_repolist.repos[i], &t);
}

static const struct sort_column sort_columns[] = {
	{"section", cmp_section},
	{"name", cmp_name},
	{"desc", cmp_desc},
	{"owner", cmp_owner},
	{"idle", cmp_idle},
	{NULL, NULL}
};

static int sort_repolist(char *field)
{
	const struct sort_column *column;

	for (column = &sort_columns[0]; column->name; column++) {
		if (strcmp(field, column->name))
			continue;
		if (column->cmp == cmp_idle || column->cmp == cmp_section)
			resolve_modtimes();
		qsort(cgit_repolist.repos, cgit_repolist.count,
			sizeof(struct cgit_repo), column->cmp);
		return 1;
	}
	return 0;
}

void cgit_print_repolist(void)
{
	char *last_section = NULL;
	char *currenturl;
	int columns = BASE_COLUMNS;
	int column_sorted = 0;
	int hits = 0;
	int shown = 0;
	int i;

	if (!any_repos_visible()) {
		cgit_print_error_page(404, "Not found", "No repositories found");
		return;
	}

	if (ctx.cfg.enable_index_links)
		++columns;
	if (ctx.cfg.enable_index_owner)
		++columns;

	ctx.page.title = ctx.cfg.root_title;
	cgit_print_http_headers();
	cgit_print_docstart();
	cgit_print_pageheader();

	if (ctx.qry.sort)
		column_sorted = sort_repolist(ctx.qry.sort);
	else if (ctx.cfg.section_sort)
		sort_repolist("section");

	currenturl = cgit_currenturl();
	html("<table class='list repolist'>\n");
	for (i = 0; i < cgit_repolist.count; i++) {
		char *section;

		ctx.repo = &cgit_repolist.repos[i];
		if (!is_visible(ctx.repo))
			continue;
		hits++;
		// Rows outside the page are stepped over rather than broken
		// out of, because the pager below is sized from the total.
		if (hits <= ctx.qry.ofs)
			continue;
		// Written as a subtraction because max-repo-count of zero
		// means unlimited and is held as INT_MAX, which any positive
		// offset would overflow if it were added to instead.
		if (hits - ctx.qry.ofs > ctx.cfg.max_repo_count)
			continue;
		if (!shown++)
			print_header_row(currenturl);
		section = ctx.repo->section;
		if (section && !strcmp(section, ""))
			section = NULL;
		if (!column_sorted && section_changed(section, last_section)) {
			print_section_row(section, columns);
			last_section = section;
		}
		print_repo_row(currenturl, !column_sorted && section);
	}
	html("</table>\n");
	if (hits > ctx.cfg.max_repo_count)
		print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
	free(currenturl);
	cgit_print_docend();
}

void cgit_print_site_readme(void)
{
	cgit_print_layout_start();
	if (!ctx.cfg.root_readme)
		goto done;
	cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
	html_include(ctx.cfg.root_readme);
	cgit_close_filter(ctx.cfg.about_filter);
done:
	cgit_print_layout_end();
}