aboutsummaryrefslogtreecommitdiffstats
path: root/cmd.c
diff options
context:
space:
mode:
authorGravatar Lars Hjemli <hjemli@gmail.com>2008-03-24 09:09:39 (JST)
committerGravatar Lars Hjemli <hjemli@gmail.com>2008-03-24 09:43:48 (JST)
commite0e4478e7b4812f822d60a13a33525f8e529e1e8 (patch)
tree577c3927deb9b122f940b69ca7db66afe2422814 /cmd.c
parentb608e88adb6f77328288afb6dd0eddf674fc9b5b (diff)
downloadcgit-e0e4478e7b4812f822d60a13a33525f8e529e1e8.zip
cgit-e0e4478e7b4812f822d60a13a33525f8e529e1e8.tar.gz
Add command dispatcher
This simplifies the code in cgit.c and makes it easier to extend cgit with new pages/commands. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'cmd.c')
-rw-r--r--cmd.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/cmd.c b/cmd.c
new file mode 100644
index 0000000..489a11c
--- /dev/null
+++ b/cmd.c
@@ -0,0 +1,101 @@
1/* cmd.c: the cgit command dispatcher
2 *
3 * Copyright (C) 2008 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
10#include "cmd.h"
11
12static void blob_fn(struct cgit_context *ctx)
13{
14 cgit_print_blob(ctx->qry.sha1, ctx->qry.path);
15}
16
17static void commit_fn(struct cgit_context *ctx)
18{
19 cgit_print_commit(ctx->qry.sha1);
20}
21
22static void diff_fn(struct cgit_context *ctx)
23{
24 cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path);
25}
26
27static void repolist_fn(struct cgit_context *ctx)
28{
29 cgit_print_repolist();
30}
31
32static void log_fn(struct cgit_context *ctx)
33{
34 cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count,
35 ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1);
36}
37
38static void patch_fn(struct cgit_context *ctx)
39{
40 cgit_print_patch(ctx->qry.sha1);
41}
42
43static void refs_fn(struct cgit_context *ctx)
44{
45 cgit_print_refs();
46}
47
48static void snapshot_fn(struct cgit_context *ctx)
49{
50 cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1,
51 cgit_repobasename(ctx->repo->url), ctx->qry.path,
52 ctx->repo->snapshots);
53}
54
55static void summary_fn(struct cgit_context *ctx)
56{
57 cgit_print_summary();
58}
59
60static void tag_fn(struct cgit_context *ctx)
61{
62 cgit_print_tag(ctx->qry.sha1);
63}
64
65static void tree_fn(struct cgit_context *ctx)
66{
67 cgit_print_tree(ctx->qry.sha1, ctx->qry.path);
68}
69
70#define def_cmd(name, want_repo, want_layout) \
71 {#name, name##_fn, want_repo, want_layout}
72
73struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx)
74{
75 static struct cgit_cmd cmds[] = {
76 def_cmd(blob, 1, 0),
77 def_cmd(commit, 1, 1),
78 def_cmd(diff, 1, 1),
79 def_cmd(log, 1, 1),
80 def_cmd(patch, 1, 0),
81 def_cmd(refs, 1, 1),
82 def_cmd(repolist, 0, 0),
83 def_cmd(snapshot, 1, 0),
84 def_cmd(summary, 1, 1),
85 def_cmd(tag, 1, 1),
86 def_cmd(tree, 1, 1),
87 };
88 int i;
89
90 if (ctx->qry.page == NULL) {
91 if (ctx->repo)
92 ctx->qry.page = "summary";
93 else
94 ctx->qry.page = "repolist";
95 }
96
97 for(i = 0; i < sizeof(cmds)/sizeof(*cmds); i++)
98 if (!strcmp(ctx->qry.page, cmds[i].name))
99 return &cmds[i];
100 return NULL;
101}