-rw-r--r-- | cache.c | 22 | ||||
-rw-r--r-- | cgit.c | 74 | ||||
-rw-r--r-- | cgit.h | 15 | ||||
-rw-r--r-- | parsing.c | 44 | ||||
-rw-r--r-- | shared.c | 30 | ||||
-rw-r--r-- | ui-shared.c | 5 |
6 files changed, 139 insertions, 51 deletions
@@ -1,70 +1,78 @@ /* cache.c: cache management * * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) */ #include "cgit.h" const int NOLOCK = -1; char *cache_safe_filename(const char *unsafe) { - static char buf[PATH_MAX]; - char *s = buf; + static char buf[4][PATH_MAX]; + static int bufidx; + char *s; char c; + bufidx++; + bufidx &= 3; + s = buf[bufidx]; + while(unsafe && (c = *unsafe++) != 0) { - if (c == '/' || c == ' ' || c == '&' || c == '|' || + if (c == '/' || c == ' ' || c == '&' || c == '|' || c == '>' || c == '<' || c == '.') c = '_'; *s++ = c; } *s = '\0'; - return buf; + return buf[bufidx]; } int cache_exist(struct cacheitem *item) { if (stat(item->name, &item->st)) { item->st.st_mtime = 0; return 0; } return 1; } int cache_create_dirs() { char *path; path = fmt("%s", cgit_cache_root); if (mkdir(path, S_IRWXU) && errno!=EEXIST) return 0; - if (!cgit_query_repo) + if (!cgit_repo) return 0; - path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); + path = fmt("%s/%s", cgit_cache_root, + cache_safe_filename(cgit_repo->url)); + if (mkdir(path, S_IRWXU) && errno!=EEXIST) return 0; if (cgit_query_page) { - path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, + path = fmt("%s/%s/%s", cgit_cache_root, + cache_safe_filename(cgit_repo->url), cgit_query_page); if (mkdir(path, S_IRWXU) && errno!=EEXIST) return 0; } return 1; } int cache_refill_overdue(const char *lockfile) { struct stat st; if (stat(lockfile, &st)) return 0; else return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); } @@ -1,67 +1,53 @@ /* cgit.c: cgi for the git scm * * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) */ #include "cgit.h" const char cgit_version[] = CGIT_VERSION; -static struct repoinfo *cgit_get_repoinfo(char *url) -{ - int i; - struct repoinfo *repo; - - for (i=0; i<cgit_repolist.count; i++) { - repo = &cgit_repolist.repos[i]; - if (!strcmp(repo->url, url)) - return repo; - } - return NULL; -} - - static int cgit_prepare_cache(struct cacheitem *item) { - if (!cgit_query_repo) { - item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); - item->ttl = cgit_cache_root_ttl; - return 1; - } - cgit_repo = cgit_get_repoinfo(cgit_query_repo); - if (!cgit_repo) { + if (!cgit_repo && cgit_query_repo) { char *title = fmt("%s - %s", cgit_root_title, "Bad request"); cgit_print_docstart(title, item); cgit_print_pageheader(title, 0); cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo)); cgit_print_docend(); return 0; } - if (!cgit_query_page) { + if (!cgit_repo) { + item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); + item->ttl = cgit_cache_root_ttl; + return 1; + } + + if (!cgit_cmd) { item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, - cgit_repo->url)); + cache_safe_filename(cgit_repo->url))); item->ttl = cgit_cache_repo_ttl; } else { item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, - cgit_repo->url, cgit_query_page, + cache_safe_filename(cgit_repo->url), cgit_query_page, cache_safe_filename(cgit_querystring))); if (cgit_query_has_symref) item->ttl = cgit_cache_dynamic_ttl; else if (cgit_query_has_sha1) item->ttl = cgit_cache_static_ttl; else item->ttl = cgit_cache_repo_ttl; } return 1; } static void cgit_print_repo_page(struct cacheitem *item) { char *title; int show_search; @@ -69,94 +55,95 @@ static void cgit_print_repo_page(struct cacheitem *item) cgit_query_head = cgit_repo->defbranch; if (chdir(cgit_repo->path)) { title = fmt("%s - %s", cgit_root_title, "Bad request"); cgit_print_docstart(title, item); cgit_print_pageheader(title, 0); cgit_print_error(fmt("Unable to scan repository: %s", strerror(errno))); cgit_print_docend(); return; } title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); show_search = 0; setenv("GIT_DIR", cgit_repo->path, 1); - if (cgit_query_page) { - if (cgit_repo->snapshots && !strcmp(cgit_query_page, "snapshot")) { + if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { cgit_print_snapshot(item, cgit_query_sha1, "zip", cgit_repo->url, cgit_query_name); return; - } - if (!strcmp(cgit_query_page, "blob")) { - cgit_print_blob(item, cgit_query_sha1, cgit_query_path); - return; - } } - if (cgit_query_page && !strcmp(cgit_query_page, "log")) - show_search = 1; + if (cgit_cmd == CMD_BLOB) { + cgit_print_blob(item, cgit_query_sha1, cgit_query_path); + return; + } + show_search = (cgit_cmd == CMD_LOG); cgit_print_docstart(title, item); - - - if (!cgit_query_page) { + if (!cgit_cmd) { cgit_print_pageheader("summary", show_search); cgit_print_summary(); cgit_print_docend(); return; } cgit_print_pageheader(cgit_query_page, show_search); - if (!strcmp(cgit_query_page, "log")) { + switch(cgit_cmd) { + case CMD_LOG: cgit_print_log(cgit_query_head, cgit_query_ofs, cgit_max_commit_count, cgit_query_search, cgit_query_path); - } else if (!strcmp(cgit_query_page, "tree")) { + break; + case CMD_TREE: cgit_print_tree(cgit_query_head, cgit_query_sha1, cgit_query_path); - } else if (!strcmp(cgit_query_page, "commit")) { + break; + case CMD_COMMIT: cgit_print_commit(cgit_query_head); - } else if (!strcmp(cgit_query_page, "view")) { + break; + case CMD_VIEW: cgit_print_view(cgit_query_sha1, cgit_query_path); - } else if (!strcmp(cgit_query_page, "diff")) { + break; + case CMD_DIFF: cgit_print_diff(cgit_query_head, cgit_query_sha1, cgit_query_sha2, cgit_query_path); - } else { + break; + default: cgit_print_error("Invalid request"); } cgit_print_docend(); } static void cgit_fill_cache(struct cacheitem *item, int use_cache) { static char buf[PATH_MAX]; int stdout2; getcwd(buf, sizeof(buf)); item->st.st_mtime = time(NULL); 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)"); } - if (cgit_query_repo) + if (cgit_repo) cgit_print_repo_page(item); else cgit_print_repolist(item); 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"); } chdir(buf); } static void cgit_check_cache(struct cacheitem *item) { @@ -235,32 +222,33 @@ static void cgit_parse_args(int argc, const char **argv) cgit_query_ofs = atoi(argv[i]+6); } } } int main(int argc, const char **argv) { struct cacheitem item; htmlfd = STDOUT_FILENO; item.st.st_mtime = time(NULL); cgit_repolist.length = 0; cgit_repolist.count = 0; cgit_repolist.repos = NULL; cgit_read_config(CGIT_CONFIG, cgit_global_config_cb); + cgit_repo = NULL; if (getenv("SCRIPT_NAME")) cgit_script_name = xstrdup(getenv("SCRIPT_NAME")); if (getenv("QUERY_STRING")) cgit_querystring = xstrdup(getenv("QUERY_STRING")); cgit_parse_args(argc, argv); cgit_parse_query(cgit_querystring, cgit_querystring_cb); if (!cgit_prepare_cache(&item)) return 0; if (cgit_nocache) { cgit_fill_cache(&item, 0); } else { cgit_check_cache(&item); cgit_print_cache(&item); } return 0; } @@ -5,32 +5,43 @@ #include <git-compat-util.h> #include <cache.h> #include <grep.h> #include <object.h> #include <tree.h> #include <commit.h> #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> +/* + * The valid cgit repo-commands + */ +#define CMD_LOG 1 +#define CMD_COMMIT 2 +#define CMD_DIFF 3 +#define CMD_TREE 4 +#define CMD_VIEW 5 +#define CMD_BLOB 6 +#define CMD_SNAPSHOT 7 + typedef void (*configfn)(const char *name, const char *value); typedef void (*filepair_fn)(struct diff_filepair *pair); typedef void (*linediff_fn)(char *line, int len); struct cacheitem { char *name; struct stat st; int ttl; int fd; }; struct repoinfo { char *url; char *name; char *path; char *desc; @@ -58,32 +69,33 @@ struct commitinfo { unsigned long committer_date; char *subject; char *msg; }; struct taginfo { char *tagger; char *tagger_email; int tagger_date; char *msg; }; extern const char cgit_version[]; extern struct repolist cgit_repolist; extern struct repoinfo *cgit_repo; +extern int cgit_cmd; extern char *cgit_root_title; extern char *cgit_css; extern char *cgit_logo; extern char *cgit_index_header; extern char *cgit_logo_link; extern char *cgit_module_link; extern char *cgit_virtual_root; extern char *cgit_script_name; extern char *cgit_cache_root; extern int cgit_nocache; extern int cgit_snapshots; extern int cgit_enable_log_filecount; extern int cgit_enable_log_linecount; extern int cgit_max_lock_attempts; @@ -100,32 +112,34 @@ extern int cgit_max_commit_count; extern int cgit_query_has_symref; extern int cgit_query_has_sha1; extern char *cgit_querystring; extern char *cgit_query_repo; extern char *cgit_query_page; extern char *cgit_query_search; extern char *cgit_query_head; extern char *cgit_query_sha1; extern char *cgit_query_sha2; extern char *cgit_query_path; extern char *cgit_query_name; extern int cgit_query_ofs; extern int htmlfd; +extern int cgit_get_cmd_index(const char *cmd); +extern struct repoinfo *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 hextoint(char c); extern void *cgit_free_commitinfo(struct commitinfo *info); extern int cgit_diff_files(const unsigned char *old_sha1, const unsigned char *new_sha1, linediff_fn fn); extern void cgit_diff_tree(const unsigned char *old_sha1, @@ -138,32 +152,33 @@ extern char *fmt(const char *format,...); extern void html(const char *txt); extern void htmlf(const char *format,...); extern void html_txt(char *txt); extern void html_ntxt(int len, char *txt); extern void html_attr(char *txt); extern void html_hidden(char *name, char *value); extern void html_link_open(char *url, char *title, char *class); extern void html_link_close(void); extern void html_filemode(unsigned short mode); extern int html_include(const char *filename); extern int cgit_read_config(const char *filename, configfn fn); extern int cgit_parse_query(char *txt, configfn fn); extern struct commitinfo *cgit_parse_commit(struct commit *commit); extern struct taginfo *cgit_parse_tag(struct tag *tag); +extern void cgit_parse_url(const char *url); extern char *cache_safe_filename(const char *unsafe); extern int cache_lock(struct cacheitem *item); extern int cache_unlock(struct cacheitem *item); extern int cache_cancel_lock(struct cacheitem *item); extern int cache_exist(struct cacheitem *item); extern int cache_expired(struct cacheitem *item); extern char *cgit_repourl(const char *reponame); extern char *cgit_pageurl(const char *reponame, const char *pagename, const char *query); extern void cgit_print_error(char *msg); extern void cgit_print_date(unsigned long secs); extern void cgit_print_docstart(char *title, struct cacheitem *item); extern void cgit_print_docend(); @@ -119,32 +119,76 @@ int cgit_parse_query(char *txt, configfn fn) *t = ' '; } else if (c=='%') { t = convert_query_hexchar(t); } else if (c=='&') { *t = '\0'; (*fn)(txt, value); txt = t+1; value = NULL; } t++; } if (t!=txt) (*fn)(txt, value); return 0; } +/* + * url syntax: [repo ['/' cmd [ '/' path]]] + * repo: any valid repo url, may contain '/' + * cmd: log | commit | diff | tree | view | blob | snapshot + * path: any valid path, may contain '/' + * + */ +void cgit_parse_url(const char *url) +{ + char *cmd, *p; + + cgit_repo = NULL; + if (!url || url[0] == '\0') + return; + + cgit_repo = cgit_get_repoinfo(url); + if (cgit_repo) { + cgit_query_repo = cgit_repo->url; + return; + } + + cmd = strchr(url, '/'); + while (!cgit_repo && cmd) { + cmd[0] = '\0'; + cgit_repo = cgit_get_repoinfo(url); + if (cgit_repo == NULL) { + cmd[0] = '/'; + cmd = strchr(cmd + 1, '/'); + continue; + } + + cgit_query_repo = cgit_repo->url; + p = strchr(cmd + 1, '/'); + if (p) { + p[0] = '\0'; + if (p[1]) + cgit_query_path = xstrdup(p + 1); + } + cgit_cmd = cgit_get_cmd_index(cmd + 1); + cgit_query_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; } struct commitinfo *cgit_parse_commit(struct commit *commit) { struct commitinfo *ret; char *p = commit->buffer, *t = commit->buffer; ret = xmalloc(sizeof(*ret)); @@ -1,28 +1,29 @@ /* shared.c: global vars + some callback functions * * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) */ #include "cgit.h" struct repolist cgit_repolist; struct repoinfo *cgit_repo; +int cgit_cmd; char *cgit_root_title = "Git repository browser"; char *cgit_css = "/cgit.css"; char *cgit_logo = "/git-logo.png"; char *cgit_index_header = NULL; char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; char *cgit_module_link = "./?repo=%s&page=commit&id=%s"; char *cgit_virtual_root = NULL; char *cgit_script_name = CGIT_SCRIPT_NAME; char *cgit_cache_root = "/var/cache/cgit"; int cgit_nocache = 0; int cgit_snapshots = 0; int cgit_enable_log_filecount = 0; int cgit_enable_log_linecount = 0; int cgit_max_lock_attempts = 5; @@ -39,32 +40,44 @@ int cgit_max_commit_count = 50; int cgit_query_has_symref = 0; int cgit_query_has_sha1 = 0; char *cgit_querystring = NULL; char *cgit_query_repo = NULL; char *cgit_query_page = NULL; char *cgit_query_head = NULL; char *cgit_query_search = NULL; char *cgit_query_sha1 = NULL; char *cgit_query_sha2 = NULL; char *cgit_query_path = NULL; char *cgit_query_name = NULL; int cgit_query_ofs = 0; int htmlfd = 0; + +int cgit_get_cmd_index(const char *cmd) +{ + static char *cmds[] = {"log", "commit", "diff", "tree", "view", "blob", "snapshot", 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; } struct repoinfo *add_repo(const char *url) { @@ -81,32 +94,45 @@ struct repoinfo *add_repo(const char *url) } ret = &cgit_repolist.repos[cgit_repolist.count-1]; ret->url = xstrdup(url); ret->name = ret->url; ret->path = NULL; ret->desc = NULL; ret->owner = NULL; ret->defbranch = "master"; ret->snapshots = cgit_snapshots; ret->enable_log_filecount = cgit_enable_log_filecount; ret->enable_log_linecount = cgit_enable_log_linecount; ret->module_link = cgit_module_link; return ret; } +struct repoinfo *cgit_get_repoinfo(const char *url) +{ + int i; + struct repoinfo *repo; + + for (i=0; i<cgit_repolist.count; i++) { + repo = &cgit_repolist.repos[i]; + if (!strcmp(repo->url, url)) + return repo; + } + return NULL; +} + void cgit_global_config_cb(const char *name, const char *value) { if (!strcmp(name, "root-title")) cgit_root_title = xstrdup(value); else if (!strcmp(name, "css")) cgit_css = xstrdup(value); else if (!strcmp(name, "logo")) cgit_logo = xstrdup(value); else if (!strcmp(name, "index-header")) cgit_index_header = xstrdup(value); else if (!strcmp(name, "logo-link")) cgit_logo_link = xstrdup(value); else if (!strcmp(name, "module-link")) cgit_module_link = xstrdup(value); else if (!strcmp(name, "virtual-root")) cgit_virtual_root = xstrdup(value); @@ -149,34 +175,38 @@ void cgit_global_config_cb(const char *name, const char *value) else if (cgit_repo && !strcmp(name, "repo.snapshots")) cgit_repo->snapshots = cgit_snapshots * atoi(value); else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value); else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value); else if (cgit_repo && !strcmp(name, "repo.module-link")) cgit_repo->module_link= xstrdup(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")) { cgit_query_repo = xstrdup(value); + cgit_repo = cgit_get_repoinfo(value); } else if (!strcmp(name, "p")) { cgit_query_page = xstrdup(value); + cgit_cmd = cgit_get_cmd_index(value); + } else if (!strcmp(name, "url")) { + cgit_parse_url(value); } else if (!strcmp(name, "q")) { cgit_query_search = xstrdup(value); } else if (!strcmp(name, "h")) { cgit_query_head = xstrdup(value); cgit_query_has_symref = 1; } else if (!strcmp(name, "id")) { cgit_query_sha1 = xstrdup(value); cgit_query_has_sha1 = 1; } else if (!strcmp(name, "id2")) { cgit_query_sha2 = xstrdup(value); cgit_query_has_sha1 = 1; } else if (!strcmp(name, "ofs")) { cgit_query_ofs = atoi(value); } else if (!strcmp(name, "path")) { cgit_query_path = xstrdup(value); } else if (!strcmp(name, "name")) { diff --git a/ui-shared.c b/ui-shared.c index 6211056..c7fbc5e 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -55,33 +55,36 @@ char *cgit_repourl(const char *reponame) } else { return fmt("?r=%s", reponame); } } char *cgit_pageurl(const char *reponame, const char *pagename, const char *query) { if (cgit_virtual_root) { if (query) return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame, pagename, query); else return fmt("%s/%s/%s/", cgit_virtual_root, reponame, pagename); } else { - return fmt("?r=%s&p=%s&%s", reponame, pagename, query); + if (query) + return fmt("?r=%s&p=%s&%s", reponame, pagename, query); + else + return fmt("?r=%s&p=%s", reponame, pagename); } } char *cgit_currurl() { if (!cgit_virtual_root) return cgit_script_name; else if (cgit_query_page) return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page); else if (cgit_query_repo) return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo); else return fmt("%s/", cgit_virtual_root); } |