diff options
| author | 2008-04-09 04:29:21 (JST) | |
|---|---|---|
| committer | 2008-04-09 04:29:21 (JST) | |
| commit | 23296ad648c0e2a9e3cf40a3de322b10ad25cce3 (patch) | |
| tree | 136493d8228b0ff4971feb06b0e8aee296367b00 /cgit.c | |
| parent | e2a44cf0923398396b7a321d5ce894ad3bf6f580 (diff) | |
| parent | c6f747649ace1a92ed5dfaae9cc1ea3affe0bf51 (diff) | |
| download | cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.zip cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.tar.gz | |
Merge branch 'lh/cleanup'
* lh/cleanup: (21 commits)
Reset ctx.repo to NULL when the config parser is finished
Move cgit_parse_query() from parsing.c to html.c as http_parse_querystring()
Move function for configfile parsing into configfile.[ch]
Add cache.h
Remove global and obsolete cgit_cmd
Makefile: copy the QUIET constructs from the Makefile in git.git
Move cgit_version from shared.c to cgit.c
Makefile: autobuild dependency rules
Initial Makefile cleanup
Move non-generic functions from shared.c to cgit.c
Add ui-shared.h
Add separate header-files for each page/view
Refactor snapshot support
Add command dispatcher
Remove obsolete cacheitem parameter to ui-functions
Add struct cgit_page to cgit_context
Introduce html.h
Improve initialization of git directory
Move cgit_repo into cgit_context
Add all config variables into struct cgit_context
...
Diffstat (limited to 'cgit.c')
| -rw-r--r-- | cgit.c | 391 |
1 files changed, 266 insertions, 125 deletions
| @@ -7,40 +7,199 @@ | |||
| 7 | */ | 7 | */ |
| 8 | 8 | ||
| 9 | #include "cgit.h" | 9 | #include "cgit.h" |
| 10 | #include "cache.h" | ||
| 11 | #include "cmd.h" | ||
| 12 | #include "configfile.h" | ||
| 13 | #include "html.h" | ||
| 14 | #include "ui-shared.h" | ||
| 15 | |||
| 16 | const char *cgit_version = CGIT_VERSION; | ||
| 17 | |||
| 18 | void config_cb(const char *name, const char *value) | ||
| 19 | { | ||
| 20 | if (!strcmp(name, "root-title")) | ||
| 21 | ctx.cfg.root_title = xstrdup(value); | ||
| 22 | else if (!strcmp(name, "css")) | ||
| 23 | ctx.cfg.css = xstrdup(value); | ||
| 24 | else if (!strcmp(name, "logo")) | ||
| 25 | ctx.cfg.logo = xstrdup(value); | ||
| 26 | else if (!strcmp(name, "index-header")) | ||
| 27 | ctx.cfg.index_header = xstrdup(value); | ||
| 28 | else if (!strcmp(name, "index-info")) | ||
| 29 | ctx.cfg.index_info = xstrdup(value); | ||
| 30 | else if (!strcmp(name, "logo-link")) | ||
| 31 | ctx.cfg.logo_link = xstrdup(value); | ||
| 32 | else if (!strcmp(name, "module-link")) | ||
| 33 | ctx.cfg.module_link = xstrdup(value); | ||
| 34 | else if (!strcmp(name, "virtual-root")) { | ||
| 35 | ctx.cfg.virtual_root = trim_end(value, '/'); | ||
| 36 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | ||
| 37 | ctx.cfg.virtual_root = ""; | ||
| 38 | } else if (!strcmp(name, "nocache")) | ||
| 39 | ctx.cfg.nocache = atoi(value); | ||
| 40 | else if (!strcmp(name, "snapshots")) | ||
| 41 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | ||
| 42 | else if (!strcmp(name, "enable-index-links")) | ||
| 43 | ctx.cfg.enable_index_links = atoi(value); | ||
| 44 | else if (!strcmp(name, "enable-log-filecount")) | ||
| 45 | ctx.cfg.enable_log_filecount = atoi(value); | ||
| 46 | else if (!strcmp(name, "enable-log-linecount")) | ||
| 47 | ctx.cfg.enable_log_linecount = atoi(value); | ||
| 48 | else if (!strcmp(name, "cache-root")) | ||
| 49 | ctx.cfg.cache_root = xstrdup(value); | ||
| 50 | else if (!strcmp(name, "cache-root-ttl")) | ||
| 51 | ctx.cfg.cache_root_ttl = atoi(value); | ||
| 52 | else if (!strcmp(name, "cache-repo-ttl")) | ||
| 53 | ctx.cfg.cache_repo_ttl = atoi(value); | ||
| 54 | else if (!strcmp(name, "cache-static-ttl")) | ||
| 55 | ctx.cfg.cache_static_ttl = atoi(value); | ||
| 56 | else if (!strcmp(name, "cache-dynamic-ttl")) | ||
| 57 | ctx.cfg.cache_dynamic_ttl = atoi(value); | ||
| 58 | else if (!strcmp(name, "max-message-length")) | ||
| 59 | ctx.cfg.max_msg_len = atoi(value); | ||
| 60 | else if (!strcmp(name, "max-repodesc-length")) | ||
| 61 | ctx.cfg.max_repodesc_len = atoi(value); | ||
| 62 | else if (!strcmp(name, "max-commit-count")) | ||
| 63 | ctx.cfg.max_commit_count = atoi(value); | ||
| 64 | else if (!strcmp(name, "summary-log")) | ||
| 65 | ctx.cfg.summary_log = atoi(value); | ||
| 66 | else if (!strcmp(name, "summary-branches")) | ||
| 67 | ctx.cfg.summary_branches = atoi(value); | ||
| 68 | else if (!strcmp(name, "summary-tags")) | ||
| 69 | ctx.cfg.summary_tags = atoi(value); | ||
| 70 | else if (!strcmp(name, "agefile")) | ||
| 71 | ctx.cfg.agefile = xstrdup(value); | ||
| 72 | else if (!strcmp(name, "renamelimit")) | ||
| 73 | ctx.cfg.renamelimit = atoi(value); | ||
| 74 | else if (!strcmp(name, "robots")) | ||
| 75 | ctx.cfg.robots = xstrdup(value); | ||
| 76 | else if (!strcmp(name, "clone-prefix")) | ||
| 77 | ctx.cfg.clone_prefix = xstrdup(value); | ||
| 78 | else if (!strcmp(name, "repo.group")) | ||
| 79 | ctx.cfg.repo_group = xstrdup(value); | ||
| 80 | else if (!strcmp(name, "repo.url")) | ||
| 81 | ctx.repo = cgit_add_repo(value); | ||
| 82 | else if (!strcmp(name, "repo.name")) | ||
| 83 | ctx.repo->name = xstrdup(value); | ||
| 84 | else if (ctx.repo && !strcmp(name, "repo.path")) | ||
| 85 | ctx.repo->path = trim_end(value, '/'); | ||
| 86 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | ||
| 87 | ctx.repo->clone_url = xstrdup(value); | ||
| 88 | else if (ctx.repo && !strcmp(name, "repo.desc")) | ||
| 89 | ctx.repo->desc = xstrdup(value); | ||
| 90 | else if (ctx.repo && !strcmp(name, "repo.owner")) | ||
| 91 | ctx.repo->owner = xstrdup(value); | ||
| 92 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | ||
| 93 | ctx.repo->defbranch = xstrdup(value); | ||
| 94 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | ||
| 95 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | ||
| 96 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | ||
| 97 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | ||
| 98 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | ||
| 99 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | ||
| 100 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | ||
| 101 | ctx.repo->module_link= xstrdup(value); | ||
| 102 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | ||
| 103 | if (*value == '/') | ||
| 104 | ctx.repo->readme = xstrdup(value); | ||
| 105 | else | ||
| 106 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | ||
| 107 | } else if (!strcmp(name, "include")) | ||
| 108 | parse_configfile(value, config_cb); | ||
| 109 | } | ||
| 110 | |||
| 111 | static void querystring_cb(const char *name, const char *value) | ||
| 112 | { | ||
| 113 | if (!strcmp(name,"r")) { | ||
| 114 | ctx.qry.repo = xstrdup(value); | ||
| 115 | ctx.repo = cgit_get_repoinfo(value); | ||
| 116 | } else if (!strcmp(name, "p")) { | ||
| 117 | ctx.qry.page = xstrdup(value); | ||
| 118 | } else if (!strcmp(name, "url")) { | ||
| 119 | cgit_parse_url(value); | ||
| 120 | } else if (!strcmp(name, "qt")) { | ||
| 121 | ctx.qry.grep = xstrdup(value); | ||
| 122 | } else if (!strcmp(name, "q")) { | ||
| 123 | ctx.qry.search = xstrdup(value); | ||
| 124 | } else if (!strcmp(name, "h")) { | ||
| 125 | ctx.qry.head = xstrdup(value); | ||
| 126 | ctx.qry.has_symref = 1; | ||
| 127 | } else if (!strcmp(name, "id")) { | ||
| 128 | ctx.qry.sha1 = xstrdup(value); | ||
| 129 | ctx.qry.has_sha1 = 1; | ||
| 130 | } else if (!strcmp(name, "id2")) { | ||
| 131 | ctx.qry.sha2 = xstrdup(value); | ||
| 132 | ctx.qry.has_sha1 = 1; | ||
| 133 | } else if (!strcmp(name, "ofs")) { | ||
| 134 | ctx.qry.ofs = atoi(value); | ||
| 135 | } else if (!strcmp(name, "path")) { | ||
| 136 | ctx.qry.path = trim_end(value, '/'); | ||
| 137 | } else if (!strcmp(name, "name")) { | ||
| 138 | ctx.qry.name = xstrdup(value); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | static void prepare_context(struct cgit_context *ctx) | ||
| 143 | { | ||
| 144 | memset(ctx, 0, sizeof(ctx)); | ||
| 145 | ctx->cfg.agefile = "info/web/last-modified"; | ||
| 146 | ctx->cfg.cache_dynamic_ttl = 5; | ||
| 147 | ctx->cfg.cache_max_create_time = 5; | ||
| 148 | ctx->cfg.cache_repo_ttl = 5; | ||
| 149 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | ||
| 150 | ctx->cfg.cache_root_ttl = 5; | ||
| 151 | ctx->cfg.cache_static_ttl = -1; | ||
| 152 | ctx->cfg.css = "/cgit.css"; | ||
| 153 | ctx->cfg.logo = "/git-logo.png"; | ||
| 154 | ctx->cfg.max_commit_count = 50; | ||
| 155 | ctx->cfg.max_lock_attempts = 5; | ||
| 156 | ctx->cfg.max_msg_len = 60; | ||
| 157 | ctx->cfg.max_repodesc_len = 60; | ||
| 158 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | ||
| 159 | ctx->cfg.renamelimit = -1; | ||
| 160 | ctx->cfg.robots = "index, nofollow"; | ||
| 161 | ctx->cfg.root_title = "Git repository browser"; | ||
| 162 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | ||
| 163 | ctx->page.mimetype = "text/html"; | ||
| 164 | ctx->page.charset = PAGE_ENCODING; | ||
| 165 | ctx->page.filename = NULL; | ||
| 166 | } | ||
| 10 | 167 | ||
| 11 | static int cgit_prepare_cache(struct cacheitem *item) | 168 | static int cgit_prepare_cache(struct cacheitem *item) |
| 12 | { | 169 | { |
| 13 | if (!cgit_repo && cgit_query_repo) { | 170 | if (!ctx.repo && ctx.qry.repo) { |
| 14 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); | 171 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
| 15 | cgit_print_docstart(title, item); | 172 | "Bad request"); |
| 16 | cgit_print_pageheader(title, 0); | 173 | cgit_print_http_headers(&ctx); |
| 17 | cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo)); | 174 | cgit_print_docstart(&ctx); |
| 175 | cgit_print_pageheader(&ctx); | ||
| 176 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | ||
| 18 | cgit_print_docend(); | 177 | cgit_print_docend(); |
| 19 | return 0; | 178 | return 0; |
| 20 | } | 179 | } |
| 21 | 180 | ||
| 22 | if (!cgit_repo) { | 181 | if (!ctx.repo) { |
| 23 | item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); | 182 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
| 24 | item->ttl = cgit_cache_root_ttl; | 183 | item->ttl = ctx.cfg.cache_root_ttl; |
| 25 | return 1; | 184 | return 1; |
| 26 | } | 185 | } |
| 27 | 186 | ||
| 28 | if (!cgit_cmd) { | 187 | if (!ctx.qry.page) { |
| 29 | item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root, | 188 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
| 30 | cache_safe_filename(cgit_repo->url), | 189 | cache_safe_filename(ctx.repo->url), |
| 31 | cache_safe_filename(cgit_querystring))); | 190 | cache_safe_filename(ctx.qry.raw))); |
| 32 | item->ttl = cgit_cache_repo_ttl; | 191 | item->ttl = ctx.cfg.cache_repo_ttl; |
| 33 | } else { | 192 | } else { |
| 34 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, | 193 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
| 35 | cache_safe_filename(cgit_repo->url), | 194 | cache_safe_filename(ctx.repo->url), |
| 36 | cgit_query_page, | 195 | ctx.qry.page, |
| 37 | cache_safe_filename(cgit_querystring))); | 196 | cache_safe_filename(ctx.qry.raw))); |
| 38 | if (cgit_query_has_symref) | 197 | if (ctx.qry.has_symref) |
| 39 | item->ttl = cgit_cache_dynamic_ttl; | 198 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
| 40 | else if (cgit_query_has_sha1) | 199 | else if (ctx.qry.has_sha1) |
| 41 | item->ttl = cgit_cache_static_ttl; | 200 | item->ttl = ctx.cfg.cache_static_ttl; |
| 42 | else | 201 | else |
| 43 | item->ttl = cgit_cache_repo_ttl; | 202 | item->ttl = ctx.cfg.cache_repo_ttl; |
| 44 | } | 203 | } |
| 45 | return 1; | 204 | return 1; |
| 46 | } | 205 | } |
| @@ -64,7 +223,7 @@ int find_current_ref(const char *refname, const unsigned char *sha1, | |||
| 64 | return info->match; | 223 | return info->match; |
| 65 | } | 224 | } |
| 66 | 225 | ||
| 67 | char *find_default_branch(struct repoinfo *repo) | 226 | char *find_default_branch(struct cgit_repo *repo) |
| 68 | { | 227 | { |
| 69 | struct refmatch info; | 228 | struct refmatch info; |
| 70 | 229 | ||
| @@ -78,113 +237,98 @@ char *find_default_branch(struct repoinfo *repo) | |||
| 78 | return info.first_ref; | 237 | return info.first_ref; |
| 79 | } | 238 | } |
| 80 | 239 | ||
| 81 | static void cgit_print_repo_page(struct cacheitem *item) | 240 | static int prepare_repo_cmd(struct cgit_context *ctx) |
| 82 | { | 241 | { |
| 83 | char *title, *tmp; | 242 | char *tmp; |
| 84 | int show_search; | ||
| 85 | unsigned char sha1[20]; | 243 | unsigned char sha1[20]; |
| 86 | 244 | int nongit = 0; | |
| 87 | if (chdir(cgit_repo->path)) { | 245 | |
| 88 | title = fmt("%s - %s", cgit_root_title, "Bad request"); | 246 | setenv("GIT_DIR", ctx->repo->path, 1); |
| 89 | cgit_print_docstart(title, item); | 247 | setup_git_directory_gently(&nongit); |
| 90 | cgit_print_pageheader(title, 0); | 248 | if (nongit) { |
| 91 | cgit_print_error(fmt("Unable to scan repository: %s", | 249 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
| 92 | strerror(errno))); | 250 | "config error"); |
| 251 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); | ||
| 252 | ctx->repo = NULL; | ||
| 253 | cgit_print_http_headers(ctx); | ||
| 254 | cgit_print_docstart(ctx); | ||
| 255 | cgit_print_pageheader(ctx); | ||
| 256 | cgit_print_error(tmp); | ||
| 93 | cgit_print_docend(); | 257 | cgit_print_docend(); |
| 94 | return; | 258 | return 1; |
| 95 | } | 259 | } |
| 260 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | ||
| 96 | 261 | ||
| 97 | title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); | 262 | if (!ctx->qry.head) { |
| 98 | show_search = 0; | 263 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
| 99 | setenv("GIT_DIR", cgit_repo->path, 1); | 264 | ctx->repo->defbranch = ctx->qry.head; |
| 100 | |||
| 101 | if (!cgit_query_head) { | ||
| 102 | cgit_query_head = xstrdup(find_default_branch(cgit_repo)); | ||
| 103 | cgit_repo->defbranch = cgit_query_head; | ||
| 104 | } | 265 | } |
| 105 | 266 | ||
| 106 | if (!cgit_query_head) { | 267 | if (!ctx->qry.head) { |
| 107 | cgit_print_docstart(title, item); | 268 | cgit_print_http_headers(ctx); |
| 108 | cgit_print_pageheader(title, 0); | 269 | cgit_print_docstart(ctx); |
| 270 | cgit_print_pageheader(ctx); | ||
| 109 | cgit_print_error("Repository seems to be empty"); | 271 | cgit_print_error("Repository seems to be empty"); |
| 110 | cgit_print_docend(); | 272 | cgit_print_docend(); |
| 111 | return; | 273 | return 1; |
| 112 | } | 274 | } |
| 113 | 275 | ||
| 114 | if (get_sha1(cgit_query_head, sha1)) { | 276 | if (get_sha1(ctx->qry.head, sha1)) { |
| 115 | tmp = xstrdup(cgit_query_head); | 277 | tmp = xstrdup(ctx->qry.head); |
| 116 | cgit_query_head = cgit_repo->defbranch; | 278 | ctx->qry.head = ctx->repo->defbranch; |
| 117 | cgit_print_docstart(title, item); | 279 | cgit_print_http_headers(ctx); |
| 118 | cgit_print_pageheader(title, 0); | 280 | cgit_print_docstart(ctx); |
| 281 | cgit_print_pageheader(ctx); | ||
| 119 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 282 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
| 120 | cgit_print_docend(); | 283 | cgit_print_docend(); |
| 121 | return; | 284 | return 1; |
| 122 | } | 285 | } |
| 286 | return 0; | ||
| 287 | } | ||
| 123 | 288 | ||
| 124 | if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { | 289 | static void process_request(struct cgit_context *ctx) |
| 125 | cgit_print_snapshot(item, cgit_query_head, cgit_query_sha1, | 290 | { |
| 126 | cgit_repobasename(cgit_repo->url), | 291 | struct cgit_cmd *cmd; |
