author | Lars Hjemli <hjemli@gmail.com> | 2008-03-24 00:09:39 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-03-24 00:43:48 (UTC) |
commit | e0e4478e7b4812f822d60a13a33525f8e529e1e8 (patch) (side-by-side diff) | |
tree | 577c3927deb9b122f940b69ca7db66afe2422814 | |
parent | b608e88adb6f77328288afb6dd0eddf674fc9b5b (diff) | |
download | cgit-e0e4478e7b4812f822d60a13a33525f8e529e1e8.zip cgit-e0e4478e7b4812f822d60a13a33525f8e529e1e8.tar.gz cgit-e0e4478e7b4812f822d60a13a33525f8e529e1e8.tar.bz2 |
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>
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | cgit.c | 130 | ||||
-rw-r--r-- | cgit.h | 14 | ||||
-rw-r--r-- | cmd.c | 101 | ||||
-rw-r--r-- | cmd.h | 15 | ||||
-rw-r--r-- | parsing.c | 4 | ||||
-rw-r--r-- | shared.c | 13 |
7 files changed, 169 insertions, 110 deletions
@@ -7,25 +7,25 @@ SHA1_HEADER = <openssl/sha.h> GIT_VER = 1.5.4.1 GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2 # # Let the user override the above settings. # -include cgit.conf EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ ui-summary.o ui-log.o ui-tree.o ui-commit.o ui-diff.o \ - ui-snapshot.o ui-blob.o ui-tag.o ui-refs.o ui-patch.o + ui-snapshot.o ui-blob.o ui-tag.o ui-refs.o ui-patch.o cmd.o ifdef NEEDS_LIBICONV EXTLIBS += -liconv endif .PHONY: all git test install clean distclean emptycache force-version get-git all: cgit git VERSION: force-version @@ -72,156 +72,126 @@ char *find_default_branch(struct cgit_repo *repo) struct refmatch info; info.req_ref = repo->defbranch; info.first_ref = NULL; info.match = 0; for_each_branch_ref(find_current_ref, &info); if (info.match) return info.req_ref; else return info.first_ref; } -static void cgit_print_repo_page() +static int prepare_repo_cmd(struct cgit_context *ctx) { char *tmp; - int show_search; unsigned char sha1[20]; int nongit = 0; - setenv("GIT_DIR", ctx.repo->path, 1); + setenv("GIT_DIR", ctx->repo->path, 1); setup_git_directory_gently(&nongit); if (nongit) { - ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, - "config error"); - tmp = fmt("Not a git repository: '%s'", ctx.repo->path); - ctx.repo = NULL; - cgit_print_http_headers(&ctx); - cgit_print_docstart(&ctx); - cgit_print_pageheader(&ctx); + ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, + "config error"); + tmp = fmt("Not a git repository: '%s'", ctx->repo->path); + ctx->repo = NULL; + cgit_print_http_headers(ctx); + cgit_print_docstart(ctx); + cgit_print_pageheader(ctx); cgit_print_error(tmp); cgit_print_docend(); - return; + return 1; } + ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); - ctx.page.title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc); - show_search = 0; - - if (!ctx.qry.head) { - ctx.qry.head = xstrdup(find_default_branch(ctx.repo)); - ctx.repo->defbranch = ctx.qry.head; + if (!ctx->qry.head) { + ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); + ctx->repo->defbranch = ctx->qry.head; } - if (!ctx.qry.head) { - cgit_print_http_headers(&ctx); - cgit_print_docstart(&ctx); - cgit_print_pageheader(&ctx); + if (!ctx->qry.head) { + cgit_print_http_headers(ctx); + cgit_print_docstart(ctx); + cgit_print_pageheader(ctx); cgit_print_error("Repository seems to be empty"); cgit_print_docend(); - return; + return 1; } - if (get_sha1(ctx.qry.head, sha1)) { - tmp = xstrdup(ctx.qry.head); - ctx.qry.head = ctx.repo->defbranch; - cgit_print_http_headers(&ctx); - cgit_print_docstart(&ctx); - cgit_print_pageheader(&ctx); + if (get_sha1(ctx->qry.head, sha1)) { + tmp = xstrdup(ctx->qry.head); + ctx->qry.head = ctx->repo->defbranch; + cgit_print_http_headers(ctx); + cgit_print_docstart(ctx); + cgit_print_pageheader(ctx); cgit_print_error(fmt("Invalid branch: %s", tmp)); cgit_print_docend(); - return; + return 1; } + return 0; +} - if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) { - cgit_print_snapshot(ctx.qry.head, ctx.qry.sha1, - cgit_repobasename(ctx.repo->url), - ctx.qry.path, - ctx.repo->snapshots ); +static void process_request(struct cgit_context *ctx) +{ + struct cgit_cmd *cmd; + + cmd = cgit_get_cmd(ctx); + if (!cmd) { + ctx->page.title = "cgit error"; + ctx->repo = NULL; + cgit_print_http_headers(ctx); + cgit_print_docstart(ctx); + cgit_print_pageheader(ctx); + cgit_print_error("Invalid request"); + cgit_print_docend(); return; } - if (cgit_cmd == CMD_PATCH) { - cgit_print_patch(ctx.qry.sha1); + if (cmd->want_repo && prepare_repo_cmd(ctx)) return; - } - if (cgit_cmd == CMD_BLOB) { - cgit_print_blob(ctx.qry.sha1, ctx.qry.path); - return; + if (cmd->want_layout) { + cgit_print_http_headers(ctx); + cgit_print_docstart(ctx); + cgit_print_pageheader(ctx); } - show_search = (cgit_cmd == CMD_LOG); - cgit_print_http_headers(&ctx); - cgit_print_docstart(&ctx); - if (!cgit_cmd) { - cgit_print_pageheader(&ctx); - cgit_print_summary(); - cgit_print_docend(); - return; - } + cmd->fn(ctx); - cgit_print_pageheader(&ctx); - - switch(cgit_cmd) { - case CMD_LOG: - cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, - ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search, - ctx.qry.path, 1); - break; - case CMD_TREE: - cgit_print_tree(ctx.qry.sha1, ctx.qry.path); - break; - case CMD_COMMIT: - cgit_print_commit(ctx.qry.sha1); - break; - case CMD_REFS: - cgit_print_refs(); - break; - case CMD_TAG: - cgit_print_tag(ctx.qry.sha1); - break; - case CMD_DIFF: - cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); - break; - default: - cgit_print_error("Invalid request"); - } - cgit_print_docend(); + if (cmd->want_layout) + cgit_print_docend(); } static long ttl_seconds(long ttl) { if (ttl<0) return 60 * 60 * 24 * 365; else return ttl * 60; } static void cgit_fill_cache(struct cacheitem *item, int use_cache) { int stdout2; if (use_cache) { stdout2 = chk_positive(dup(STDOUT_FILENO), "Preserving STDOUT"); chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); } ctx.page.modified = time(NULL); ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); - if (ctx.repo) - cgit_print_repo_page(); - else - cgit_print_repolist(); + process_request(&ctx); if (use_cache) { chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); chk_positive(dup2(stdout2, STDOUT_FILENO), "Restoring original STDOUT"); chk_zero(close(stdout2), "Closing temporary STDOUT"); } } static void cgit_check_cache(struct cacheitem *item) { int i = 0; @@ -11,37 +11,24 @@ #include <tag.h> #include <diff.h> #include <diffcore.h> #include <refs.h> #include <revision.h> #include <log-tree.h> #include <archive.h> #include <xdiff/xdiff.h> #include <utf8.h> /* - * The valid cgit repo-commands - */ -#define CMD_LOG 1 -#define CMD_COMMIT 2 -#define CMD_DIFF 3 -#define CMD_TREE 4 -#define CMD_BLOB 5 -#define CMD_SNAPSHOT 6 -#define CMD_TAG 7 -#define CMD_REFS 8 -#define CMD_PATCH 9 - -/* * Dateformats used on misc. pages */ #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" #define FMT_SHORTDATE "%Y-%m-%d" /* * Limits used for relative dates */ #define TM_MIN 60 #define TM_HOUR (TM_MIN * 60) #define TM_DAY (TM_HOUR * 24) @@ -188,25 +175,24 @@ struct cgit_context { struct cgit_config cfg; struct cgit_repo *repo; struct cgit_page page; }; extern const char *cgit_version; extern struct cgit_repolist cgit_repolist; extern struct cgit_context ctx; extern int cgit_cmd; extern void cgit_prepare_context(struct cgit_context *ctx); -extern int cgit_get_cmd_index(const char *cmd); extern struct cgit_repo *cgit_get_repoinfo(const char *url); extern void cgit_global_config_cb(const char *name, const char *value); extern void cgit_repo_config_cb(const char *name, const char *value); extern void cgit_querystring_cb(const char *name, const char *value); extern int chk_zero(int result, char *msg); extern int chk_positive(int result, char *msg); extern int chk_non_negative(int result, char *msg); extern int hextoint(char c); extern char *trim_end(const char *str, char c); extern char *strlpart(char *txt, int maxlen); @@ -0,0 +1,101 @@ +/* cmd.c: the cgit command dispatcher + * + * Copyright (C) 2008 Lars Hjemli + * + * Licensed under GNU General Public License v2 + * (see COPYING for full license text) + */ + +#include "cgit.h" +#include "cmd.h" + +static void blob_fn(struct cgit_context *ctx) +{ + cgit_print_blob(ctx->qry.sha1, ctx->qry.path); +} + +static void commit_fn(struct cgit_context *ctx) +{ + cgit_print_commit(ctx->qry.sha1); +} + +static void diff_fn(struct cgit_context *ctx) +{ + cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path); +} + +static void repolist_fn(struct cgit_context *ctx) +{ + cgit_print_repolist(); +} + +static void log_fn(struct cgit_context *ctx) +{ + cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count, + ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1); +} + +static void patch_fn(struct cgit_context *ctx) +{ + cgit_print_patch(ctx->qry.sha1); +} + +static void refs_fn(struct cgit_context *ctx) +{ + cgit_print_refs(); +} + +static void snapshot_fn(struct cgit_context *ctx) +{ + cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, + cgit_repobasename(ctx->repo->url), ctx->qry.path, + ctx->repo->snapshots); +} + +static void summary_fn(struct cgit_context *ctx) +{ + cgit_print_summary(); +} + +static void tag_fn(struct cgit_context *ctx) +{ + cgit_print_tag(ctx->qry.sha1); +} + +static void tree_fn(struct cgit_context *ctx) +{ + cgit_print_tree(ctx->qry.sha1, ctx->qry.path); +} + +#define def_cmd(name, want_repo, want_layout) \ + {#name, name##_fn, want_repo, want_layout} + +struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) +{ + static struct cgit_cmd cmds[] = { + def_cmd(blob, 1, 0), + def_cmd(commit, 1, 1), + def_cmd(diff, 1, 1), + def_cmd(log, 1, 1), + def_cmd(patch, 1, 0), + def_cmd(refs, 1, 1), + def_cmd(repolist, 0, 0), + def_cmd(snapshot, 1, 0), + def_cmd(summary, 1, 1), + def_cmd(tag, 1, 1), + def_cmd(tree, 1, 1), + }; + int i; + + if (ctx->qry.page == NULL) { + if (ctx->repo) + ctx->qry.page = "summary"; + else + ctx->qry.page = "repolist"; + } + + for(i = 0; i < sizeof(cmds)/sizeof(*cmds); i++) + if (!strcmp(ctx->qry.page, cmds[i].name)) + return &cmds[i]; + return NULL; +} @@ -0,0 +1,15 @@ +#ifndef CMD_H +#define CMD_H + +typedef void (*cgit_cmd_fn)(struct cgit_context *ctx); + +struct cgit_cmd { + const char *name; + cgit_cmd_fn fn; + unsigned int want_repo:1, + want_layout:1; +}; + +extern struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx); + +#endif /* CMD_H */ @@ -161,26 +161,26 @@ void cgit_parse_url(const char *url) cmd[0] = '/'; cmd = strchr(cmd + 1, '/'); continue; } ctx.qry.repo = ctx.repo->url; p = strchr(cmd + 1, '/'); if (p) { p[0] = '\0'; if (p[1]) ctx.qry.path = trim_end(p + 1, '/'); } - cgit_cmd = cgit_get_cmd_index(cmd + 1); - ctx.qry.page = xstrdup(cmd + 1); + if (cmd[1]) + ctx.qry.page = xstrdup(cmd + 1); return; } } char *substr(const char *head, const char *tail) { char *buf; buf = xmalloc(tail - head + 1); strncpy(buf, head, tail - head); buf[tail - head] = '\0'; return buf; @@ -31,36 +31,24 @@ void cgit_prepare_context(struct cgit_context *ctx) ctx->cfg.max_msg_len = 60; ctx->cfg.max_repodesc_len = 60; ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; ctx->cfg.renamelimit = -1; ctx->cfg.robots = "index, nofollow"; ctx->cfg.root_title = "Git repository browser"; ctx->cfg.script_name = CGIT_SCRIPT_NAME; ctx->page.mimetype = "text/html"; ctx->page.charset = PAGE_ENCODING; ctx->page.filename = NULL; } -int cgit_get_cmd_index(const char *cmd) -{ - static char *cmds[] = {"log", "commit", "diff", "tree", "blob", - "snapshot", "tag", "refs", "patch", NULL}; - int i; - - for(i = 0; cmds[i]; i++) - if (!strcmp(cmd, cmds[i])) - return i + 1; - return 0; -} - int chk_zero(int result, char *msg) { if (result != 0) die("%s: %s", msg, strerror(errno)); return result; } int chk_positive(int result, char *msg) { if (result <= 0) die("%s: %s", msg, strerror(errno)); return result; @@ -207,25 +195,24 @@ void cgit_global_config_cb(const char *name, const char *value) ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); } else if (!strcmp(name, "include")) cgit_read_config(value, cgit_global_config_cb); } void cgit_querystring_cb(const char *name, const char *value) { if (!strcmp(name,"r")) { ctx.qry.repo = xstrdup(value); ctx.repo = cgit_get_repoinfo(value); } else if (!strcmp(name, "p")) { ctx.qry.page = xstrdup(value); - cgit_cmd = cgit_get_cmd_index(value); } else if (!strcmp(name, "url")) { cgit_parse_url(value); } else if (!strcmp(name, "qt")) { ctx.qry.grep = xstrdup(value); } else if (!strcmp(name, "q")) { ctx.qry.search = xstrdup(value); } else if (!strcmp(name, "h")) { ctx.qry.head = xstrdup(value); ctx.qry.has_symref = 1; } else if (!strcmp(name, "id")) { ctx.qry.sha1 = xstrdup(value); ctx.qry.has_sha1 = 1; |