blob: 3fd393b601b792b7f6904b264a26e4597a062f52 (plain) (blame)
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
/*
 * A renderer that reads the request itself goes into the page table as it is.
 * The others sit behind a thunk here that pulls their arguments out of the
 * global ctx or does some work first, such as checking that the page is
 * enabled.
 */

#include "cache.h"
#include "cgit.h"
#include "cmd.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 about_fn(void)
{
	char *query, *summary;

	// The page shows the readme alone, and the readme's links lead into
	// the tree, so nothing lives below it.
	if (ctx.qry.path) {
		cgit_print_error_page(404, "Not Found", "Not found");
		return;
	}
	if (!ctx.repo) {
		cgit_prefix_title("about");
		cgit_print_site_readme();
		return;
	}
	if (ctx.repo->readme.nr) {
		cgit_prefix_title("about");
		cgit_print_repo_readme();
		return;
	}
	// The branch and the rest of the query travel along to the summary.
	query = cgit_currentquery();
	summary = cgit_repourl(ctx.repo->url);
	cgit_redirect(summary, query, false);
	free(query);
	free(summary);
}

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

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

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

static void log_fn(void)
{
	cgit_prefix_title("log");
	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, 0
	);
}

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";
	// The keys printed are visitor-chosen URL bytes, so the page is marked
	// untrusted to keep a browser from sniffing an HTML-shaped key into
	// markup.
	ctx.page.untrusted = 1;
	cgit_print_http_headers();
	cache_ls(ctx.cfg.cache_root);
}

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

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_prefix_title("refs");
	cgit_print_refs();
}

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->max_stats) {
		cgit_prefix_title("stats");
		cgit_show_stats();
	} else {
		cgit_print_error_page(404, "Not Found", "Stats are disabled");
	}
}

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 = cgit_clone_head, .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, .is_static = 1 },
	{ .name = "blob", .fn = blob_fn, .want_repo = 1, .is_static = 1 },
	{ .name = "commit", .fn = commit_fn, .want_repo = 1, .want_vpath = 1, .is_static = 1 },
	{ .name = "diff", .fn = diff_fn, .want_repo = 1, .want_vpath = 1, .is_static = 1 },
	{ .name = "info", .fn = cgit_clone_info, .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 = cgit_clone_objects, .want_repo = 1, .is_clone = 1 },
	{ .name = "patch", .fn = patch_fn, .want_repo = 1, .want_vpath = 1, .is_static = 1 },
	{ .name = "plain", .fn = cgit_print_plain, .want_repo = 1, .is_static = 1 },
	{ .name = "rawdiff", .fn = rawdiff_fn, .want_repo = 1, .want_vpath = 1, .is_static = 1 },
	{ .name = "refs", .fn = refs_fn, .want_repo = 1 },
	{ .name = "repolist", .fn = cgit_print_repolist },
	{ .name = "snapshot", .fn = snapshot_fn, .want_repo = 1, .is_static = 1 },
	{ .name = "stats", .fn = stats_fn, .want_repo = 1, .want_vpath = 1 },
	{ .name = "summary", .fn = cgit_print_summary, .want_repo = 1 },
	{ .name = "tag", .fn = tag_fn, .want_repo = 1, .is_static = 1 },
	{ .name = "tree", .fn = tree_fn, .want_repo = 1, .want_vpath = 1, .is_static = 1 },
};

const struct cgit_cmd *cgit_find_cmd(const char *name)
{
	size_t i;

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

const struct cgit_cmd *cgit_get_cmd(void)
{
	if (!ctx.qry.page) {
		if (ctx.repo)
			ctx.qry.page = "summary";
		else
			ctx.qry.page = "repolist";
	}
	return cgit_find_cmd(ctx.qry.page);
}