blob: 97ffc78fedd7a8424ed8fc2900bbff362be318bd (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
/*
 * One thunk per page, each pulling the parts of the request its renderer
 * needs out of the global ctx, plus the table that maps a page name onto its
 * thunk. Keeping the thunks here lets every renderer take ordinary arguments
 * instead of reaching into the request state itself.
 */

#include "cache.h"
#include "cgit.h"
#include "cmd.h"
#include "html.h"
#include "ui-atom.h"
#include "ui-blame.h"
#include "ui-blob.h"
#include "ui-clone.h"
#include "ui-commit.h"
#include "ui-diff.h"
#include "ui-log.h"
#include "ui-patch.h"
#include "ui-plain.h"
#include "ui-refs.h"
#include "ui-repolist.h"
#include "ui-shared.h"
#include "ui-snapshot.h"
#include "ui-stats.h"
#include "ui-summary.h"
#include "ui-tag.h"
#include "ui-tree.h"

static void head_fn(void)
{
	cgit_clone_head();
}

static void about_fn(void)
{
	char *currenturl, *redirect;
	size_t path_info_len;

	if (!ctx.repo) {
		cgit_print_site_readme();
		return;
	}

	// The about page resolves relative links against its own URL, so it
	// only works with a trailing slash.
	path_info_len = ctx.env.path_info ? strlen(ctx.env.path_info) : 0;
	if (!ctx.qry.path &&
	    (!ctx.qry.url || !*ctx.qry.url ||
	     ctx.qry.url[strlen(ctx.qry.url) - 1] != '/') &&
	    (!path_info_len || ctx.env.path_info[path_info_len - 1] != '/')) {
		currenturl = cgit_currenturl();
		redirect = cgit_fmtalloc("%s/", currenturl);
		cgit_redirect(redirect, true);
		free(currenturl);
		free(redirect);
	} else if (ctx.repo->readme.nr) {
		cgit_print_repo_readme(ctx.qry.path);
	} else {
		currenturl = cgit_currenturl();
		redirect = cgit_fmtalloc("%s../", currenturl);
		cgit_redirect(redirect, false);
		free(currenturl);
		free(redirect);
	}
}

static void atom_fn(void)
{
	cgit_print_atom(ctx.qry.head, ctx.qry.path, ctx.cfg.max_atom_items);
}

static void blame_fn(void)
{
	if (ctx.repo->enable_blame)
		cgit_print_blame();
	else
		cgit_print_error_page(403, "Forbidden", "Blame is disabled");
}

static void blob_fn(void)
{
	cgit_print_blob(ctx.qry.oid, ctx.qry.path, ctx.qry.head, 0);
}

static void commit_fn(void)
{
	cgit_print_commit(ctx.qry.oid, ctx.qry.path);
}

static void diff_fn(void)
{
	cgit_print_diff(ctx.qry.oid, ctx.qry.oid2, ctx.qry.path, 1, 0);
}

static void info_fn(void)
{
	cgit_clone_info();
}

static void log_fn(void)
{
	cgit_print_log(ctx.qry.oid, ctx.qry.ofs, ctx.cfg.max_commit_count,
		       ctx.qry.grep, ctx.qry.search, ctx.qry.path, 1,
		       ctx.repo->enable_commit_graph,
		       ctx.repo->commit_sort);
}

static void ls_cache_fn(void)
{
	// The listing names the cache path and the URLs other visitors asked
	// for, so it stays off until an administrator opts in.
	if (!ctx.cfg.enable_cache_list) {
		cgit_print_error_page(404, "Not found", "Not found");
		return;
	}
	ctx.page.mimetype = "text/plain";
	ctx.page.filename = "ls-cache.txt";
	cgit_print_http_headers();
	cache_ls(ctx.cfg.cache_root);
}

static void objects_fn(void)
{
	cgit_clone_objects();
}

static void patch_fn(void)
{
	cgit_print_patch(ctx.qry.oid, ctx.qry.oid2, ctx.qry.path);
}

static void plain_fn(void)
{
	cgit_print_plain();
}

static void rawdiff_fn(void)
{
	cgit_print_diff(ctx.qry.oid, ctx.qry.oid2, ctx.qry.path, 1, 1);
}

static void refs_fn(void)
{
	cgit_print_refs();
}

static void repolist_fn(void)
{
	cgit_print_repolist();
}

static void snapshot_fn(void)
{
	cgit_print_snapshot(ctx.qry.head, ctx.qry.oid, ctx.qry.path,
			    ctx.qry.nohead);
}

static void stats_fn(void)
{
	if (ctx.repo->enable_stats)
		cgit_show_stats();
	else
		cgit_print_error_page(404, "Not found", "Stats are disabled");
}

static void summary_fn(void)
{
	cgit_print_summary();
}

static void tag_fn(void)
{
	cgit_print_tag(ctx.qry.oid);
}

static void tree_fn(void)
{
	cgit_print_tree(ctx.qry.oid, ctx.qry.path);
}

// The name is what a request spells as p=, so these strings are part of the
// URL space and cannot be renamed.
static const struct cgit_cmd commands[] = {
	{ .name = "HEAD", .fn = head_fn, .want_repo = 1, .is_clone = 1 },
	{ .name = "about", .fn = about_fn },
	{ .name = "atom", .fn = atom_fn, .want_repo = 1 },
	{ .name = "blame", .fn = blame_fn, .want_repo = 1, .want_vpath = 1 },
	{ .name = "blob", .fn = blob_fn, .want_repo = 1 },
	{ .name = "commit", .fn = commit_fn, .want_repo = 1, .want_vpath = 1 },
	{ .name = "diff", .fn = diff_fn, .want_repo = 1, .want_vpath = 1 },
	{ .name = "info", .fn = info_fn, .want_repo = 1, .is_clone = 1 },
	{ .name = "log", .fn = log_fn, .want_repo = 1, .want_vpath = 1 },
	{ .name = "ls_cache", .fn = ls_cache_fn },
	{ .name = "objects", .fn = objects_fn, .want_repo = 1, .is_clone = 1 },
	{ .name = "patch", .fn = patch_fn, .want_repo = 1, .want_vpath = 1 },
	{ .name = "plain", .fn = plain_fn, .want_repo = 1 },
	{ .name = "rawdiff", .fn = rawdiff_fn, .want_repo = 1, .want_vpath = 1 },
	{ .name = "refs", .fn = refs_fn, .want_repo = 1 },
	{ .name = "repolist", .fn = repolist_fn },
	{ .name = "snapshot", .fn = snapshot_fn, .want_repo = 1 },
	{ .name = "stats", .fn = stats_fn, .want_repo = 1, .want_vpath = 1 },
	{ .name = "summary", .fn = summary_fn, .want_repo = 1 },
	{ .name = "tag", .fn = tag_fn, .want_repo = 1 },
	{ .name = "tree", .fn = tree_fn, .want_repo = 1, .want_vpath = 1 },
};

const struct cgit_cmd *cgit_get_cmd(void)
{
	size_t i;

	if (ctx.qry.page == NULL) {
		if (ctx.repo)
			ctx.qry.page = "summary";
		else
			ctx.qry.page = "repolist";
	}

	for (i = 0; i < ARRAY_SIZE(commands); i++)
		if (!strcmp(ctx.qry.page, commands[i].name))
			return &commands[i];
	return NULL;
}