diff options
| author | Lars Hjemli | 2008-03-24 01:09:39 +0100 |
|---|---|---|
| committer | Lars Hjemli | 2008-03-24 01:43:48 +0100 |
| commit | e0e4478e7b4812f822d60a13a33525f8e529e1e8 (patch) | |
| tree | 577c3927deb9b122f940b69ca7db66afe2422814 /cgit.c | |
| parent | b608e88adb6f77328288afb6dd0eddf674fc9b5b (diff) | |
| download | cgit-e0e4478e7b4812f822d60a13a33525f8e529e1e8.tar.gz cgit-e0e4478e7b4812f822d60a13a33525f8e529e1e8.tar.bz2 cgit-e0e4478e7b4812f822d60a13a33525f8e529e1e8.zip | |
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 'cgit.c')
| -rw-r--r-- | cgit.c | 130 |
1 files changed, 50 insertions, 80 deletions
| @@ -81,111 +81,84 @@ char *find_default_branch(struct cgit_repo *repo) | |||
| 81 | return info.first_ref; | 81 | return info.first_ref; |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | static void cgit_print_repo_page() | 84 | static int prepare_repo_cmd(struct cgit_context *ctx) |
| 85 | { | 85 | { |
| 86 | char *tmp; | 86 | char *tmp; |
| 87 | int show_search; | ||
| 88 | unsigned char sha1[20]; | 87 | unsigned char sha1[20]; |
| 89 | int nongit = 0; | 88 | int nongit = 0; |
| 90 | 89 | ||
| 91 | setenv("GIT_DIR", ctx.repo->path, 1); | 90 | setenv("GIT_DIR", ctx->repo->path, 1); |
| 92 | setup_git_directory_gently(&nongit); | 91 | setup_git_directory_gently(&nongit); |
| 93 | if (nongit) { | 92 | if (nongit) { |
| 94 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, | 93 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
| 95 | "config error"); | 94 | "config error"); |
| 96 | tmp = fmt("Not a git repository: '%s'", ctx.repo->path); | 95 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
| 97 | ctx.repo = NULL; | 96 | ctx->repo = NULL; |
| 98 | cgit_print_http_headers(&ctx); | 97 | cgit_print_http_headers(ctx); |
| 99 | cgit_print_docstart(&ctx); | 98 | cgit_print_docstart(ctx); |
| 100 | cgit_print_pageheader(&ctx); | 99 | cgit_print_pageheader(ctx); |
| 101 | cgit_print_error(tmp); | 100 | cgit_print_error(tmp); |
| 102 | cgit_print_docend(); | 101 | cgit_print_docend(); |
| 103 | return; | 102 | return 1; |
| 104 | } | 103 | } |
| 104 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | ||
| 105 | 105 | ||
| 106 | ctx.page.title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc); | 106 | if (!ctx->qry.head) { |
| 107 | show_search = 0; | 107 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
| 108 | 108 | ctx->repo->defbranch = ctx->qry.head; | |
| 109 | if (!ctx.qry.head) { | ||
| 110 | ctx.qry.head = xstrdup(find_default_branch(ctx.repo)); | ||
| 111 | ctx.repo->defbranch = ctx.qry.head; | ||
| 112 | } | 109 | } |
| 113 | 110 | ||
| 114 | if (!ctx.qry.head) { | 111 | if (!ctx->qry.head) { |
| 115 | cgit_print_http_headers(&ctx); | 112 | cgit_print_http_headers(ctx); |
| 116 | cgit_print_docstart(&ctx); | 113 | cgit_print_docstart(ctx); |
| 117 | cgit_print_pageheader(&ctx); | 114 | cgit_print_pageheader(ctx); |
| 118 | cgit_print_error("Repository seems to be empty"); | 115 | cgit_print_error("Repository seems to be empty"); |
| 119 | cgit_print_docend(); | 116 | cgit_print_docend(); |
| 120 | return; | 117 | return 1; |
| 121 | } | 118 | } |
| 122 | 119 | ||
| 123 | if (get_sha1(ctx.qry.head, sha1)) { | 120 | if (get_sha1(ctx->qry.head, sha1)) { |
| 124 | tmp = xstrdup(ctx.qry.head); | 121 | tmp = xstrdup(ctx->qry.head); |
| 125 | ctx.qry.head = ctx.repo->defbranch; | 122 | ctx->qry.head = ctx->repo->defbranch; |
| 126 | cgit_print_http_headers(&ctx); | 123 | cgit_print_http_headers(ctx); |
| 127 | cgit_print_docstart(&ctx); | 124 | cgit_print_docstart(ctx); |
| 128 | cgit_print_pageheader(&ctx); | 125 | cgit_print_pageheader(ctx); |
| 129 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 126 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
| 130 | cgit_print_docend(); | 127 | cgit_print_docend(); |
| 131 | return; | 128 | return 1; |
| 132 | } | 129 | } |
| 130 | return 0; | ||
| 131 | } | ||
| 133 | 132 | ||
| 134 | if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) { | 133 | static void process_request(struct cgit_context *ctx) |
| 135 | cgit_print_snapshot(ctx.qry.head, ctx.qry.sha1, | 134 | { |
| 136 | cgit_repobasename(ctx.repo->url), | 135 | struct cgit_cmd *cmd; |
| 137 | ctx.qry.path, | 136 | |
| 138 | ctx.repo->snapshots ); | 137 | cmd = cgit_get_cmd(ctx); |
| 138 | if (!cmd) { | ||
| 139 | ctx->page.title = "cgit error"; | ||
| 140 | ctx->repo = NULL; | ||
| 141 | cgit_print_http_headers(ctx); | ||
| 142 | cgit_print_docstart(ctx); | ||
| 143 | cgit_print_pageheader(ctx); | ||
| 144 | cgit_print_error("Invalid request"); | ||
| 145 | cgit_print_docend(); | ||
| 139 | return; | 146 | return; |
| 140 | } | 147 | } |
| 141 | 148 | ||
| 142 | if (cgit_cmd == CMD_PATCH) { | 149 | if (cmd->want_repo && prepare_repo_cmd(ctx)) |
| 143 | cgit_print_patch(ctx.qry.sha1); | ||
| 144 | return; | 150 | return; |
| 145 | } | ||
| 146 | 151 | ||
| 147 | if (cgit_cmd == CMD_BLOB) { | 152 | if (cmd->want_layout) { |
| 148 | cgit_print_blob(ctx.qry.sha1, ctx.qry.path); | 153 | cgit_print_http_headers(ctx); |
| 149 | return; | 154 | cgit_print_docstart(ctx); |
| 155 | cgit_print_pageheader(ctx); | ||
| 150 | } | 156 | } |
| 151 | 157 | ||
| 152 | show_search = (cgit_cmd == CMD_LOG); | 158 | cmd->fn(ctx); |
| 153 | cgit_print_http_headers(&ctx); | ||
| 154 | cgit_print_docstart(&ctx); | ||
| 155 | if (!cgit_cmd) { | ||
| 156 | cgit_print_pageheader(&ctx); | ||
| 157 | cgit_print_summary(); | ||
| 158 | cgit_print_docend(); | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | 159 | ||
| 162 | cgit_print_pageheader(&ctx); | 160 | if (cmd->want_layout) |
| 163 | 161 | cgit_print_docend(); | |
| 164 | switch(cgit_cmd) { | ||
| 165 | case CMD_LOG: | ||
| 166 | cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, | ||
| 167 | ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search, | ||
| 168 | ctx.qry.path, 1); | ||
| 169 | break; | ||
| 170 | case CMD_TREE: | ||
| 171 | cgit_print_tree(ctx.qry.sha1, ctx.qry.path); | ||
| 172 | break; | ||
| 173 | case CMD_COMMIT: | ||
| 174 | cgit_print_commit(ctx.qry.sha1); | ||
| 175 | break; | ||
| 176 | case CMD_REFS: | ||
| 177 | cgit_print_refs(); | ||
| 178 | break; | ||
| 179 | case CMD_TAG: | ||
| 180 | cgit_print_tag(ctx.qry.sha1); | ||
| 181 | break; | ||
| 182 | case CMD_DIFF: | ||
| 183 | cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); | ||
| 184 | break; | ||
| 185 | default: | ||
| 186 | cgit_print_error("Invalid request"); | ||
| 187 | } | ||
| 188 | cgit_print_docend(); | ||
| 189 | } | 162 | } |
| 190 | 163 | ||
| 191 | static long ttl_seconds(long ttl) | 164 | static long ttl_seconds(long ttl) |
| @@ -209,10 +182,7 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache) | |||
| 209 | 182 | ||
| 210 | ctx.page.modified = time(NULL); | 183 | ctx.page.modified = time(NULL); |
| 211 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); | 184 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); |
| 212 | if (ctx.repo) | 185 | process_request(&ctx); |
| 213 | cgit_print_repo_page(); | ||
| 214 | else | ||
| 215 | cgit_print_repolist(); | ||
| 216 | 186 | ||
| 217 | if (use_cache) { | 187 | if (use_cache) { |
| 218 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | 188 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
