diff --git a/.mailmap b/.mailmap deleted file mode 100644 index ca6b57e..0000000 --- a/.mailmap +++ /dev/null @@ -1,10 +0,0 @@ -Florian Pritz -Harley Laue -John Keeping -Lars Hjemli -Lars Hjemli -Lars Hjemli -Lars Hjemli -Lukas Fleischer -Lukas Fleischer -Stefan Bühler diff --git a/AUTHORS b/AUTHORS deleted file mode 100644 index 031de33..0000000 --- a/AUTHORS +++ /dev/null @@ -1,13 +0,0 @@ -Maintainer: - Jason A. Donenfeld - -Contributors: - Jason A. Donenfeld - Lukas Fleischer - Johan Herland - Lars Hjemli - Ferry Huberts - John Keeping - -Previous Maintainer: - Lars Hjemli diff --git a/Makefile b/Makefile index 2dc92df..d86d131 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,12 @@ all:: -CGIT_VERSION = v0.10 +CGIT_VERSION = v0.9.2 CGIT_SCRIPT_NAME = cgit.cgi CGIT_SCRIPT_PATH = /var/www/htdocs/cgit CGIT_DATA_PATH = $(CGIT_SCRIPT_PATH) CGIT_CONFIG = /etc/cgitrc CACHE_ROOT = /var/cache/cgit -prefix = /usr/local +prefix = /usr libdir = $(prefix)/lib filterdir = $(libdir)/cgit/filters docdir = $(prefix)/share/doc/cgit @@ -14,7 +14,7 @@ htmldir = $(docdir) pdfdir = $(docdir) mandir = $(prefix)/share/man SHA1_HEADER = -GIT_VER = 1.8.5 +GIT_VER = 1.8.3 GIT_URL = https://git-core.googlecode.com/files/git-$(GIT_VER).tar.gz INSTALL = install COPYTREE = cp -r @@ -77,8 +77,6 @@ install: all $(INSTALL) -m 0755 -d $(DESTDIR)$(CGIT_DATA_PATH) $(INSTALL) -m 0644 cgit.css $(DESTDIR)$(CGIT_DATA_PATH)/cgit.css $(INSTALL) -m 0644 cgit.png $(DESTDIR)$(CGIT_DATA_PATH)/cgit.png - $(INSTALL) -m 0644 favicon.ico $(DESTDIR)$(CGIT_DATA_PATH)/favicon.ico - $(INSTALL) -m 0644 robots.txt $(DESTDIR)$(CGIT_DATA_PATH)/robots.txt $(INSTALL) -m 0755 -d $(DESTDIR)$(filterdir) $(COPYTREE) filters/* $(DESTDIR)$(filterdir) @@ -100,7 +98,6 @@ uninstall: rm -f $(DESTDIR)$(CGIT_SCRIPT_PATH)/$(CGIT_SCRIPT_NAME) rm -f $(DESTDIR)$(CGIT_DATA_PATH)/cgit.css rm -f $(DESTDIR)$(CGIT_DATA_PATH)/cgit.png - rm -f $(DESTDIR)$(CGIT_DATA_PATH)/favicon.ico uninstall-doc: uninstall-man uninstall-html uninstall-pdf diff --git a/README b/README index 7b72842..8e4ed53 100644 --- a/README +++ b/README @@ -1,102 +1,86 @@ -cgit - CGI for Git -================== -This is an attempt to create a fast web interface for the Git SCM, using a -built-in cache to decrease server I/O pressure. + cgit - cgi for git + + +This is an attempt to create a fast web interface for the git scm, using a +builtin cache to decrease server io-pressure. + Installation ------------- -Building cgit involves building a proper version of Git. How to do this +Building cgit involves building a proper version of git. How to do this depends on how you obtained the cgit sources: a) If you're working in a cloned cgit repository, you first need to -initialize and update the Git submodule: +initialize and update the git submodule: - $ git submodule init # register the Git submodule in .git/config - $ $EDITOR .git/config # if you want to specify a different url for git - $ git submodule update # clone/fetch and checkout correct git version + $ git submodule init # register the git submodule in .git/config + $ $EDITOR .git/config # if you want to specify a different url for git + $ git submodule update # clone/fetch and checkout correct git version b) If you're building from a cgit tarball, you can download a proper git version like this: - $ make get-git + $ make get-git + When either a) or b) has been performed, you can build and install cgit like this: - $ make - $ sudo make install + $ make + $ sudo make install -This will install `cgit.cgi` and `cgit.css` into `/var/www/htdocs/cgit`. You -can configure this location (and a few other things) by providing a `cgit.conf` +This will install cgit.cgi and cgit.css into "/var/www/htdocs/cgit". You can +configure this location (and a few other things) by providing a "cgit.conf" file (see the Makefile for details). -If you'd like to compile without Lua support, you may use: - - $ make NO_LUA=1 - -And if you'd like to specify a Lua implementation, you may use: - - $ make LUA_IMPLEMENTATION=JIT -for using the LuaJIT project. Or: +Dependencies: + -git 1.7.4 + -zip lib + -crypto lib + -openssl lib - $ make LUA_IMPLEMENTATION=VANILLA - -for the mainline Lua project. If you specify neither implementation, it will -be auto-detected, preferring LuaJIT if both are present. - - -Dependencies ------------- - -* libzip -* libcrypto (OpenSSL) -* libssl (OpenSSL) -* optional: luajit or lua Apache configuration --------------------- -A new `Directory` section must probably be added for cgit, possibly something +A new Directory-section must probably be added for cgit, possibly something like this: - - AllowOverride None - Options +ExecCGI - Order allow,deny - Allow from all - + + AllowOverride None + Options +ExecCGI + Order allow,deny + Allow from all + Runtime configuration ---------------------- -The file `/etc/cgitrc` is read by cgit before handling a request. In addition +The file /etc/cgitrc is read by cgit before handling a request. In addition to runtime parameters, this file may also contain a list of repositories -displayed by cgit (see `cgitrc.5.txt` for further details). +displayed by cgit (see cgitrc.5.txt for further details). + The cache ---------- -When cgit is invoked it looks for a cache file matching the request and -returns it to the client. If no such cache file exists (or if it has expired), -the content for the request is written into the proper cache file before the +When cgit is invoked it looks for a cachefile matching the request and +returns it to the client. If no such cachefile exist (or if it has expired), +the content for the request is written into the proper cachefile before the file is returned. -If the cache file has expired but cgit is unable to obtain a lock for it, the -stale cache file is returned to the client. This is done to favour page +If the cachefile has expired but cgit is unable to obtain a lock for it, the +stale cachefile is returned to the client. This is done to favour page throughput over page freshness. The generated content contains the complete response to the client, including -the HTTP headers `Modified` and `Expires`. +the http-headers "Modified" and "Expires". + Online presence ---------------- * The cgit homepage is hosted by cgit at -* Patches, bug reports, discussions and support should go to the cgit +* Patches, bugreports, discussions and support should go to the cgit mailing list: . To sign up, visit diff --git a/cache.c b/cache.c index fcd461f..aa870e3 100644 --- a/cache.c +++ b/cache.c @@ -1,6 +1,6 @@ /* cache.c: cache management * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -24,6 +24,7 @@ struct cache_slot { int keylen; int ttl; cache_fill_fn fn; + void *cbdata; int cache_fd; int lock_fd; const char *cache_name; @@ -186,7 +187,7 @@ static int fill_slot(struct cache_slot *slot) return errno; /* Generate cache content */ - slot->fn(); + slot->fn(slot->cbdata); /* Restore stdout */ if (dup2(tmp, STDOUT_FILENO) == -1) @@ -276,7 +277,7 @@ static int process_slot(struct cache_slot *slot) if ((err = lock_slot(slot)) != 0) { cache_log("[cgit] Unable to lock slot %s: %s (%d)\n", slot->lock_name, strerror(err), err); - slot->fn(); + slot->fn(slot->cbdata); return 0; } @@ -285,7 +286,7 @@ static int process_slot(struct cache_slot *slot) slot->lock_name, strerror(err), err); unlock_slot(slot, 0); close_lock(slot); - slot->fn(); + slot->fn(slot->cbdata); return 0; } // We've got a valid cache slot in the lock file, which @@ -309,7 +310,7 @@ static int process_slot(struct cache_slot *slot) /* Print cached content to stdout, generate the content if necessary. */ int cache_process(int size, const char *path, const char *key, int ttl, - cache_fill_fn fn) + cache_fill_fn fn, void *cbdata) { unsigned long hash; int i; @@ -320,14 +321,14 @@ int cache_process(int size, const char *path, const char *key, int ttl, /* If the cache is disabled, just generate the content */ if (size <= 0) { - fn(); + fn(cbdata); return 0; } /* Verify input, calculate filenames */ if (!path) { cache_log("[cgit] Cache path not specified, caching is disabled\n"); - fn(); + fn(cbdata); return 0; } if (!key) @@ -342,6 +343,7 @@ int cache_process(int size, const char *path, const char *key, int ttl, strbuf_addbuf(&lockname, &filename); strbuf_addstr(&lockname, ".lock"); slot.fn = fn; + slot.cbdata = cbdata; slot.ttl = ttl; slot.cache_name = filename.buf; slot.lock_name = lockname.buf; @@ -374,7 +376,7 @@ int cache_ls(const char *path) DIR *dir; struct dirent *ent; int err = 0; - struct cache_slot slot = { 0 }; + struct cache_slot slot; struct strbuf fullname = STRBUF_INIT; size_t prefixlen; diff --git a/cache.h b/cache.h index 9392836..5cfdb4f 100644 --- a/cache.h +++ b/cache.h @@ -6,7 +6,7 @@ #ifndef CGIT_CACHE_H #define CGIT_CACHE_H -typedef void (*cache_fill_fn)(void); +typedef void (*cache_fill_fn)(void *cbdata); /* Print cached content to stdout, generate the content if necessary. @@ -17,12 +17,13 @@ typedef void (*cache_fill_fn)(void); * key the key used to lookup cache files * ttl max cache time in seconds for this key * fn content generator function for this key + * cbdata user-supplied data to the content generator function * * Return value * 0 indicates success, everyting else is an error */ extern int cache_process(int size, const char *path, const char *key, int ttl, - cache_fill_fn fn); + cache_fill_fn fn, void *cbdata); /* List info about all cache entries on stdout */ diff --git a/cgit.c b/cgit.c index 09fce0c..f0a9acf 100644 --- a/cgit.c +++ b/cgit.c @@ -1,6 +1,7 @@ /* cgit.c: cgi for the git scm * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006 Lars Hjemli + * Copyright (C) 2010-2013 Jason A. Donenfeld * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -27,6 +28,36 @@ static void add_mimetype(const char *name, const char *value) item->util = xstrdup(value); } +static struct cgit_filter *new_filter(const char *cmd, filter_type filtertype) +{ + struct cgit_filter *f; + int args_size = 0; + int extra_args; + + if (!cmd || !cmd[0]) + return NULL; + + switch (filtertype) { + case SOURCE: + case ABOUT: + extra_args = 1; + break; + + case COMMIT: + default: + extra_args = 0; + break; + } + + f = xmalloc(sizeof(struct cgit_filter)); + f->cmd = xstrdup(cmd); + args_size = (2 + extra_args) * sizeof(char *); + f->argv = xmalloc(args_size); + memset(f->argv, 0, args_size); + f->argv[0] = f->cmd; + return f; +} + static void process_cached_repolist(const char *path); static void repo_config(struct cgit_repo *repo, const char *name, const char *value) @@ -84,13 +115,11 @@ static void repo_config(struct cgit_repo *repo, const char *name, const char *va repo->logo_link = xstrdup(value); else if (ctx.cfg.enable_filter_overrides) { if (!strcmp(name, "about-filter")) - repo->about_filter = cgit_new_filter(value, ABOUT); + repo->about_filter = new_filter(value, ABOUT); else if (!strcmp(name, "commit-filter")) - repo->commit_filter = cgit_new_filter(value, COMMIT); + repo->commit_filter = new_filter(value, COMMIT); else if (!strcmp(name, "source-filter")) - repo->source_filter = cgit_new_filter(value, SOURCE); - else if (!strcmp(name, "email-filter")) - repo->email_filter = cgit_new_filter(value, EMAIL); + repo->source_filter = new_filter(value, SOURCE); } } @@ -182,18 +211,12 @@ static void config_cb(const char *name, const char *value) ctx.cfg.cache_static_ttl = atoi(value); else if (!strcmp(name, "cache-dynamic-ttl")) ctx.cfg.cache_dynamic_ttl = atoi(value); - else if (!strcmp(name, "cache-about-ttl")) - ctx.cfg.cache_about_ttl = atoi(value); else if (!strcmp(name, "case-sensitive-sort")) ctx.cfg.case_sensitive_sort = atoi(value); else if (!strcmp(name, "about-filter")) - ctx.cfg.about_filter = cgit_new_filter(value, ABOUT); + ctx.cfg.about_filter = new_filter(value, ABOUT); else if (!strcmp(name, "commit-filter")) - ctx.cfg.commit_filter = cgit_new_filter(value, COMMIT); - else if (!strcmp(name, "email-filter")) - ctx.cfg.email_filter = cgit_new_filter(value, EMAIL); - else if (!strcmp(name, "auth-filter")) - ctx.cfg.auth_filter = cgit_new_filter(value, AUTH); + ctx.cfg.commit_filter = new_filter(value, COMMIT); else if (!strcmp(name, "embedded")) ctx.cfg.embedded = atoi(value); else if (!strcmp(name, "max-atom-items")) @@ -227,7 +250,7 @@ static void config_cb(const char *name, const char *value) else if (!strcmp(name, "section-sort")) ctx.cfg.section_sort = atoi(value); else if (!strcmp(name, "source-filter")) - ctx.cfg.source_filter = cgit_new_filter(value, SOURCE); + ctx.cfg.source_filter = new_filter(value, SOURCE); else if (!strcmp(name, "summary-log")) ctx.cfg.summary_log = atoi(value); else if (!strcmp(name, "summary-branches")) @@ -322,82 +345,76 @@ static void querystring_cb(const char *name, const char *value) } } -static void prepare_context(void) +static void prepare_context(struct cgit_context *ctx) { - memset(&ctx, 0, sizeof(ctx)); - ctx.cfg.agefile = "info/web/last-modified"; - ctx.cfg.nocache = 0; - ctx.cfg.cache_size = 0; - ctx.cfg.cache_max_create_time = 5; - ctx.cfg.cache_root = CGIT_CACHE_ROOT; - ctx.cfg.cache_about_ttl = 15; - ctx.cfg.cache_repo_ttl = 5; - ctx.cfg.cache_root_ttl = 5; - ctx.cfg.cache_scanrc_ttl = 15; - ctx.cfg.cache_dynamic_ttl = 5; - ctx.cfg.cache_static_ttl = -1; - ctx.cfg.case_sensitive_sort = 1; - ctx.cfg.branch_sort = 0; - ctx.cfg.commit_sort = 0; - ctx.cfg.css = "/cgit.css"; - ctx.cfg.logo = "/cgit.png"; - ctx.cfg.favicon = "/favicon.ico"; - ctx.cfg.local_time = 0; - ctx.cfg.enable_http_clone = 1; - ctx.cfg.enable_index_owner = 1; - ctx.cfg.enable_tree_linenumbers = 1; - ctx.cfg.enable_git_config = 0; - ctx.cfg.max_repo_count = 50; - ctx.cfg.max_commit_count = 50; - ctx.cfg.max_lock_attempts = 5; - ctx.cfg.max_msg_len = 80; - ctx.cfg.max_repodesc_len = 80; - ctx.cfg.max_blob_size = 0; - ctx.cfg.max_stats = 0; - ctx.cfg.project_list = NULL; - ctx.cfg.renamelimit = -1; - ctx.cfg.remove_suffix = 0; - ctx.cfg.robots = "index, nofollow"; - ctx.cfg.root_title = "Git repository browser"; - ctx.cfg.root_desc = "a fast webinterface for the git dscm"; - ctx.cfg.scan_hidden_path = 0; - ctx.cfg.script_name = CGIT_SCRIPT_NAME; - ctx.cfg.section = ""; - ctx.cfg.repository_sort = "name"; - ctx.cfg.section_sort = 1; - ctx.cfg.summary_branches = 10; - ctx.cfg.summary_log = 10; - ctx.cfg.summary_tags = 10; - ctx.cfg.max_atom_items = 10; - ctx.cfg.ssdiff = 0; - ctx.env.cgit_config = getenv("CGIT_CONFIG"); - ctx.env.http_host = getenv("HTTP_HOST"); - ctx.env.https = getenv("HTTPS"); - ctx.env.no_http = getenv("NO_HTTP"); - ctx.env.path_info = getenv("PATH_INFO"); - ctx.env.query_string = getenv("QUERY_STRING"); - ctx.env.request_method = getenv("REQUEST_METHOD"); - ctx.env.script_name = getenv("SCRIPT_NAME"); - ctx.env.server_name = getenv("SERVER_NAME"); - ctx.env.server_port = getenv("SERVER_PORT"); - ctx.env.http_cookie = getenv("HTTP_COOKIE"); - ctx.env.http_referer = getenv("HTTP_REFERER"); - ctx.env.content_length = getenv("CONTENT_LENGTH") ? strtoul(getenv("CONTENT_LENGTH"), NULL, 10) : 0; - ctx.env.authenticated = 0; - ctx.page.mimetype = "text/html"; - ctx.page.charset = PAGE_ENCODING; - ctx.page.filename = NULL; - ctx.page.size = 0; - ctx.page.modified = time(NULL); - ctx.page.expires = ctx.page.modified; - ctx.page.etag = NULL; - memset(&ctx.cfg.mimetypes, 0, sizeof(struct string_list)); - if (ctx.env.script_name) - ctx.cfg.script_name = xstrdup(ctx.env.script_name); - if (ctx.env.query_string) - ctx.qry.raw = xstrdup(ctx.env.query_string); - if (!ctx.env.cgit_config) - ctx.env.cgit_config = CGIT_CONFIG; + memset(ctx, 0, sizeof(*ctx)); + ctx->cfg.agefile = "info/web/last-modified"; + ctx->cfg.nocache = 0; + ctx->cfg.cache_size = 0; + ctx->cfg.cache_dynamic_ttl = 5; + ctx->cfg.cache_max_create_time = 5; + ctx->cfg.cache_repo_ttl = 5; + ctx->cfg.cache_root = CGIT_CACHE_ROOT; + ctx->cfg.cache_root_ttl = 5; + ctx->cfg.cache_scanrc_ttl = 15; + ctx->cfg.cache_static_ttl = -1; + ctx->cfg.case_sensitive_sort = 1; + ctx->cfg.branch_sort = 0; + ctx->cfg.commit_sort = 0; + ctx->cfg.css = "/cgit.css"; + ctx->cfg.logo = "/cgit.png"; + ctx->cfg.local_time = 0; + ctx->cfg.enable_http_clone = 1; + ctx->cfg.enable_index_owner = 1; + ctx->cfg.enable_tree_linenumbers = 1; + ctx->cfg.enable_git_config = 0; + ctx->cfg.max_repo_count = 50; + ctx->cfg.max_commit_count = 50; + ctx->cfg.max_lock_attempts = 5; + ctx->cfg.max_msg_len = 80; + ctx->cfg.max_repodesc_len = 80; + ctx->cfg.max_blob_size = 0; + ctx->cfg.max_stats = 0; + ctx->cfg.project_list = NULL; + ctx->cfg.renamelimit = -1; + ctx->cfg.remove_suffix = 0; + ctx->cfg.robots = "index, nofollow"; + ctx->cfg.root_title = "Git repository browser"; + ctx->cfg.root_desc = "a fast webinterface for the git dscm"; + ctx->cfg.scan_hidden_path = 0; + ctx->cfg.script_name = CGIT_SCRIPT_NAME; + ctx->cfg.section = ""; + ctx->cfg.repository_sort = "name"; + ctx->cfg.section_sort = 1; + ctx->cfg.summary_branches = 10; + ctx->cfg.summary_log = 10; + ctx->cfg.summary_tags = 10; + ctx->cfg.max_atom_items = 10; + ctx->cfg.ssdiff = 0; + ctx->env.cgit_config = getenv("CGIT_CONFIG"); + ctx->env.http_host = getenv("HTTP_HOST"); + ctx->env.https = getenv("HTTPS"); + ctx->env.no_http = getenv("NO_HTTP"); + ctx->env.path_info = getenv("PATH_INFO"); + ctx->env.query_string = getenv("QUERY_STRING"); + ctx->env.request_method = getenv("REQUEST_METHOD"); + ctx->env.script_name = getenv("SCRIPT_NAME"); + ctx->env.server_name = getenv("SERVER_NAME"); + ctx->env.server_port = getenv("SERVER_PORT"); + ctx->page.mimetype = "text/html"; + ctx->page.charset = PAGE_ENCODING; + ctx->page.filename = NULL; + ctx->page.size = 0; + ctx->page.modified = time(NULL); + ctx->page.expires = ctx->page.modified; + ctx->page.etag = NULL; + memset(&ctx->cfg.mimetypes, 0, sizeof(struct string_list)); + if (ctx->env.script_name) + ctx->cfg.script_name = xstrdup(ctx->env.script_name); + if (ctx->env.query_string) + ctx->qry.raw = xstrdup(ctx->env.query_string); + if (!ctx->env.cgit_config) + ctx->env.cgit_config = CGIT_CONFIG; } struct refmatch { @@ -527,14 +544,14 @@ static void choose_readme(struct cgit_repo *repo) string_list_append(&repo->readme, filename)->util = ref; } -static int prepare_repo_cmd(void) +static int prepare_repo_cmd(struct cgit_context *ctx) { unsigned char sha1[20]; int nongit = 0; int rc; /* The path to the git repository. */ - setenv("GIT_DIR", ctx.repo->path, 1); + setenv("GIT_DIR", ctx->repo->path, 1); /* Do not look in /etc/ for gitconfig and gitattributes. */ setenv("GIT_CONFIG_NOSYSTEM", "1", 1); @@ -549,180 +566,104 @@ static int prepare_repo_cmd(void) init_display_notes(NULL); if (nongit) { - const char *name = ctx.repo->name; + const char *name = ctx->repo->name; rc = errno; - ctx.page.title = fmtalloc("%s - %s", ctx.cfg.root_title, + ctx->page.title = fmtalloc("%s - %s", ctx->cfg.root_title, "config error"); - ctx.repo = NULL; - cgit_print_http_headers(); - cgit_print_docstart(); - cgit_print_pageheader(); + ctx->repo = NULL; + cgit_print_http_headers(ctx); + cgit_print_docstart(ctx); + cgit_print_pageheader(ctx); cgit_print_error("Failed to open %s: %s", name, rc ? strerror(rc) : "Not a valid git repository"); cgit_print_docend(); return 1; } - ctx.page.title = fmtalloc("%s - %s", ctx.repo->name, ctx.repo->desc); + ctx->page.title = fmtalloc("%s - %s", ctx->repo->name, ctx->repo->desc); - if (!ctx.repo->defbranch) - ctx.repo->defbranch = guess_defbranch(); + if (!ctx->repo->defbranch) + ctx->repo->defbranch = guess_defbranch(); - if (!ctx.qry.head) { - ctx.qry.nohead = 1; - ctx.qry.head = find_default_branch(ctx.repo); + if (!ctx->qry.head) { + ctx->qry.nohead = 1; + ctx->qry.head = find_default_branch(ctx->repo); } - if (!ctx.qry.head) { - cgit_print_http_headers(); - cgit_print_docstart(); - cgit_print_pageheader(); + 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 1; } - if (get_sha1(ctx.qry.head, sha1)) { - char *tmp = xstrdup(ctx.qry.head); - ctx.qry.head = ctx.repo->defbranch; - ctx.page.status = 404; - ctx.page.statusmsg = "Not found"; - cgit_print_http_headers(); - cgit_print_docstart(); - cgit_print_pageheader(); + if (get_sha1(ctx->qry.head, sha1)) { + char *tmp = xstrdup(ctx->qry.head); + ctx->qry.head = ctx->repo->defbranch; + ctx->page.status = 404; + ctx->page.statusmsg = "Not found"; + cgit_print_http_headers(ctx); + cgit_print_docstart(ctx); + cgit_print_pageheader(ctx); cgit_print_error("Invalid branch: %s", tmp); cgit_print_docend(); - free(tmp); return 1; } - sort_string_list(&ctx.repo->submodules); - cgit_prepare_repo_env(ctx.repo); - choose_readme(ctx.repo); + sort_string_list(&ctx->repo->submodules); + cgit_prepare_repo_env(ctx->repo); + choose_readme(ctx->repo); return 0; } -static inline void open_auth_filter(const char *function) -{ - cgit_open_filter(ctx.cfg.auth_filter, function, - ctx.env.http_cookie ? ctx.env.http_cookie : "", - ctx.env.request_method ? ctx.env.request_method : "", - ctx.env.query_string ? ctx.env.query_string : "", - ctx.env.http_referer ? ctx.env.http_referer : "", - ctx.env.path_info ? ctx.env.path_info : "", - ctx.env.http_host ? ctx.env.http_host : "", - ctx.env.https ? ctx.env.https : "", - ctx.qry.repo ? ctx.qry.repo : "", - ctx.qry.page ? ctx.qry.page : "", - ctx.qry.url ? ctx.qry.url : "", - cgit_loginurl()); -} - -/* We intentionally keep this rather small, instead of looping and - * feeding it to the filter a couple bytes at a time. This way, the - * filter itself does not need to handle any denial of service or - * buffer bloat issues. If this winds up being too small, people - * will complain on the mailing list, and we'll increase it as needed. */ -#define MAX_AUTHENTICATION_POST_BYTES 4096 -/* The filter is expected to spit out "Status: " and all headers. */ -static inline void authenticate_post(void) -{ - char buffer[MAX_AUTHENTICATION_POST_BYTES]; - int len; - - open_auth_filter("authenticate-post"); - len = ctx.env.content_length; - if (len > MAX_AUTHENTICATION_POST_BYTES) - len = MAX_AUTHENTICATION_POST_BYTES; - if (read(STDIN_FILENO, buffer, len) < 0) - die_errno("Could not read POST from stdin"); - if (write(STDOUT_FILENO, buffer, len) < 0) - die_errno("Could not write POST to stdout"); - cgit_close_filter(ctx.cfg.auth_filter); - exit(0); -} - -static inline void authenticate_cookie(void) -{ - /* If we don't have an auth_filter, consider all cookies valid, and thus return early. */ - if (!ctx.cfg.auth_filter) { - ctx.env.authenticated = 1; - return; - } - - /* If we're having something POST'd to /login, we're authenticating POST, - * instead of the cookie, so call authenticate_post and bail out early. - * This pattern here should match /?p=login with POST. */ - if (ctx.env.request_method && ctx.qry.page && !ctx.repo && \ - !strcmp(ctx.env.request_method, "POST") && !strcmp(ctx.qry.page, "login")) { - authenticate_post(); - return; - } - - /* If we've made it this far, we're authenticating the cookie for real, so do that. */ - open_auth_filter("authenticate-cookie"); - ctx.env.authenticated = cgit_close_filter(ctx.cfg.auth_filter); -} - -static void process_request(void) +static void process_request(void *cbdata) { + struct cgit_context *ctx = cbdata; struct cgit_cmd *cmd; - /* If we're not yet authenticated, no matter what page we're on, - * display the authentication body from the auth_filter. This should - * never be cached. */ - if (!ctx.env.authenticated) { - ctx.page.title = "Authentication Required"; - cgit_print_http_headers(); - cgit_print_docstart(); - cgit_print_pageheader(); - open_auth_filter("body"); - cgit_close_filter(ctx.cfg.auth_filter); - cgit_print_docend(); - return; - } - - cmd = cgit_get_cmd(); + cmd = cgit_get_cmd(ctx); if (!cmd) { - ctx.page.title = "cgit error"; - ctx.page.status = 404; - ctx.page.statusmsg = "Not found"; - cgit_print_http_headers(); - cgit_print_docstart(); - cgit_print_pageheader(); + ctx->page.title = "cgit error"; + ctx->page.status = 404; + ctx->page.statusmsg = "Not found"; + cgit_print_http_headers(ctx); + cgit_print_docstart(ctx); + cgit_print_pageheader(ctx); cgit_print_error("Invalid request"); cgit_print_docend(); return; } - if (!ctx.cfg.enable_http_clone && cmd->is_clone) { + if (!ctx->cfg.enable_http_clone && cmd->is_clone) { html_status(404, "Not found", 0); return; } - /* If cmd->want_vpath is set, assume ctx.qry.path contains a "virtual" - * in-project path limit to be made available at ctx.qry.vpath. - * Otherwise, no path limit is in effect (ctx.qry.vpath = NULL). + /* If cmd->want_vpath is set, assume ctx->qry.path contains a "virtual" + * in-project path limit to be made available at ctx->qry.vpath. + * Otherwise, no path limit is in effect (ctx->qry.vpath = NULL). */ - ctx.qry.vpath = cmd->want_vpath ? ctx.qry.path : NULL; + ctx->qry.vpath = cmd->want_vpath ? ctx->qry.path : NULL; - if (cmd->want_repo && !ctx.repo) { - cgit_print_http_headers(); - cgit_print_docstart(); - cgit_print_pageheader(); + if (cmd->want_repo && !ctx->repo) { + cgit_print_http_headers(ctx); + cgit_print_docstart(ctx); + cgit_print_pageheader(ctx); cgit_print_error("No repository selected"); cgit_print_docend(); return; } - if (ctx.repo && prepare_repo_cmd()) + if (ctx->repo && prepare_repo_cmd(ctx)) return; if (cmd->want_layout) { - cgit_print_http_headers(); - cgit_print_docstart(); - cgit_print_pageheader(); + cgit_print_http_headers(ctx); + cgit_print_docstart(ctx); + cgit_print_pageheader(ctx); } - cmd->fn(); + cmd->fn(ctx); if (cmd->want_layout) cgit_print_docend(); @@ -792,13 +733,11 @@ static void print_repo(FILE *f, struct cgit_repo *repo) fprintf(f, "repo.enable-log-linecount=%d\n", repo->enable_log_linecount); if (repo->about_filter && repo->about_filter != ctx.cfg.about_filter) - cgit_fprintf_filter(repo->about_filter, f, "repo.about-filter="); + fprintf(f, "repo.about-filter=%s\n", repo->about_filter->cmd); if (repo->commit_filter && repo->commit_filter != ctx.cfg.commit_filter) - cgit_fprintf_filter(repo->commit_filter, f, "repo.commit-filter="); + fprintf(f, "repo.commit-filter=%s\n", repo->commit_filter->cmd); if (repo->source_filter && repo->source_filter != ctx.cfg.source_filter) - cgit_fprintf_filter(repo->source_filter, f, "repo.source-filter="); - if (repo->email_filter && repo->email_filter != ctx.cfg.email_filter) - cgit_fprintf_filter(repo->email_filter, f, "repo.email-filter="); + fprintf(f, "repo.source-filter=%s\n", repo->source_filter->cmd); if (repo->snapshots != ctx.cfg.snapshots) { char *tmp = build_snapshot_setting(repo->snapshots); fprintf(f, "repo.snapshots=%s\n", tmp ? tmp : ""); @@ -921,38 +860,45 @@ static void cgit_parse_args(int argc, const char **argv) int scan = 0; for (i = 1; i < argc; i++) { - if (!prefixcmp(argv[i], "--cache=")) { + if (!strncmp(argv[i], "--cache=", 8)) { ctx.cfg.cache_root = xstrdup(argv[i] + 8); - } else if (!strcmp(argv[i], "--nocache")) { + } + if (!strcmp(argv[i], "--nocache")) { ctx.cfg.nocache = 1; - } else if (!strcmp(argv[i], "--nohttp")) { + } + if (!strcmp(argv[i], "--nohttp")) { ctx.env.no_http = "1"; - } else if (!prefixcmp(argv[i], "--query=")) { + } + if (!strncmp(argv[i], "--query=", 8)) { ctx.qry.raw = xstrdup(argv[i] + 8); - } else if (!prefixcmp(argv[i], "--repo=")) { + } + if (!strncmp(argv[i], "--repo=", 7)) { ctx.qry.repo = xstrdup(argv[i] + 7); - } else if (!prefixcmp(argv[i], "--page=")) { + } + if (!strncmp(argv[i], "--page=", 7)) { ctx.qry.page = xstrdup(argv[i] + 7); - } else if (!prefixcmp(argv[i], "--head=")) { + } + if (!strncmp(argv[i], "--head=", 7)) { ctx.qry.head = xstrdup(argv[i] + 7); ctx.qry.has_symref = 1; - } else if (!prefixcmp(argv[i], "--sha1=")) { + } + if (!strncmp(argv[i], "--sha1=", 7)) { ctx.qry.sha1 = xstrdup(argv[i] + 7); ctx.qry.has_sha1 = 1; - } else if (!prefixcmp(argv[i], "--ofs=")) { + } + if (!strncmp(argv[i], "--ofs=", 6)) { ctx.qry.ofs = atoi(argv[i] + 6); - } else if (!prefixcmp(argv[i], "--scan-tree=") || - !prefixcmp(argv[i], "--scan-path=")) { - /* - * HACK: The global snapshot bit mask defines the set - * of allowed snapshot formats, but the config file - * hasn't been parsed yet so the mask is currently 0. - * By setting all bits high before scanning we make - * sure that any in-repo cgitrc snapshot setting is - * respected by scan_tree(). - * - * NOTE: We assume that there aren't more than 8 - * different snapshot formats supported by cgit... + } + if (!strncmp(argv[i], "--scan-tree=", 12) || + !strncmp(argv[i], "--scan-path=", 12)) { + /* HACK: the global snapshot bitmask defines the + * set of allowed snapshot formats, but the config + * file hasn't been parsed yet so the mask is + * currently 0. By setting all bits high before + * scanning we make sure that any in-repo cgitrc + * snapshot setting is respected by scan_tree(). + * BTW: we assume that there'll never be more than + * 255 different snapshot formats supported by cgit... */ ctx.cfg.snapshots = 0xFF; scan++; @@ -975,15 +921,12 @@ static int calc_ttl() if (!ctx.qry.page) return ctx.cfg.cache_repo_ttl; - if (!strcmp(ctx.qry.page, "about")) - return ctx.cfg.cache_about_ttl; + if (ctx.qry.has_symref) + return ctx.cfg.cache_dynamic_ttl; if (ctx.qry.has_sha1) return ctx.cfg.cache_static_ttl; - if (ctx.qry.has_symref) - return ctx.cfg.cache_dynamic_ttl; - return ctx.cfg.cache_repo_ttl; } @@ -992,10 +935,7 @@ int main(int argc, const char **argv) const char *path; int err, ttl; - cgit_init_filters(); - atexit(cgit_cleanup_filters); - - prepare_context(); + prepare_context(&ctx); cgit_repolist.length = 0; cgit_repolist.count = 0; cgit_repolist.repos = NULL; @@ -1031,23 +971,14 @@ int main(int argc, const char **argv) cgit_parse_url(ctx.qry.url); } - /* Before we go any further, we set ctx.env.authenticated by checking to see - * if the supplied cookie is valid. All cookies are valid if there is no - * auth_filter. If there is an auth_filter, the filter decides. */ - authenticate_cookie(); - ttl = calc_ttl(); - if (ttl < 0) - ctx.page.expires += 10 * 365 * 24 * 60 * 60; /* 10 years */ - else - ctx.page.expires += ttl * 60; - if (!ctx.env.authenticated || (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))) + ctx.page.expires += ttl * 60; + if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD")) ctx.cfg.nocache = 1; if (ctx.cfg.nocache) ctx.cfg.cache_size = 0; err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, - ctx.qry.raw, ttl, process_request); - cgit_cleanup_filters(); + ctx.qry.raw, ttl, process_request, &ctx); if (err) cgit_print_error("Error processing page: %s (%d)", strerror(err), err); diff --git a/cgit.css b/cgit.css index 71b0b9b..d467c66 100644 --- a/cgit.css +++ b/cgit.css @@ -291,15 +291,13 @@ div#cgit table.blob pre { padding: 0; margin: 0; } -div#cgit table.blob td.linenumbers a, -div#cgit table.ssdiff td.lineno a { +div#cgit table.blob a.no, div#cgit table.ssdiff a.no { color: gray; text-align: right; text-decoration: none; } -div#cgit table.blob td.linenumbers a:hover, -div#cgit table.ssdiff td.lineno a:hover { +div#cgit table.blob a.no a:hover { color: black; } diff --git a/cgit.h b/cgit.h index 496d0f6..f28cf30 100644 --- a/cgit.h +++ b/cgit.h @@ -53,24 +53,16 @@ typedef void (*filepair_fn)(struct diff_filepair *pair); typedef void (*linediff_fn)(char *line, int len); typedef enum { - ABOUT, COMMIT, SOURCE, EMAIL, AUTH + ABOUT, COMMIT, SOURCE } filter_type; struct cgit_filter { - int (*open)(struct cgit_filter *, va_list ap); - int (*close)(struct cgit_filter *); - void (*fprintf)(struct cgit_filter *, FILE *, const char *prefix); - void (*cleanup)(struct cgit_filter *); - int argument_count; -}; - -struct cgit_exec_filter { - struct cgit_filter base; char *cmd; char **argv; int old_stdout; int pipe_fh[2]; int pid; + int exitstatus; }; struct cgit_repo { @@ -99,7 +91,6 @@ struct cgit_repo { struct cgit_filter *about_filter; struct cgit_filter *commit_filter; struct cgit_filter *source_filter; - struct cgit_filter *email_filter; struct string_list submodules; }; @@ -209,7 +200,6 @@ struct cgit_config { int cache_root_ttl; int cache_scanrc_ttl; int cache_static_ttl; - int cache_about_ttl; int case_sensitive_sort; int embedded; int enable_filter_overrides; @@ -251,8 +241,6 @@ struct cgit_config { struct cgit_filter *about_filter; struct cgit_filter *commit_filter; struct cgit_filter *source_filter; - struct cgit_filter *email_filter; - struct cgit_filter *auth_filter; }; struct cgit_page { @@ -279,10 +267,6 @@ struct cgit_environment { const char *script_name; const char *server_name; const char *server_port; - const char *http_cookie; - const char *http_referer; - unsigned int content_length; - int authenticated; }; struct cgit_context { @@ -358,13 +342,8 @@ extern const char *cgit_repobasename(const char *reponame); extern int cgit_parse_snapshots_mask(const char *str); -extern int cgit_open_filter(struct cgit_filter *filter, ...); +extern int cgit_open_filter(struct cgit_filter *filter); extern int cgit_close_filter(struct cgit_filter *filter); -extern void cgit_fprintf_filter(struct cgit_filter *filter, FILE *f, const char *prefix); -extern void cgit_exec_filter_init(struct cgit_exec_filter *filter, char *cmd, char **argv); -extern struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype); -extern void cgit_cleanup_filters(void); -extern void cgit_init_filters(void); extern void cgit_prepare_repo_env(struct cgit_repo * repo); diff --git a/cgit.mk b/cgit.mk index 056c3f9..8af0041 100644 --- a/cgit.mk +++ b/cgit.mk @@ -25,55 +25,10 @@ ifdef NO_C99_FORMAT CFLAGS += -DNO_C99_FORMAT endif -ifdef NO_LUA - LUA_MESSAGE := linking without specified Lua support - CGIT_CFLAGS += -DNO_LUA -else -LUAJIT_CFLAGS := $(shell pkg-config --cflags luajit 2>/dev/null) -LUAJIT_LIBS := $(shell pkg-config --libs luajit 2>/dev/null) -LUA_LIBS := $(shell pkg-config --libs lua 2>/dev/null) -LUA_CFLAGS := $(shell pkg-config --cflags lua 2>/dev/null) -ifeq (JIT,$(LUA_IMPLEMENTATION)) - ifeq ($(strip $(LUAJIT_LIBS)),) - $(error LuaJIT specified via LUA_IMPLEMENTATION=JIT, but library could not be found.) - endif - LUA_MESSAGE := linking with selected LuaJIT - CGIT_LIBS += $(LUAJIT_LIBS) - CGIT_CFLAGS += $(LUAJIT_CFLAGS) -else ifeq (VANILLA,$(LUA_IMPLEMENTATION)) - ifeq ($(strip $(LUA_LIBS)),) - $(error Lua specified via LUA_IMPLEMENTATION=VANILLA, but library could not be found.) - endif - LUA_MESSAGE := linking with selected Lua - CGIT_LIBS += $(LUA_LIBS) - CGIT_LIBS += $(LUA_CFLAGS) -else ifneq ($(strip $(LUAJIT_LIBS)),) - LUA_MESSAGE := linking with autodetected LuaJIT - CGIT_LIBS += $(LUAJIT_LIBS) - CGIT_CFLAGS += $(LUAJIT_CFLAGS) -else ifneq ($(strip $(LUA_LIBS)),) - LUA_MESSAGE := linking with autodetected Lua - CGIT_LIBS += $(LUA_LIBS) - CGIT_CFLAGS += $(LUA_CFLAGS) -else - LUA_MESSAGE := linking without autodetected Lua support - NO_LUA := YesPlease - CGIT_CFLAGS += -DNO_LUA -endif - -endif - -# Add -ldl to linker flags on non-BSD systems. -ifeq ($(findstring BSD,$(uname_S)),) - CGIT_LIBS += -ldl -endif - - CGIT_OBJ_NAMES += cgit.o CGIT_OBJ_NAMES += cache.o CGIT_OBJ_NAMES += cmd.o CGIT_OBJ_NAMES += configfile.o -CGIT_OBJ_NAMES += filter.o CGIT_OBJ_NAMES += html.o CGIT_OBJ_NAMES += parsing.o CGIT_OBJ_NAMES += scan-tree.o @@ -95,6 +50,7 @@ CGIT_OBJ_NAMES += ui-stats.o CGIT_OBJ_NAMES += ui-summary.o CGIT_OBJ_NAMES += ui-tag.o CGIT_OBJ_NAMES += ui-tree.o +CGIT_OBJ_NAMES += vector.o CGIT_OBJS := $(addprefix $(CGIT_PREFIX),$(CGIT_OBJ_NAMES)) @@ -105,6 +61,7 @@ $(CGIT_VERSION_OBJS): $(CGIT_PREFIX)VERSION $(CGIT_VERSION_OBJS): EXTRA_CPPFLAGS = \ -DCGIT_VERSION='"$(CGIT_VERSION)"' + # Git handles dependencies using ":=" so dependencies in CGIT_OBJ are not # handled by that and we must handle them ourselves. cgit_dep_files := $(foreach f,$(CGIT_OBJS),$(dir $f).depend/$(notdir $f).d) @@ -131,5 +88,4 @@ $(CGIT_OBJS): %.o: %.c GIT-CFLAGS $(CGIT_PREFIX)CGIT-CFLAGS $(missing_dep_dirs) $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $(CGIT_CFLAGS) $< $(CGIT_PREFIX)cgit: $(CGIT_OBJS) GIT-LDFLAGS $(GITLIBS) - @echo 1>&1 " * $(LUA_MESSAGE)" - $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) $(CGIT_LIBS) + $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) diff --git a/cgitrc.5.txt b/cgitrc.5.txt index 8eafc4a..b072467 100644 --- a/cgitrc.5.txt +++ b/cgitrc.5.txt @@ -39,15 +39,7 @@ agefile:: to specify the date and time of the youngest commit in the repository. The first line in the file is used as input to the "parse_date" function in libgit. Recommended timestamp-format is "yyyy-mm-dd - hh:mm:ss". You may want to generate this file from a post-receive - hook. Default value: "info/web/last-modified". - -auth-filter:: - Specifies a command that will be invoked for authenticating repository - access. Receives quite a few arguments, and data on both stdin and - stdout for authentication processing. Details follow later in this - document. If no auth-filter is specified, no authentication is - performed. Default value: none. See also: "FILTER API". + hh:mm:ss". Default value: "info/web/last-modified". branch-sort:: Flag which, when set to "age", enables date ordering in the branch ref @@ -58,40 +50,32 @@ cache-root:: Path used to store the cgit cache entries. Default value: "/var/cache/cgit". See also: "MACRO EXPANSION". -cache-static-ttl:: - Number which specifies the time-to-live, in minutes, for the cached - version of repository pages accessed with a fixed SHA1. Negative - values have infinite ttl. Default value: -1". - cache-dynamic-ttl:: Number which specifies the time-to-live, in minutes, for the cached - version of repository pages accessed without a fixed SHA1. Negative - values have infinite ttl. Default value: "5". + version of repository pages accessed without a fixed SHA1. Default + value: "5". cache-repo-ttl:: Number which specifies the time-to-live, in minutes, for the cached - version of the repository summary page. Negative values have infinite - ttl. Default value: "5". + version of the repository summary page. Default value: "5". cache-root-ttl:: Number which specifies the time-to-live, in minutes, for the cached - version of the repository index page. Negative values have infinite - ttl. Default value: "5". + version of the repository index page. Default value: "5". cache-scanrc-ttl:: Number which specifies the time-to-live, in minutes, for the result - of scanning a path for git repositories. Negative values have infinite - ttl. Default value: "15". - -cache-about-ttl:: - Number which specifies the time-to-live, in minutes, for the cached - version of the repository about page. Negative values have infinite - ttl. Default value: "15". + of scanning a path for git repositories. Default value: "15". cache-size:: The maximum number of entries in the cgit cache. Default value: "0" (i.e. caching is disabled). +cache-static-ttl:: + Number which specifies the time-to-live, in minutes, for the cached + version of repository pages accessed with a fixed SHA1. Default value: + "5". + case-sensitive-sort:: Sort items in the repo list case sensitively. Default value: "1". See also: repository-sort, section-sort. @@ -124,14 +108,6 @@ css:: Url which specifies the css document to include in all cgit pages. Default value: "/cgit.css". -email-filter:: - Specifies a command which will be invoked to format names and email - address of committers, authors, and taggers, as represented in various - places throughout the cgit interface. This command will receive an - email address and an origin page string as its command line arguments, - and the text to format on STDIN. It is to write the formatted text back - out onto STDOUT. Default value: none. See also: "FILTER API". - embedded:: Flag which, when set to "1", will make cgit generate a html fragment suitable for embedding in other html pages. Default value: none. See @@ -148,9 +124,8 @@ enable-filter-overrides:: enable-http-clone:: If set to "1", cgit will act as an dumb HTTP endpoint for git clones. - You can add "http://$HTTP_HOST$SCRIPT_NAME/$CGIT_REPO_URL" to clone-url - to expose this feature. If you use an alternate way of serving git - repositories, you may wish to disable this. Default value: "1". + If you use an alternate way of serving git repositories, you may wish + to disable this. Default value: "1". enable-index-links:: Flag which, when set to "1", will make cgit generate extra links for @@ -192,14 +167,14 @@ enable-git-config:: "scan-path", and must be defined prior, to augment repo-specific settings. The keys gitweb.owner, gitweb.category, and gitweb.description will map to the cgit keys repo.owner, repo.section, and repo.desc, - respectively. All git config keys that begin with "cgit." will be mapped + respectivly. All git config keys that begin with "cgit." will be mapped to the corresponding "repo." key in cgit. Default value: "0". See also: scan-path, section-from-path. favicon:: - Url used as link to a shortcut icon for cgit. It is suggested to use - the value "/favicon.ico" since certain browsers will ignore other - values. Default value: "/favicon.ico". + Url used as link to a shortcut icon for cgit. If specified, it is + suggested to use the value "/favicon.ico" since certain browsers will + ignore other values. Default value: none. footer:: The content of the file specified with this option will be included @@ -300,7 +275,7 @@ nocache:: value: "0". noplainemail:: - If set to "1" showing full author email addresses will be disabled. + If set to "1" showing full author email adresses will be disabled. Default value: "0". noheader:: @@ -397,10 +372,10 @@ side-by-side-diffs:: default. Default value: "0". snapshots:: - Text which specifies the default set of snapshot formats that cgit - generates links for. The value is a space-separated list of zero or - more of the values "tar", "tar.gz", "tar.bz2", "tar.xz" and "zip". - Default value: none. + Text which specifies the default set of snapshot formats generated by + cgit. The value is a space-separated list of zero or more of the + values "tar", "tar.gz", "tar.bz2", "tar.xz" and "zip". Default value: + none. source-filter:: Specifies a command which will be invoked to format plaintext blobs @@ -472,10 +447,6 @@ repo.defbranch:: repo.desc:: The value to show as repository description. Default value: none. -repo.email-filter:: - Override the default email-filter. Default value: none. See also: - "enable-filter-overrides". See also: "FILTER API". - repo.enable-commit-graph:: A flag which can be used to disable the global setting `enable-commit-graph'. Default value: none. @@ -545,9 +516,8 @@ repo.readme:: file. Default value: . repo.snapshots:: - A mask of snapshot formats for this repo that cgit generates links for, - restricted by the global "snapshots" setting. Default value: - . + A mask of allowed snapshot-formats for this repo, restricted by the + "snapshots" global setting. Default value: . repo.section:: Override the current section name for this repository. Default value: @@ -577,47 +547,6 @@ config files, e.g. "repo.desc" becomes "desc". FILTER API ---------- -By default, filters are separate processes that are executed each time they -are needed. Alternative technologies may be used by prefixing the filter -specification with the relevant string; available values are: - -'exec:':: - The default "one process per filter" mode. - -'lua:':: - Executes the script using a built-in Lua interpreter. The script is - loaded once per execution of cgit, and may be called multiple times - during cgit's lifetime, making it a good choice for repeated filters - such as the 'email filter'. It responds to three functions: - - 'filter_open(argument1, argument2, argument3, ...)':: - This is called upon activation of the filter for a particular - set of data. - 'filter_write(buffer)':: - This is called whenever cgit writes data to the webpage. - 'filter_close()':: - This is called when the current filtering operation is - completed. It must return an integer value. Usually 0 - indicates success. - - Additionally, cgit exposes to the Lua the following built-in functions: - - 'html(str)':: - Writes 'str' to the webpage. - 'html_txt(str)':: - HTML escapes and writes 'str' to the webpage. - 'html_attr(str)':: - HTML escapes for an attribute and writes "str' to the webpage. - 'html_url_path(str)':: - URL escapes for a path and writes 'str' to the webpage. - 'html_url_arg(str)':: - URL escapes for an argument and writes 'str' to the webpage. - 'html_include(file)':: - Includes 'file' in webpage. - - -Parameters are provided to filters as follows. - about filter:: This filter is given a single parameter: the filename of the source file to filter. The filter can use the filename to determine (for @@ -630,13 +559,6 @@ commit filter:: be filtered is available on standard input and the filtered text is expected on standard output. -email filter:: - This filter is given two parameters: the email address of the relevent - author and a string indicating the originating page. The filter will - then receive the text string to format on standard input and is - expected to write to standard output the formatted text to be included - in the page. - source filter:: This filter is given a single parameter: the filename of the source file to filter. The filter can use the filename to determine (for @@ -644,34 +566,7 @@ source filter:: file that is to be filtered is available on standard input and the filtered contents is expected on standard output. -auth filter:: - The authentication filter receives 12 parameters: - - filter action, explained below, which specifies which action the - filter is called for - - http cookie - - http method - - http referer - - http path - - http https flag - - cgit repo - - cgit page - - cgit url - - cgit login url - When the filter action is "body", this filter must write to output the - HTML for displaying the login form, which POSTs to the login url. When - the filter action is "authenticate-cookie", this filter must validate - the http cookie and return a 0 if it is invalid or 1 if it is invalid, - in the exit code / close function. If the filter action is - "authenticate-post", this filter receives POST'd parameters on - standard input, and should write a complete CGI request, preferably - with a 302 redirect, and write to output one or more "Set-Cookie" - HTTP headers, each followed by a newline. - - Please see `filters/simple-authentication.lua` for a clear example - script that may be modified. - - -All filters are handed the following environment variables: +Also, all filters are handed the following environment variables: - CGIT_REPO_URL (from repo.url) - CGIT_REPO_NAME (from repo.name) @@ -688,8 +583,8 @@ environment variable will be unset. MACRO EXPANSION --------------- -The following cgitrc options support a simple macro expansion feature, -where tokens prefixed with "$" are replaced with the value of a similarly +The following cgitrc options supports a simple macro expansion feature, +where tokens prefixed with "$" are replaced with the value of a similary named environment variable: - cache-root @@ -716,7 +611,7 @@ EXAMPLE CGITRC FILE ------------------- .... -# Enable caching of up to 1000 output entries +# Enable caching of up to 1000 output entriess cache-size=1000 @@ -732,7 +627,7 @@ enable-index-owner=1 # Allow http transport git clone -enable-http-clone=1 +enable-git-clone=1 # Show extra links for each repository on the index page @@ -796,7 +691,7 @@ mimetype.png=image/png mimetype.svg=image/svg+xml -# Highlight source code with python pygments-based highlighter +# Highlight source code with python pygments-based highligher source-filter=/var/www/cgit/filters/syntax-highlighting.py # Format markdown, restructuredtext, manpages, text files, and html files diff --git a/cmd.c b/cmd.c index cbd235c..abe8e46 100644 --- a/cmd.c +++ b/cmd.c @@ -1,6 +1,7 @@ /* cmd.c: the cgit command dispatcher * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2008 Lars Hjemli + * Copyright (C) 2013 Jason A. Donenfeld . * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -26,120 +27,115 @@ #include "ui-tag.h" #include "ui-tree.h" -static void HEAD_fn(void) +static void HEAD_fn(struct cgit_context *ctx) { - cgit_clone_head(); + cgit_clone_head(ctx); } -static void atom_fn(void) +static void atom_fn(struct cgit_context *ctx) { - cgit_print_atom(ctx.qry.head, ctx.qry.path, ctx.cfg.max_atom_items); + cgit_print_atom(ctx->qry.head, ctx->qry.path, ctx->cfg.max_atom_items); } -static void about_fn(void) +static void about_fn(struct cgit_context *ctx) { - if (ctx.repo) - cgit_print_repo_readme(ctx.qry.path); + if (ctx->repo) + cgit_print_repo_readme(ctx->qry.path); else cgit_print_site_readme(); } -static void blob_fn(void) +static void blob_fn(struct cgit_context *ctx) { - cgit_print_blob(ctx.qry.sha1, ctx.qry.path, ctx.qry.head, 0); + cgit_print_blob(ctx->qry.sha1, ctx->qry.path, ctx->qry.head, 0); } -static void commit_fn(void) +static void commit_fn(struct cgit_context *ctx) { - cgit_print_commit(ctx.qry.sha1, ctx.qry.path); + cgit_print_commit(ctx->qry.sha1, ctx->qry.path); } -static void diff_fn(void) +static void diff_fn(struct cgit_context *ctx) { - cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1, 0); + cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path, 1); } -static void rawdiff_fn(void) +static void info_fn(struct cgit_context *ctx) { - cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1, 1); + cgit_clone_info(ctx); } -static void info_fn(void) +static void log_fn(struct cgit_context *ctx) { - cgit_clone_info(); + cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count, + ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1, + ctx->repo->enable_commit_graph, + ctx->repo->commit_sort); } -static void log_fn(void) +static void ls_cache_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, - ctx.repo->enable_commit_graph, - ctx.repo->commit_sort); + ctx->page.mimetype = "text/plain"; + ctx->page.filename = "ls-cache.txt"; + cgit_print_http_headers(ctx); + cache_ls(ctx->cfg.cache_root); } -static void ls_cache_fn(void) +static void objects_fn(struct cgit_context *ctx) { - ctx.page.mimetype = "text/plain"; - ctx.page.filename = "ls-cache.txt"; - cgit_print_http_headers(); - cache_ls(ctx.cfg.cache_root); + cgit_clone_objects(ctx); } -static void objects_fn(void) -{ - cgit_clone_objects(); -} - -static void repolist_fn(void) +static void repolist_fn(struct cgit_context *ctx) { cgit_print_repolist(); } -static void patch_fn(void) +static void patch_fn(struct cgit_context *ctx) { - cgit_print_patch(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); + cgit_print_patch(ctx->qry.sha1, ctx->qry.path); } -static void plain_fn(void) +static void plain_fn(struct cgit_context *ctx) { - cgit_print_plain(); + cgit_print_plain(ctx); } -static void refs_fn(void) +static void refs_fn(struct cgit_context *ctx) { cgit_print_refs(); } -static void snapshot_fn(void) +static void snapshot_fn(struct cgit_context *ctx) { - cgit_print_snapshot(ctx.qry.head, ctx.qry.sha1, ctx.qry.path, - ctx.repo->snapshots, ctx.qry.nohead); + cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, ctx->qry.path, + ctx->repo->snapshots, ctx->qry.nohead); } -static void stats_fn(void) +static void stats_fn(struct cgit_context *ctx) { - cgit_show_stats(); + cgit_show_stats(ctx); } -static void summary_fn(void) +static void summary_fn(struct cgit_context *ctx) { cgit_print_summary(); } -static void tag_fn(void) +static void tag_fn(struct cgit_context *ctx) { - cgit_print_tag(ctx.qry.sha1); + cgit_print_tag(ctx->qry.sha1); } -static void tree_fn(void) +static void tree_fn(struct cgit_context *ctx) { - cgit_print_tree(ctx.qry.sha1, ctx.qry.path); + cgit_print_tree(ctx->qry.sha1, ctx->qry.path); } #define def_cmd(name, want_repo, want_layout, want_vpath, is_clone) \ {#name, name##_fn, want_repo, want_layout, want_vpath, is_clone} -struct cgit_cmd *cgit_get_cmd(void) +struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) { static struct cgit_cmd cmds[] = { def_cmd(HEAD, 1, 0, 0, 1), @@ -154,7 +150,6 @@ struct cgit_cmd *cgit_get_cmd(void) def_cmd(objects, 1, 0, 0, 1), def_cmd(patch, 1, 0, 1, 0), def_cmd(plain, 1, 0, 0, 0), - def_cmd(rawdiff, 1, 0, 1, 0), def_cmd(refs, 1, 1, 0, 0), def_cmd(repolist, 0, 0, 0, 0), def_cmd(snapshot, 1, 0, 0, 0), @@ -165,15 +160,15 @@ struct cgit_cmd *cgit_get_cmd(void) }; int i; - if (ctx.qry.page == NULL) { - if (ctx.repo) - ctx.qry.page = "summary"; + if (ctx->qry.page == NULL) { + if (ctx->repo) + ctx->qry.page = "summary"; else - ctx.qry.page = "repolist"; + ctx->qry.page = "repolist"; } for (i = 0; i < sizeof(cmds)/sizeof(*cmds); i++) - if (!strcmp(ctx.qry.page, cmds[i].name)) + if (!strcmp(ctx->qry.page, cmds[i].name)) return &cmds[i]; return NULL; } diff --git a/cmd.h b/cmd.h index 752f078..eb5bc87 100644 --- a/cmd.h +++ b/cmd.h @@ -1,7 +1,7 @@ #ifndef CMD_H #define CMD_H -typedef void (*cgit_cmd_fn)(void); +typedef void (*cgit_cmd_fn)(struct cgit_context *ctx); struct cgit_cmd { const char *name; @@ -12,6 +12,6 @@ struct cgit_cmd { is_clone:1; }; -extern struct cgit_cmd *cgit_get_cmd(void); +extern struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx); #endif /* CMD_H */ diff --git a/configfile.c b/configfile.c index 833f158..d98989c 100644 --- a/configfile.c +++ b/configfile.c @@ -1,6 +1,6 @@ /* configfile.c: parsing of config files * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2008 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -31,45 +31,45 @@ static void skip_line(FILE *f) ; } -static int read_config_line(FILE *f, struct strbuf *name, struct strbuf *value) +static int read_config_line(FILE *f, char *line, const char **value, int bufsize) { - int c = next_char(f); + int i = 0, isname = 0; - strbuf_reset(name); - strbuf_reset(value); - - /* Skip comments and preceding spaces. */ - for(;;) { - if (c == '#' || c == ';') + *value = NULL; + while (i < bufsize - 1) { + int c = next_char(f); + if (!isname && (c == '#' || c == ';')) { skip_line(f); - else if (!isspace(c)) - break; - c = next_char(f); - } - - /* Read variable name. */ - while (c != '=') { - if (c == '\n' || c == EOF) - return 0; - strbuf_addch(name, c); - c = next_char(f); - } + continue; + } + if (!isname && isspace(c)) + continue; - /* Read variable value. */ - c = next_char(f); - while (c != '\n' && c != EOF) { - strbuf_addch(value, c); - c = next_char(f); + if (c == '=' && !*value) { + line[i] = 0; + *value = &line[i + 1]; + } else if (c == '\n' && !isname) { + i = 0; + continue; + } else if (c == '\n' || c == EOF) { + line[i] = 0; + break; + } else { + line[i] = c; + } + isname = 1; + i++; } - - return 1; + line[i + 1] = 0; + return i; } int parse_configfile(const char *filename, configfile_value_fn fn) { static int nesting; - struct strbuf name = STRBUF_INIT; - struct strbuf value = STRBUF_INIT; + int len; + char line[256]; + const char *value; FILE *f; /* cancel deeply nested include-commands */ @@ -78,12 +78,10 @@ int parse_configfile(const char *filename, configfile_value_fn fn) if (!(f = fopen(filename, "r"))) return -1; nesting++; - while (read_config_line(f, &name, &value)) - fn(name.buf, value.buf); + while ((len = read_config_line(f, line, &value, sizeof(line))) > 0) + fn(line, value); nesting--; fclose(f); - strbuf_release(&name); - strbuf_release(&value); return 0; } diff --git a/configfile.h b/configfile.h index af7ca19..04235e5 100644 --- a/configfile.h +++ b/configfile.h @@ -1,8 +1,6 @@ #ifndef CONFIGFILE_H #define CONFIGFILE_H -#include "cgit.h" - typedef void (*configfile_value_fn)(const char *name, const char *value); extern int parse_configfile(const char *filename, configfile_value_fn fn); diff --git a/favicon.ico b/favicon.ico deleted file mode 100644 index 56ff593..0000000 Binary files a/favicon.ico and /dev/null differ diff --git a/filter.c b/filter.c deleted file mode 100644 index 90b9d68..0000000 --- a/filter.c +++ /dev/null @@ -1,450 +0,0 @@ -/* filter.c: filter framework functions - * - * Copyright (C) 2006-2014 cgit Development Team - * - * Licensed under GNU General Public License v2 - * (see COPYING for full license text) - */ - -#include "cgit.h" -#include "html.h" -#include -#include -#include -#include -#include -#include -#include -#ifndef NO_LUA -#include -#include -#include -#endif - -static ssize_t (*libc_write)(int fd, const void *buf, size_t count); -static ssize_t (*filter_write)(struct cgit_filter *base, const void *buf, size_t count) = NULL; -static struct cgit_filter *current_write_filter = NULL; - -static inline void reap_filter(struct cgit_filter *filter) -{ - if (filter && filter->cleanup) - filter->cleanup(filter); -} - -void cgit_cleanup_filters(void) -{ - int i; - reap_filter(ctx.cfg.about_filter); - reap_filter(ctx.cfg.commit_filter); - reap_filter(ctx.cfg.source_filter); - reap_filter(ctx.cfg.email_filter); - reap_filter(ctx.cfg.auth_filter); - for (i = 0; i < cgit_repolist.count; ++i) { - reap_filter(cgit_repolist.repos[i].about_filter); - reap_filter(cgit_repolist.repos[i].commit_filter); - reap_filter(cgit_repolist.repos[i].source_filter); - reap_filter(cgit_repolist.repos[i].email_filter); - } -} - -void cgit_init_filters(void) -{ - libc_write = dlsym(RTLD_NEXT, "write"); - if (!libc_write) - die("Could not locate libc's write function"); -} - -ssize_t write(int fd, const void *buf, size_t count) -{ - if (fd != STDOUT_FILENO || !filter_write) - return libc_write(fd, buf, count); - return filter_write(current_write_filter, buf, count); -} - -static inline void hook_write(struct cgit_filter *filter, ssize_t (*new_write)(struct cgit_filter *base, const void *buf, size_t count)) -{ - /* We want to avoid buggy nested patterns. */ - assert(filter_write == NULL); - assert(current_write_filter == NULL); - current_write_filter = filter; - filter_write = new_write; -} - -static inline void unhook_write() -{ - assert(filter_write != NULL); - assert(current_write_filter != NULL); - filter_write = NULL; - current_write_filter = NULL; -} - -static int open_exec_filter(struct cgit_filter *base, va_list ap) -{ - struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base; - int i; - - for (i = 0; i < filter->base.argument_count; i++) - filter->argv[i + 1] = va_arg(ap, char *); - - filter->old_stdout = chk_positive(dup(STDOUT_FILENO), - "Unable to duplicate STDOUT"); - chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess"); - filter->pid = chk_non_negative(fork(), "Unable to create subprocess"); - if (filter->pid == 0) { - close(filter->pipe_fh[1]); - chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO), - "Unable to use pipe as STDIN"); - execvp(filter->cmd, filter->argv); - die_errno("Unable to exec subprocess %s", filter->cmd); - } - close(filter->pipe_fh[0]); - chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO), - "Unable to use pipe as STDOUT"); - close(filter->pipe_fh[1]); - return 0; -} - -static int close_exec_filter(struct cgit_filter *base) -{ - struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base; - int i, exit_status = 0; - - chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), - "Unable to restore STDOUT"); - close(filter->old_stdout); - if (filter->pid < 0) - goto done; - waitpid(filter->pid, &exit_status, 0); - if (WIFEXITED(exit_status)) - goto done; - die("Subprocess %s exited abnormally", filter->cmd); - -done: - for (i = 0; i < filter->base.argument_count; i++) - filter->argv[i + 1] = NULL; - return WEXITSTATUS(exit_status); - -} - -static void fprintf_exec_filter(struct cgit_filter *base, FILE *f, const char *prefix) -{ - struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base; - fprintf(f, "%sexec:%s\n", prefix, filter->cmd); -} - -static void cleanup_exec_filter(struct cgit_filter *base) -{ - struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base; - if (filter->argv) { - free(filter->argv); - filter->argv = NULL; - } - if (filter->cmd) { - free(filter->cmd); - filter->cmd = NULL; - } -} - -static struct cgit_filter *new_exec_filter(const char *cmd, int argument_count) -{ - struct cgit_exec_filter *f; - int args_size = 0; - - f = xmalloc(sizeof(*f)); - /* We leave argv for now and assign it below. */ - cgit_exec_filter_init(f, xstrdup(cmd), NULL); - f->base.argument_count = argument_count; - args_size = (2 + argument_count) * sizeof(char *); - f->argv = xmalloc(args_size); - memset(f->argv, 0, args_size); - f->argv[0] = f->cmd; - return &f->base; -} - -void cgit_exec_filter_init(struct cgit_exec_filter *filter, char *cmd, char **argv) -{ - memset(filter, 0, sizeof(*filter)); - filter->base.open = open_exec_filter; - filter->base.close = close_exec_filter; - filter->base.fprintf = fprintf_exec_filter; - filter->base.cleanup = cleanup_exec_filter; - filter->cmd = cmd; - filter->argv = argv; - /* The argument count for open_filter is zero by default, unless called from new_filter, above. */ - filter->base.argument_count = 0; -} - -#ifndef NO_LUA -struct lua_filter { - struct cgit_filter base; - char *script_file; - lua_State *lua_state; -}; - -static void error_lua_filter(struct lua_filter *filter) -{ - die("Lua error in %s: %s", filter->script_file, lua_tostring(filter->lua_state, -1)); - lua_pop(filter->lua_state, 1); -} - -static ssize_t write_lua_filter(struct cgit_filter *base, const void *buf, size_t count) -{ - struct lua_filter *filter = (struct lua_filter *)base; - - lua_getglobal(filter->lua_state, "filter_write"); - lua_pushlstring(filter->lua_state, buf, count); - if (lua_pcall(filter->lua_state, 1, 0, 0)) { - error_lua_filter(filter); - errno = EIO; - return -1; - } - return count; -} - -static inline int hook_lua_filter(lua_State *lua_state, void (*fn)(const char *txt)) -{ - const char *str; - ssize_t (*save_filter_write)(struct cgit_filter *base, const void *buf, size_t count); - struct cgit_filter *save_filter; - - str = lua_tostring(lua_state, 1); - if (!str) - return 0; - - save_filter_write = filter_write; - save_filter = current_write_filter; - unhook_write(); - fn(str); - hook_write(save_filter, save_filter_write); - - return 0; -} - -static int html_lua_filter(lua_State *lua_state) -{ - return hook_lua_filter(lua_state, html); -} - -static int html_txt_lua_filter(lua_State *lua_state) -{ - return hook_lua_filter(lua_state, html_txt); -} - -static int html_attr_lua_filter(lua_State *lua_state) -{ - return hook_lua_filter(lua_state, html_attr); -} - -static int html_url_path_lua_filter(lua_State *lua_state) -{ - return hook_lua_filter(lua_state, html_url_path); -} - -static int html_url_arg_lua_filter(lua_State *lua_state) -{ - return hook_lua_filter(lua_state, html_url_arg); -} - -static int html_include_lua_filter(lua_State *lua_state) -{ - return hook_lua_filter(lua_state, (void (*)(const char *))html_include); -} - -static void cleanup_lua_filter(struct cgit_filter *base) -{ - struct lua_filter *filter = (struct lua_filter *)base; - - if (!filter->lua_state) - return; - - lua_close(filter->lua_state); - filter->lua_state = NULL; - if (filter->script_file) { - free(filter->script_file); - filter->script_file = NULL; - } -} - -static int init_lua_filter(struct lua_filter *filter) -{ - if (filter->lua_state) - return 0; - - if (!(filter->lua_state = luaL_newstate())) - return 1; - - luaL_openlibs(filter->lua_state); - - lua_pushcfunction(filter->lua_state, html_lua_filter); - lua_setglobal(filter->lua_state, "html"); - lua_pushcfunction(filter->lua_state, html_txt_lua_filter); - lua_setglobal(filter->lua_state, "html_txt"); - lua_pushcfunction(filter->lua_state, html_attr_lua_filter); - lua_setglobal(filter->lua_state, "html_attr"); - lua_pushcfunction(filter->lua_state, html_url_path_lua_filter); - lua_setglobal(filter->lua_state, "html_url_path"); - lua_pushcfunction(filter->lua_state, html_url_arg_lua_filter); - lua_setglobal(filter->lua_state, "html_url_arg"); - lua_pushcfunction(filter->lua_state, html_include_lua_filter); - lua_setglobal(filter->lua_state, "html_include"); - - if (luaL_dofile(filter->lua_state, filter->script_file)) { - error_lua_filter(filter); - lua_close(filter->lua_state); - filter->lua_state = NULL; - return 1; - } - return 0; -} - -static int open_lua_filter(struct cgit_filter *base, va_list ap) -{ - struct lua_filter *filter = (struct lua_filter *)base; - int i; - - if (init_lua_filter(filter)) - return 1; - - hook_write(base, write_lua_filter); - - lua_getglobal(filter->lua_state, "filter_open"); - for (i = 0; i < filter->base.argument_count; ++i) - lua_pushstring(filter->lua_state, va_arg(ap, char *)); - if (lua_pcall(filter->lua_state, filter->base.argument_count, 0, 0)) { - error_lua_filter(filter); - return 1; - } - return 0; -} - -static int close_lua_filter(struct cgit_filter *base) -{ - struct lua_filter *filter = (struct lua_filter *)base; - int ret = 0; - - lua_getglobal(filter->lua_state, "filter_close"); - if (lua_pcall(filter->lua_state, 0, 1, 0)) { - error_lua_filter(filter); - ret = -1; - } else { - ret = lua_tonumber(filter->lua_state, -1); - lua_pop(filter->lua_state, 1); - } - - unhook_write(); - return ret; -} - -static void fprintf_lua_filter(struct cgit_filter *base, FILE *f, const char *prefix) -{ - struct lua_filter *filter = (struct lua_filter *)base; - fprintf(f, "%slua:%s\n", prefix, filter->script_file); -} - - -static struct cgit_filter *new_lua_filter(const char *cmd, int argument_count) -{ - struct lua_filter *filter; - - filter = xmalloc(sizeof(*filter)); - memset(filter, 0, sizeof(*filter)); - filter->base.open = open_lua_filter; - filter->base.close = close_lua_filter; - filter->base.fprintf = fprintf_lua_filter; - filter->base.cleanup = cleanup_lua_filter; - filter->base.argument_count = argument_count; - filter->script_file = xstrdup(cmd); - - return &filter->base; -} - -#endif - - -int cgit_open_filter(struct cgit_filter *filter, ...) -{ - int result; - va_list ap; - if (!filter) - return 0; - va_start(ap, filter); - result = filter->open(filter, ap); - va_end(ap); - return result; -} - -int cgit_close_filter(struct cgit_filter *filter) -{ - if (!filter) - return 0; - return filter->close(filter); -} - -void cgit_fprintf_filter(struct cgit_filter *filter, FILE *f, const char *prefix) -{ - filter->fprintf(filter, f, prefix); -} - - - -static const struct { - const char *prefix; - struct cgit_filter *(*ctor)(const char *cmd, int argument_count); -} filter_specs[] = { - { "exec", new_exec_filter }, -#ifndef NO_LUA - { "lua", new_lua_filter }, -#endif -}; - -struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype) -{ - char *colon; - int i; - size_t len; - int argument_count; - - if (!cmd || !cmd[0]) - return NULL; - - colon = strchr(cmd, ':'); - len = colon - cmd; - /* - * In case we're running on Windows, don't allow a single letter before - * the colon. - */ - if (len == 1) - colon = NULL; - - switch (filtertype) { - case AUTH: - argument_count = 12; - break; - - case EMAIL: - argument_count = 2; - break; - - case SOURCE: - case ABOUT: - argument_count = 1; - break; - - case COMMIT: - default: - argument_count = 0; - break; - } - - /* If no prefix is given, exec filter is the default. */ - if (!colon) - return new_exec_filter(cmd, argument_count); - - for (i = 0; i < ARRAY_SIZE(filter_specs); i++) { - if (len == strlen(filter_specs[i].prefix) && - !strncmp(filter_specs[i].prefix, cmd, len)) - return filter_specs[i].ctor(colon + 1, argument_count); - } - - die("Invalid filter type: %.*s", (int) len, cmd); -} diff --git a/filters/about-formatting.sh b/filters/about-formatting.sh index 892fbeb..313a4e6 100755 --- a/filters/about-formatting.sh +++ b/filters/about-formatting.sh @@ -18,7 +18,7 @@ # CGIT_REPO_CLONE_URL ( = repo.clone-url setting ) cd "$(dirname $0)/html-converters/" -case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in +case "$(tr '[:upper:]' '[:lower:]' <<<"$1")" in *.md|*.mkd) exec ./md2html; ;; *.rst) exec ./rst2html; ;; *.[1-9]) exec ./man2html; ;; diff --git a/filters/email-gravatar.lua b/filters/email-gravatar.lua deleted file mode 100644 index 52cf426..0000000 --- a/filters/email-gravatar.lua +++ /dev/null @@ -1,26 +0,0 @@ --- This script may be used with the email-filter or repo.email-filter settings in cgitrc. --- It adds gravatar icons to author names. It is designed to be used with the lua: --- prefix in filters. It is much faster than the corresponding python script. --- --- Requirements: --- luacrypto >= 0.3 --- --- - -local crypto = require("crypto") - -function filter_open(email, page) - buffer = "" - md5 = crypto.digest("md5", email:sub(2, -2):lower()) -end - -function filter_close() - html("Gravatar " .. buffer) - return 0 -end - -function filter_write(str) - buffer = buffer .. str -end - - diff --git a/filters/email-gravatar.py b/filters/email-gravatar.py deleted file mode 100755 index d70440e..0000000 --- a/filters/email-gravatar.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python3 - -# Please prefer the email-gravatar.lua using lua: as a prefix over this script. This -# script is very slow, in comparison. -# -# This script may be used with the email-filter or repo.email-filter settings in cgitrc. -# -# The following environment variables can be used to retrieve the configuration -# of the repository for which this script is called: -# CGIT_REPO_URL ( = repo.url setting ) -# CGIT_REPO_NAME ( = repo.name setting ) -# CGIT_REPO_PATH ( = repo.path setting ) -# CGIT_REPO_OWNER ( = repo.owner setting ) -# CGIT_REPO_DEFBRANCH ( = repo.defbranch setting ) -# CGIT_REPO_SECTION ( = section setting ) -# CGIT_REPO_CLONE_URL ( = repo.clone-url setting ) -# -# It receives an email address on argv[1] and text on stdin. It prints -# to stdout that text prepended by a gravatar at 10pt. - -import sys -import hashlib -import codecs - -email = sys.argv[1].lower().strip() -if email[0] == '<': - email = email[1:] -if email[-1] == '>': - email = email[0:-1] - -page = sys.argv[2] - -sys.stdin = codecs.getreader("utf-8")(sys.stdin.detach()) -sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) - -md5 = hashlib.md5(email.encode()).hexdigest() -text = sys.stdin.read().strip() - -print("Gravatar " + text) diff --git a/filters/html-converters/resources/markdown.pl b/filters/html-converters/resources/markdown.pl index d40b2d0..abec173 100755 --- a/filters/html-converters/resources/markdown.pl +++ b/filters/html-converters/resources/markdown.pl @@ -18,6 +18,10 @@ use vars qw($VERSION); $VERSION = '1.0.1'; # Tue 14 Dec 2004 +## Disabled; causes problems under Perl 5.6.1: +use utf8; +binmode( STDOUT, ":utf8" ); # c.f.: http://acis.openlib.org/dev/perl-unicode-struggle.html + # # Global default settings: diff --git a/filters/simple-authentication.lua b/filters/simple-authentication.lua deleted file mode 100644 index 230d3a3..0000000 --- a/filters/simple-authentication.lua +++ /dev/null @@ -1,272 +0,0 @@ --- This script may be used with the auth-filter. Be sure to configure it as you wish. --- --- Requirements: --- luacrypto >= 0.3 --- --- - - --- --- --- Configure these variables for your settings. --- --- - --- A list of password protected repositories along with the users who can access them. -local protected_repos = { - glouglou = { laurent = true, jason = true }, - qt = { jason = true, bob = true } -} - --- Please note that, in production, you'll want to replace this simple lookup --- table with either a table of salted and hashed passwords (using something --- smart like scrypt), or replace this table lookup with an external support, --- such as consulting your system's pam / shadow system, or an external --- database, or an external validating web service. For testing, or for --- extremely low-security usage, you may be able, however, to get away with --- compromising on hardcoding the passwords in cleartext, as we have done here. -local users = { - jason = "secretpassword", - laurent = "s3cr3t", - bob = "ilikelua" -} - --- All cookies will be authenticated based on this secret. Make it something --- totally random and impossible to guess. It should be large. -local secret = "BE SURE TO CUSTOMIZE THIS STRING TO SOMETHING BIG AND RANDOM" - - - --- --- --- Authentication functions follow below. Swap these out if you want different authentication semantics. --- --- - --- Sets HTTP cookie headers based on post and sets up redirection. -function authenticate_post() - local password = users[post["username"]] - local redirect = validate_value(post["redirect"]) - - if redirect == nil then - not_found() - return 0 - end - - redirect_to(redirect) - - -- Lua hashes strings, so these comparisons are time invariant. - if password == nil or password ~= post["password"] then - set_cookie("cgitauth", "") - else - -- One week expiration time - local username = secure_value(post["username"], os.time() + 604800) - set_cookie("cgitauth", username) - end - - html("\n") - return 0 -end - - --- Returns 1 if the cookie is valid and 0 if it is not. -function authenticate_cookie() - accepted_users = protected_repos[cgit["repo"]] - if accepted_users == nil then - -- We return as valid if the repo is not protected. - return 1 - end - - local username = validate_value(get_cookie(http["cookie"], "cgitauth")) - if username == nil or not accepted_users[username:lower()] then - return 0 - else - return 1 - end -end - --- Prints the html for the login form. -function body() - html("

Authentication Required

") - html("
") - html("") - html("") - html("") - html("") - html("") - html("
") - - return 0 -end - - - --- --- --- Wrapper around filter API, exposing the http table, the cgit table, and the post table to the above functions. --- --- - -local actions = {} -actions["authenticate-post"] = authenticate_post -actions["authenticate-cookie"] = authenticate_cookie -actions["body"] = body - -function filter_open(...) - action = actions[select(1, ...)] - - http = {} - http["cookie"] = select(2, ...) - http["method"] = select(3, ...) - http["query"] = select(4, ...) - http["referer"] = select(5, ...) - http["path"] = select(6, ...) - http["host"] = select(7, ...) - http["https"] = select(8, ...) - - cgit = {} - cgit["repo"] = select(9, ...) - cgit["page"] = select(10, ...) - cgit["url"] = select(11, ...) - cgit["login"] = select(12, ...) - -end - -function filter_close() - return action() -end - -function filter_write(str) - post = parse_qs(str) -end - - --- --- --- Utility functions based on keplerproject/wsapi. --- --- - -function url_decode(str) - if not str then - return "" - end - str = string.gsub(str, "+", " ") - str = string.gsub(str, "%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end) - str = string.gsub(str, "\r\n", "\n") - return str -end - -function url_encode(str) - if not str then - return "" - end - str = string.gsub(str, "\n", "\r\n") - str = string.gsub(str, "([^%w ])", function (c) return string.format("%%%02X", string.byte(c)) end) - str = string.gsub(str, " ", "+") - return str -end - -function parse_qs(qs) - local tab = {} - for key, val in string.gmatch(qs, "([^&=]+)=([^&=]*)&?") do - tab[url_decode(key)] = url_decode(val) - end - return tab -end - -function get_cookie(cookies, name) - cookies = string.gsub(";" .. cookies .. ";", "%s*;%s*", ";") - return url_decode(string.match(cookies, ";" .. name .. "=(.-);")) -end - - --- --- --- Cookie construction and validation helpers. --- --- - -local crypto = require("crypto") - --- Returns value of cookie if cookie is valid. Otherwise returns nil. -function validate_value(cookie) - local i = 0 - local value = "" - local expiration = 0 - local salt = "" - local hmac = "" - - if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then - return nil - end - - for component in string.gmatch(cookie, "[^|]+") do - if i == 0 then - value = component - elseif i == 1 then - expiration = tonumber(component) - if expiration == nil then - expiration = 0 - end - elseif i == 2 then - salt = component - elseif i == 3 then - hmac = component - else - break - end - i = i + 1 - end - - if hmac == nil or hmac:len() == 0 then - return nil - end - - -- Lua hashes strings, so these comparisons are time invariant. - if hmac ~= crypto.hmac.digest("sha1", value .. "|" .. tostring(expiration) .. "|" .. salt, secret) then - return nil - end - - if expiration ~= 0 and expiration <= os.time() then - return nil - end - - return url_decode(value) -end - -function secure_value(value, expiration) - if value == nil or value:len() <= 0 then - return "" - end - - local authstr = "" - local salt = crypto.hex(crypto.rand.bytes(16)) - value = url_encode(value) - authstr = value .. "|" .. tostring(expiration) .. "|" .. salt - authstr = authstr .. "|" .. crypto.hmac.digest("sha1", authstr, secret) - return authstr -end - -function set_cookie(cookie, value) - html("Set-Cookie: " .. cookie .. "=" .. value .. "; HttpOnly") - if http["https"] == "yes" or http["https"] == "on" or http["https"] == "1" then - html("; secure") - end - html("\n") -end - -function redirect_to(url) - html("Status: 302 Redirect\n") - html("Cache-Control: no-cache, no-store\n") - html("Location: " .. url .. "\n") -end - -function not_found() - html("Status: 404 Not Found\n") - html("Cache-Control: no-cache, no-store\n\n") -end diff --git a/filters/syntax-highlighting.py b/filters/syntax-highlighting.py index bcf32c8..dcdba03 100755 --- a/filters/syntax-highlighting.py +++ b/filters/syntax-highlighting.py @@ -1,16 +1,13 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 -# This script uses Pygments and Python2. You must have both installed -# for this to work. -# +# This script uses Pygments and Python3. You must have both installed for this to work. # http://pygments.org/ # http://python.org/ # -# It may be used with the source-filter or repo.source-filter settings -# in cgitrc. +# It may be used with the source-filter or repo.source-filter settings in cgitrc. # -# The following environment variables can be used to retrieve the -# configuration of the repository for which this script is called: +# The following environment variables can be used to retrieve the configuration +# of the repository for which this script is called: # CGIT_REPO_URL ( = repo.url setting ) # CGIT_REPO_NAME ( = repo.name setting ) # CGIT_REPO_PATH ( = repo.path setting ) @@ -21,33 +18,21 @@ import sys +import cgi +import codecs +from pygments.lexers import get_lexer_for_filename from pygments import highlight -from pygments.util import ClassNotFound -from pygments.lexers import TextLexer -from pygments.lexers import guess_lexer -from pygments.lexers import guess_lexer_for_filename from pygments.formatters import HtmlFormatter - -# read stdin and decode to utf-8. ignore any unkown signs. -data = sys.stdin.read().decode(encoding='utf-8', errors='ignore') -filename = sys.argv[1] -formatter = HtmlFormatter(encoding='utf-8', style='pastie') - +sys.stdin = codecs.getreader("utf-8")(sys.stdin.detach()) +doc = sys.stdin.read() try: - lexer = guess_lexer_for_filename(filename, data, encoding='utf-8') -except ClassNotFound: - # check if there is any shebang - if data[0:2] == '#!': - lexer = guess_lexer(data, encoding='utf-8') - else: - lexer = TextLexer(encoding='utf-8') -except TypeError: - lexer = TextLexer(encoding='utf-8') + lexer = get_lexer_for_filename(sys.argv[1]) + formatter = HtmlFormatter(style='pastie') + sys.stdout.write("") -# highlight! :-) -# printout pygments' css definitions as well -sys.stdout.write('') -highlight(data, lexer, formatter, outfile=sys.stdout) + highlight(doc, lexer, formatter, sys.stdout) +except: + sys.stdout.write(str(cgi.escape(doc).encode("ascii", "xmlcharrefreplace"), "ascii")) diff --git a/filters/syntax-highlighting.sh b/filters/syntax-highlighting.sh index 4fa7928..24f6bb4 100755 --- a/filters/syntax-highlighting.sh +++ b/filters/syntax-highlighting.sh @@ -9,9 +9,7 @@ # # Note: the highlight command (http://www.andre-simon.de/) uses css for syntax # highlighting, so you'll probably want something like the following included -# in your css file: -# -# Style definition file generated by highlight 2.4.8, http://www.andre-simon.de/ +# in your css file (generated by highlight 2.4.8 and adapted for cgit): # # table.blob .num { color:#2928ff; } # table.blob .esc { color:#ff00ff; } @@ -26,66 +24,6 @@ # table.blob .kwc { color:#000000; font-weight:bold; } # table.blob .kwd { color:#010181; } # -# -# Style definition file generated by highlight 2.6.14, http://www.andre-simon.de/ -# -# body.hl { background-color:#ffffff; } -# pre.hl { color:#000000; background-color:#ffffff; font-size:10pt; font-family:'Courier New';} -# .hl.num { color:#2928ff; } -# .hl.esc { color:#ff00ff; } -# .hl.str { color:#ff0000; } -# .hl.dstr { color:#818100; } -# .hl.slc { color:#838183; font-style:italic; } -# .hl.com { color:#838183; font-style:italic; } -# .hl.dir { color:#008200; } -# .hl.sym { color:#000000; } -# .hl.line { color:#555555; } -# .hl.mark { background-color:#ffffbb;} -# .hl.kwa { color:#000000; font-weight:bold; } -# .hl.kwb { color:#830000; } -# .hl.kwc { color:#000000; font-weight:bold; } -# .hl.kwd { color:#010181; } -# -# -# Style definition file generated by highlight 3.8, http://www.andre-simon.de/ -# -# body.hl { background-color:#e0eaee; } -# pre.hl { color:#000000; background-color:#e0eaee; font-size:10pt; font-family:'Courier New';} -# .hl.num { color:#b07e00; } -# .hl.esc { color:#ff00ff; } -# .hl.str { color:#bf0303; } -# .hl.pps { color:#818100; } -# .hl.slc { color:#838183; font-style:italic; } -# .hl.com { color:#838183; font-style:italic; } -# .hl.ppc { color:#008200; } -# .hl.opt { color:#000000; } -# .hl.lin { color:#555555; } -# .hl.kwa { color:#000000; font-weight:bold; } -# .hl.kwb { color:#0057ae; } -# .hl.kwc { color:#000000; font-weight:bold; } -# .hl.kwd { color:#010181; } -# -# -# Style definition file generated by highlight 3.13, http://www.andre-simon.de/ -# -# body.hl { background-color:#e0eaee; } -# pre.hl { color:#000000; background-color:#e0eaee; font-size:10pt; font-family:'Courier New',monospace;} -# .hl.num { color:#b07e00; } -# .hl.esc { color:#ff00ff; } -# .hl.str { color:#bf0303; } -# .hl.pps { color:#818100; } -# .hl.slc { color:#838183; font-style:italic; } -# .hl.com { color:#838183; font-style:italic; } -# .hl.ppc { color:#008200; } -# .hl.opt { color:#000000; } -# .hl.ipl { color:#0057ae; } -# .hl.lin { color:#555555; } -# .hl.kwa { color:#000000; font-weight:bold; } -# .hl.kwb { color:#0057ae; } -# .hl.kwc { color:#000000; font-weight:bold; } -# .hl.kwd { color:#010181; } -# -# # The following environment variables can be used to retrieve the configuration # of the repository for which this script is called: # CGIT_REPO_URL ( = repo.url setting ) diff --git a/git b/git index d2446df..edca415 160000 --- a/git +++ b/git @@ -1 +1 @@ -Subproject commit d2446dfd7f3b3f8948142cfb07a0270e2497d93f +Subproject commit edca4152560522a431a51fc0a06147fc680b5b18 diff --git a/html.c b/html.c index f0ee2d6..03277db 100644 --- a/html.c +++ b/html.c @@ -1,6 +1,6 @@ /* html.c: helper functions for html output * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -41,6 +41,8 @@ static const char* url_escape_table[256] = { "%fe", "%ff" }; +static int htmlfd = STDOUT_FILENO; + char *fmt(const char *format, ...) { static char buf[8][1024]; @@ -75,7 +77,7 @@ char *fmtalloc(const char *format, ...) void html_raw(const char *data, size_t size) { - if (write(STDOUT_FILENO, data, size) != size) + if (write(htmlfd, data, size) != size) die_errno("write error on html output"); } diff --git a/parsing.c b/parsing.c index 599f61e..658621d 100644 --- a/parsing.c +++ b/parsing.c @@ -1,6 +1,6 @@ -/* parsing.c: parsing of config files +/* config.c: parsing of config files * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -142,25 +142,25 @@ struct commitinfo *cgit_parse_commit(struct commit *commit) if (p == NULL) return ret; - if (prefixcmp(p, "tree ")) + if (strncmp(p, "tree ", 5)) die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); else p += 46; // "tree " + hex[40] + "\n" - while (!prefixcmp(p, "parent ")) + while (!strncmp(p, "parent ", 7)) p += 48; // "parent " + hex[40] + "\n" - if (p && !prefixcmp(p, "author ")) { + if (p && !strncmp(p, "author ", 7)) { p = parse_user(p + 7, &ret->author, &ret->author_email, &ret->author_date); } - if (p && !prefixcmp(p, "committer ")) { - p = parse_user(p + 10, &ret->committer, &ret->committer_email, + if (p && !strncmp(p, "committer ", 9)) { + p = parse_user(p + 9, &ret->committer, &ret->committer_email, &ret->committer_date); } - if (p && !prefixcmp(p, "encoding ")) { + if (p && !strncmp(p, "encoding ", 9)) { p += 9; t = strchr(p, '\n'); if (t) { @@ -239,7 +239,7 @@ struct taginfo *cgit_parse_tag(struct tag *tag) if (*p == '\n') break; - if (!prefixcmp(p, "tagger ")) { + if (!strncmp(p, "tagger ", 7)) { p = parse_user(p + 7, &ret->tagger, &ret->tagger_email, &ret->tagger_date); } else { diff --git a/robots.txt b/robots.txt deleted file mode 100644 index 4ce948f..0000000 --- a/robots.txt +++ /dev/null @@ -1,3 +0,0 @@ -User-agent: * -Disallow: /*/snapshot/* -Allow: / diff --git a/scan-tree.c b/scan-tree.c index 49de658..2684b44 100644 --- a/scan-tree.c +++ b/scan-tree.c @@ -1,6 +1,7 @@ /* scan-tree.c - * - * Copyright (C) 2006-2014 cgit Development Team + * + * Copyright (C) 2008-2009 Lars Hjemli + * Copyright (C) 2010-2013 Jason A. Donenfeld * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -105,7 +106,7 @@ static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn) return; strbuf_setlen(path, pathlen); - if (prefixcmp(path->buf, base)) + if (strncmp(base, path->buf, strlen(base))) strbuf_addbuf(&rel, path); else strbuf_addstr(&rel, path->buf + strlen(base) + 1); @@ -147,14 +148,14 @@ static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn) } if (ctx.cfg.section_from_path) { - n = ctx.cfg.section_from_path; + n = ctx.cfg.section_from_path; if (n > 0) { - slash = rel.buf - 1; - while (slash && n && (slash = strchr(slash + 1, '/'))) + slash = rel.buf; + while (slash && n && (slash = strchr(slash, '/'))) n--; } else { slash = rel.buf + rel.len; - while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/'))) + while (slash && n && (slash = xstrrchr(rel.buf, slash, '/'))) n++; } if (slash && !n) { @@ -229,6 +230,8 @@ end: closedir(dir); } +#define lastc(s) s[strlen(s) - 1] + void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn) { struct strbuf line = STRBUF_INIT; diff --git a/shared.c b/shared.c index 7e88bbd..919a99e 100644 --- a/shared.c +++ b/shared.c @@ -1,6 +1,6 @@ /* shared.c: global vars + some callback functions * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -71,7 +71,6 @@ struct cgit_repo *cgit_add_repo(const char *url) ret->about_filter = ctx.cfg.about_filter; ret->commit_filter = ctx.cfg.commit_filter; ret->source_filter = ctx.cfg.source_filter; - ret->email_filter = ctx.cfg.email_filter; ret->clone_url = ctx.cfg.clone_url; ret->submodules.strdup_strings = 1; return ret; @@ -405,29 +404,28 @@ void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix) int cgit_parse_snapshots_mask(const char *str) { - struct string_list tokens = STRING_LIST_INIT_DUP; - struct string_list_item *item; const struct cgit_snapshot_format *f; - int rv = 0; + static const char *delim = " \t,:/|;"; + int tl, sl, rv = 0; /* favor legacy setting */ if (atoi(str)) return 1; - - string_list_split(&tokens, str, ' ', -1); - string_list_remove_empty_items(&tokens, 0); - - for_each_string_list_item(item, &tokens) { + for (;;) { + str += strspn(str, delim); + tl = strcspn(str, delim); + if (!tl) + break; for (f = cgit_snapshot_formats; f->suffix; f++) { - if (!strcmp(item->string, f->suffix) || - !strcmp(item->string, f->suffix + 1)) { + sl = strlen(f->suffix); + if ((tl == sl && !strncmp(f->suffix, str, tl)) || + (tl == sl - 1 && !strncmp(f->suffix + 1, str, tl - 1))) { rv |= f->bit; break; } } + str += tl; } - - string_list_clear(&tokens, 0); return rv; } @@ -458,6 +456,40 @@ void cgit_prepare_repo_env(struct cgit_repo * repo) fprintf(stderr, warn, p->name, p->value); } +int cgit_open_filter(struct cgit_filter *filter) +{ + + filter->old_stdout = chk_positive(dup(STDOUT_FILENO), + "Unable to duplicate STDOUT"); + chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess"); + filter->pid = chk_non_negative(fork(), "Unable to create subprocess"); + if (filter->pid == 0) { + close(filter->pipe_fh[1]); + chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO), + "Unable to use pipe as STDIN"); + execvp(filter->cmd, filter->argv); + die_errno("Unable to exec subprocess %s", filter->cmd); + } + close(filter->pipe_fh[0]); + chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO), + "Unable to use pipe as STDOUT"); + close(filter->pipe_fh[1]); + return 0; +} + +int cgit_close_filter(struct cgit_filter *filter) +{ + chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), + "Unable to restore STDOUT"); + close(filter->old_stdout); + if (filter->pid < 0) + return 0; + waitpid(filter->pid, &filter->exitstatus, 0); + if (WIFEXITED(filter->exitstatus) && !WEXITSTATUS(filter->exitstatus)) + return 0; + die("Subprocess %s exited abnormally", filter->cmd); +} + /* Read the content of the specified file into a newly allocated buffer, * zeroterminate the buffer and return 0 on success, errno otherwise. */ diff --git a/tests/Makefile b/tests/Makefile index 1556475..8c6c236 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -5,7 +5,7 @@ T = $(wildcard t[0-9][0-9][0-9][0-9]-*.sh) all: $(T) $(T): - @./$@ $(CGIT_TEST_OPTS) + @./$@ clean: $(RM) -rf trash diff --git a/tests/filters/dump.lua b/tests/filters/dump.lua deleted file mode 100644 index 1f15c93..0000000 --- a/tests/filters/dump.lua +++ /dev/null @@ -1,17 +0,0 @@ -function filter_open(...) - buffer = "" - for i = 1, select("#", ...) do - buffer = buffer .. select(i, ...) .. " " - end -end - -function filter_close() - html(buffer) - return 0 -end - -function filter_write(str) - buffer = buffer .. string.upper(str) -end - - diff --git a/tests/filters/dump.sh b/tests/filters/dump.sh deleted file mode 100755 index da6f7a1..0000000 --- a/tests/filters/dump.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -[ "$#" -gt 0 ] && printf "%s " "$*" -tr '[:lower:]' '[:upper:]' diff --git a/tests/setup.sh b/tests/setup.sh index 785edd7..1d8677a 100755 --- a/tests/setup.sh +++ b/tests/setup.sh @@ -15,50 +15,13 @@ # run_test 'repo index' 'cgit_url "/" | tidy -e' # run_test 'repo summary' 'cgit_url "/foo" | tidy -e' -# We don't want to run Git commands through Valgrind, so we filter out the -# --valgrind option here and handle it ourselves. We copy the arguments -# assuming that none contain a newline, although other whitespace is -# preserved. -LF=' -' -test_argv= - -while test $# != 0 -do - case "$1" in - --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind) - cgit_valgrind=t - test_argv="$test_argv${LF}--verbose" - ;; - *) - test_argv="$test_argv$LF$1" - ;; - esac - shift -done - -OLDIFS=$IFS -IFS=$LF -set -- $test_argv -IFS=$OLDIFS - : ${TEST_DIRECTORY=$(pwd)/../git/t} : ${TEST_OUTPUT_DIRECTORY=$(pwd)} TEST_NO_CREATE_REPO=YesPlease . "$TEST_DIRECTORY"/test-lib.sh # Prepend the directory containing cgit to PATH. -if test -n "$cgit_valgrind" -then - GIT_VALGRIND="$TEST_DIRECTORY/valgrind" - CGIT_VALGRIND=$(cd ../valgrind && pwd) - PATH="$CGIT_VALGRIND/bin:$PATH" - export GIT_VALGRIND CGIT_VALGRIND -else - PATH="$(pwd)/../..:$PATH" -fi - -FILTER_DIRECTORY=$(cd ../filters && pwd) +PATH="$(pwd)/../..:$PATH" mkrepo() { name=$1 @@ -92,7 +55,6 @@ setup_repos() mkrepo repos/bar 50 >/dev/null mkrepo repos/foo+bar 10 testplus >/dev/null mkrepo "repos/with space" 2 >/dev/null - mkrepo repos/filter 5 testplus >/dev/null cat >cgitrc <tmp' test_expect_success 'find line 1' ' - grep "1" tmp + grep "1" tmp ' test_expect_success 'no line 2' ' - ! grep "2" tmp + ! grep "2" tmp ' test_expect_success 'generate foo+bar/tree' 'cgit_url "foo%2bbar/tree" >tmp' diff --git a/tests/t0108-patch.sh b/tests/t0108-patch.sh index fcc749d..3b5bae4 100755 --- a/tests/t0108-patch.sh +++ b/tests/t0108-patch.sh @@ -20,18 +20,11 @@ test_expect_success 'find `Subject:` line' ' ' test_expect_success 'find `cgit` signature' ' - tail -2 tmp | head -1 | grep "^cgit" -' - -test_expect_success 'compare with output of git-format-patch(1)' ' - CGIT_VERSION=$(sed -n "s/CGIT_VERSION = //p" ../../VERSION) - git --git-dir="$PWD/repos/foo/.git" format-patch -p --subject-prefix="" --signature="cgit $CGIT_VERSION" --stdout HEAD^ >tmp2 - sed "1,5d" tmp >tmp_ - cmp tmp_ tmp2 + tail -1 tmp | grep "^cgit" ' test_expect_success 'find initial commit' ' - root=$(git --git-dir="$PWD/repos/foo/.git" rev-list --max-parents=0 HEAD) + root=$(git --git-dir="$PWD/repos/foo/.git" rev-list HEAD | tail -1) ' test_expect_success 'generate patch for initial commit' ' @@ -39,24 +32,7 @@ test_expect_success 'generate patch for initial commit' ' ' test_expect_success 'find `cgit` signature' ' - tail -2 tmp | head -1 | grep "^cgit" -' - -test_expect_success 'generate patches for multiple commits' ' - id=$(git --git-dir="$PWD/repos/foo/.git" rev-parse HEAD) - id2=$(git --git-dir="$PWD/repos/foo/.git" rev-parse HEAD~3) - cgit_query "url=foo/patch&id=$id&id2=$id2" >tmp -' - -test_expect_success 'find `cgit` signature' ' - tail -2 tmp | head -1 | grep "^cgit" -' - -test_expect_success 'compare with output of git-format-patch(1)' ' - CGIT_VERSION=$(sed -n "s/CGIT_VERSION = //p" ../../VERSION) - git --git-dir="$PWD/repos/foo/.git" format-patch -p -N --subject-prefix="" --signature="cgit $CGIT_VERSION" --stdout HEAD~3..HEAD >tmp2 - sed "1,5d" tmp >tmp_ - cmp tmp_ tmp2 + tail -1 tmp | grep "^cgit" ' test_done diff --git a/tests/t0110-rawdiff.sh b/tests/t0110-rawdiff.sh deleted file mode 100755 index 500e68c..0000000 --- a/tests/t0110-rawdiff.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/sh - -test_description='Check content on rawdiff page' -. ./setup.sh - -test_expect_success 'generate foo/rawdiff' ' - cgit_query "url=foo/rawdiff" >tmp -' - -test_expect_success 'compare with output of git-diff(1)' ' - git --git-dir="$PWD/repos/foo/.git" diff HEAD^.. >tmp2 - sed "1,4d" tmp >tmp_ - cmp tmp_ tmp2 -' - -test_expect_success 'find initial commit' ' - root=$(git --git-dir="$PWD/repos/foo/.git" rev-list --max-parents=0 HEAD) -' - -test_expect_success 'generate diff for initial commit' ' - cgit_query "url=foo/rawdiff&id=$root" >tmp -' - -test_expect_success 'compare with output of git-diff-tree(1)' ' - git --git-dir="$PWD/repos/foo/.git" diff-tree -p --no-commit-id --root "$root" >tmp2 - sed "1,4d" tmp >tmp_ - cmp tmp_ tmp2 -' - -test_expect_success 'generate diff for multiple commits' ' - id=$(git --git-dir="$PWD/repos/foo/.git" rev-parse HEAD) - id2=$(git --git-dir="$PWD/repos/foo/.git" rev-parse HEAD~3) - cgit_query "url=foo/rawdiff&id=$id&id2=$id2" >tmp -' - -test_expect_success 'compare with output of git-diff(1)' ' - git --git-dir="$PWD/repos/foo/.git" diff HEAD~3..HEAD >tmp2 - sed "1,4d" tmp >tmp_ - cmp tmp_ tmp2 -' - -test_done diff --git a/tests/t0111-filter.sh b/tests/t0111-filter.sh deleted file mode 100755 index 730f1c0..0000000 --- a/tests/t0111-filter.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/sh - -test_description='Check filtered content' -. ./setup.sh - -for prefix in exec lua -do - test_expect_success "generate filter-$prefix/tree/a%2bb" " - cgit_url 'filter-$prefix/tree/a%2bb' >tmp - " - - test_expect_success "check whether the $prefix source filter works" ' - grep "a+b HELLO$" tmp - ' - - test_expect_success "generate filter-$prefix/about/" " - cgit_url 'filter-$prefix/about/' >tmp - " - - test_expect_success "check whether the $prefix about filter works" ' - grep "
a+b HELLO$" tmp - ' - - test_expect_success "generate filter-$prefix/commit/" " - cgit_url 'filter-$prefix/commit/' >tmp - " - - test_expect_success "check whether the $prefix commit filter works" ' - grep "
ADD A+B" tmp - ' - - test_expect_success "check whether the $prefix email filter works for authors" ' - grep " commit A U THOR <AUTHOR@EXAMPLE.COM>" tmp - ' - - test_expect_success "check whether the $prefix email filter works for committers" ' - grep " commit C O MITTER <COMMITTER@EXAMPLE.COM>" tmp - ' -done - -test_done diff --git a/tests/valgrind/bin/cgit b/tests/valgrind/bin/cgit deleted file mode 100755 index dcdfbe5..0000000 --- a/tests/valgrind/bin/cgit +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh - -# Note that we currently use Git's suppression file and there are variables -# $GIT_VALGRIND and $CGIT_VALGRIND which point to different places. -exec valgrind -q --error-exitcode=126 \ - --suppressions="$GIT_VALGRIND/default.supp" \ - --gen-suppressions=all \ - --leak-check=no \ - --track-origins=yes \ - --log-fd=4 \ - --input-fd=4 \ - "$CGIT_VALGRIND/../../cgit" "$@" diff --git a/ui-atom.c b/ui-atom.c index b22d745..2a1eb59 100644 --- a/ui-atom.c +++ b/ui-atom.c @@ -1,6 +1,6 @@ /* ui-atom.c: functions for atom feeds * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2008 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -108,7 +108,7 @@ void cgit_print_atom(char *tip, char *path, int max_count) host = cgit_hosturl(); ctx.page.mimetype = "text/xml"; ctx.page.charset = "utf-8"; - cgit_print_http_headers(); + cgit_print_http_headers(&ctx); html("\n"); html(""); html_txt(ctx.repo->name); diff --git a/ui-blob.c b/ui-blob.c index 9c99519..eb14a75 100644 --- a/ui-blob.c +++ b/ui-blob.c @@ -1,6 +1,7 @@ /* ui-blob.c: show blob content * - * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> + * Copyright (C) 2008 Lars Hjemli + * Copyright (C) 2010-2013 Jason A. Donenfeld <Jason@zx2c4.com> * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -164,6 +165,6 @@ void cgit_print_blob(const char *hex, char *path, const char *head, int file_onl ctx.page.mimetype = "text/plain"; } ctx.page.filename = path; - cgit_print_http_headers(); + cgit_print_http_headers(&ctx); html_raw(buf, size); } diff --git a/ui-clone.c b/ui-clone.c index d25553b..30d020e 100644 --- a/ui-clone.c +++ b/ui-clone.c @@ -1,7 +1,7 @@ /* ui-clone.c: functions for http cloning, based on * git's http-backend.c by Shawn O. Pearce * - * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> + * Copyright (C) 2008 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -29,22 +29,22 @@ static int print_ref_info(const char *refname, const unsigned char *sha1, return 0; } -static void print_pack_info(void) +static void print_pack_info(struct cgit_context *ctx) { struct packed_git *pack; int ofs; - ctx.page.mimetype = "text/plain"; - ctx.page.filename = "objects/info/packs"; - cgit_print_http_headers(); - ofs = strlen(ctx.repo->path) + strlen("/objects/pack/"); + ctx->page.mimetype = "text/plain"; + ctx->page.filename = "objects/info/packs"; + cgit_print_http_headers(ctx); + ofs = strlen(ctx->repo->path) + strlen("/objects/pack/"); prepare_packed_git(); for (pack = packed_git; pack; pack = pack->next) if (pack->pack_local) htmlf("P %s\n", pack->pack_name + ofs); } -static void send_file(char *path) +static void send_file(struct cgit_context *ctx, char *path) { struct stat st; @@ -61,41 +61,41 @@ static void send_file(char *path) } return; } - ctx.page.mimetype = "application/octet-stream"; - ctx.page.filename = path; - if (prefixcmp(ctx.repo->path, path)) - ctx.page.filename += strlen(ctx.repo->path) + 1; - cgit_print_http_headers(); + ctx->page.mimetype = "application/octet-stream"; + ctx->page.filename = path; + if (prefixcmp(ctx->repo->path, path)) + ctx->page.filename += strlen(ctx->repo->path) + 1; + cgit_print_http_headers(ctx); html_include(path); } -void cgit_clone_info(void) +void cgit_clone_info(struct cgit_context *ctx) { - if (!ctx.qry.path || strcmp(ctx.qry.path, "refs")) + if (!ctx->qry.path || strcmp(ctx->qry.path, "refs")) return; - ctx.page.mimetype = "text/plain"; - ctx.page.filename = "info/refs"; - cgit_print_http_headers(); - for_each_ref(print_ref_info, NULL); + ctx->page.mimetype = "text/plain"; + ctx->page.filename = "info/refs"; + cgit_print_http_headers(ctx); + for_each_ref(print_ref_info, ctx); } -void cgit_clone_objects(void) +void cgit_clone_objects(struct cgit_context *ctx) { - if (!ctx.qry.path) { + if (!ctx->qry.path) { html_status(400, "Bad request", 0); return; } - if (!strcmp(ctx.qry.path, "info/packs")) { - print_pack_info(); + if (!strcmp(ctx->qry.path, "info/packs")) { + print_pack_info(ctx); return; } - send_file(git_path("objects/%s", ctx.qry.path)); + send_file(ctx, git_path("objects/%s", ctx->qry.path)); } -void cgit_clone_head(void) +void cgit_clone_head(struct cgit_context *ctx) { - send_file(git_path("%s", "HEAD")); + send_file(ctx, git_path("%s", "HEAD")); } diff --git a/ui-clone.h b/ui-clone.h index 3e460a3..89cd4f1 100644 --- a/ui-clone.h +++ b/ui-clone.h @@ -1,8 +1,8 @@ #ifndef UI_CLONE_H #define UI_CLONE_H -void cgit_clone_info(void); -void cgit_clone_objects(void); -void cgit_clone_head(void); +void cgit_clone_info(struct cgit_context *ctx); +void cgit_clone_objects(struct cgit_context *ctx); +void cgit_clone_head(struct cgit_context *ctx); #endif /* UI_CLONE_H */ diff --git a/ui-commit.c b/ui-commit.c index c48bfe8..a5a6ea8 100644 --- a/ui-commit.c +++ b/ui-commit.c @@ -1,6 +1,6 @@ /* ui-commit.c: generate commit view * - * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> + * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -44,24 +44,20 @@ void cgit_print_commit(char *hex, const char *prefix) cgit_print_diff_ctrls(); html("<table summary='commit info' class='commit-info'>\n"); html("<tr><th>author</th><td>"); - cgit_open_filter(ctx.repo->email_filter, info->author_email, "commit"); html_txt(info->author); if (!ctx.cfg.noplainemail) { html(" "); html_txt(info->author_email); } - cgit_close_filter(ctx.repo->email_filter); html("</td><td class='right'>"); cgit_print_date(info->author_date, FMT_LONGDATE, ctx.cfg.local_time); html("</td></tr>\n"); html("<tr><th>committer</th><td>"); - cgit_open_filter(ctx.repo->email_filter, info->committer_email, "commit"); html_txt(info->committer); if (!ctx.cfg.noplainemail) { html(" "); html_txt(info->committer_email); } - cgit_close_filter(ctx.repo->email_filter); html("</td><td class='right'>"); cgit_print_date(info->committer_date, FMT_LONGDATE, ctx.cfg.local_time); html("</td></tr>\n"); @@ -111,22 +107,28 @@ void cgit_print_commit(char *hex, const char *prefix) } html("</table>\n"); html("<div class='commit-subject'>"); - cgit_open_filter(ctx.repo->commit_filter); + if (ctx.repo->commit_filter) + cgit_open_filter(ctx.repo->commit_filter); html_txt(info->subject); - cgit_close_filter(ctx.repo->commit_filter); + if (ctx.repo->commit_filter) + cgit_close_filter(ctx.repo->commit_filter); show_commit_decorations(commit); html("</div>"); html("<div class='commit-msg'>"); - cgit_open_filter(ctx.repo->commit_filter); + if (ctx.repo->commit_filter) + cgit_open_filter(ctx.repo->commit_filter); html_txt(info->msg); - cgit_close_filter(ctx.repo->commit_filter); + if (ctx.repo->commit_filter) + cgit_close_filter(ctx.repo->commit_filter); html("</div>"); if (notes.len != 0) { html("<div class='notes-header'>Notes</div>"); html("<div class='notes'>"); - cgit_open_filter(ctx.repo->commit_filter); + if (ctx.repo->commit_filter) + cgit_open_filter(ctx.repo->commit_filter); html_txt(notes.buf); - cgit_close_filter(ctx.repo->commit_filter); + if (ctx.repo->commit_filter) + cgit_close_filter(ctx.repo->commit_filter); html("</div>"); html("<div class='notes-footer'></div>"); } @@ -135,7 +137,7 @@ void cgit_print_commit(char *hex, const char *prefix) tmp = sha1_to_hex(commit->parents->item->object.sha1); else tmp = NULL; - cgit_print_diff(ctx.qry.sha1, tmp, prefix, 0, 0); + cgit_print_diff(ctx.qry.sha1, tmp, prefix, 0); } strbuf_release(¬es); cgit_free_commitinfo(info); diff --git a/ui-diff.c b/ui-diff.c index 71273aa..8b38209 100644 --- a/ui-diff.c +++ b/ui-diff.c @@ -1,6 +1,6 @@ /* ui-diff.c: show diff between two blobs * - * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> + * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -358,14 +358,17 @@ void cgit_print_diff_ctrls() } void cgit_print_diff(const char *new_rev, const char *old_rev, - const char *prefix, int show_ctrls, int raw) + const char *prefix, int show_ctrls) { + enum object_type type; + unsigned long size; struct commit *commit, *commit2; - const unsigned char *old_tree_sha1, *new_tree_sha1; if (!new_rev) new_rev = ctx.qry.head; - if (get_sha1(new_rev, new_rev_sha1)) { + get_sha1(new_rev, new_rev_sha1); + type = sha1_object_info(new_rev_sha1, &size); + if (type == OBJ_BAD) { cgit_print_error("Bad object name: %s", new_rev); return; } @@ -374,50 +377,25 @@ void cgit_print_diff(const char *new_rev, const char *old_rev, cgit_print_error("Bad commit: %s", sha1_to_hex(new_rev_sha1)); return; } - new_tree_sha1 = commit->tree->object.sha1; - if (old_rev) { - if (get_sha1(old_rev, old_rev_sha1)) { - cgit_print_error("Bad object name: %s", old_rev); - return; - } - } else if (commit->parents && commit->parents->item) { + if (old_rev) + get_sha1(old_rev, old_rev_sha1); + else if (commit->parents && commit->parents->item) hashcpy(old_rev_sha1, commit->parents->item->object.sha1); - } else { + else hashclr(old_rev_sha1); - } if (!is_null_sha1(old_rev_sha1)) { + type = sha1_object_info(old_rev_sha1, &size); + if (type == OBJ_BAD) { + cgit_print_error("Bad object name: %s", sha1_to_hex(old_rev_sha1)); + return; + } commit2 = lookup_commit_reference(old_rev_sha1); if (!commit2 || parse_commit(commit2)) { cgit_print_error("Bad commit: %s", sha1_to_hex(old_rev_sha1)); return; } - old_tree_sha1 = commit2->tree->object.sha1; - } else { - old_tree_sha1 = NULL; - } - - if (raw) { - struct diff_options diffopt; - - diff_setup(&diffopt); - diffopt.output_format = DIFF_FORMAT_PATCH; - DIFF_OPT_SET(&diffopt, RECURSIVE); - diff_setup_done(&diffopt); - - ctx.page.mimetype = "text/plain"; - cgit_print_http_headers(); - if (old_tree_sha1) { - diff_tree_sha1(old_tree_sha1, new_tree_sha1, "", - &diffopt); - } else { - diff_root_tree_sha1(new_tree_sha1, "", &diffopt); - } - diffcore_std(&diffopt); - diff_flush(&diffopt); - - return; } use_ssdiff = ctx.qry.has_ssdiff ? ctx.qry.ssdiff : ctx.cfg.ssdiff; diff --git a/ui-diff.h b/ui-diff.h index 04f9029..25a9296 100644 --- a/ui-diff.h +++ b/ui-diff.h @@ -4,7 +4,7 @@ extern void cgit_print_diff_ctrls(); extern void cgit_print_diff(const char *new_hex, const char *old_hex, - const char *prefix, int show_ctrls, int raw); + const char *prefix, int show_ctrls); extern struct diff_filespec *cgit_get_current_old_file(void); extern struct diff_filespec *cgit_get_current_new_file(void); diff --git a/ui-log.c b/ui-log.c index 499534c..6f1249b 100644 --- a/ui-log.c +++ b/ui-log.c @@ -1,6 +1,6 @@ /* ui-log.c: functions for log output * - * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> + * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -10,7 +10,7 @@ #include "ui-log.h" #include "html.h" #include "ui-shared.h" -#include "argv-array.h" +#include "vector.h" int files, add_lines, rem_lines; @@ -168,9 +168,7 @@ static void print_commit(struct commit *commit, struct rev_info *revs) sha1_to_hex(commit->object.sha1), ctx.qry.vpath, 0); show_commit_decorations(commit); html("</td><td>"); - cgit_open_filter(ctx.repo->email_filter, info->author_email, "log"); html_txt(info->author); - cgit_close_filter(ctx.repo->email_filter); if (revs->graph) { html("</td><td>"); @@ -290,64 +288,77 @@ void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern { struct rev_info rev; struct commit *commit; - struct argv_array rev_argv = ARGV_ARRAY_INIT; + struct vector vec = VECTOR_INIT(char *); int i, columns = commit_graph ? 4 : 3; int must_free_tip = 0; + struct strbuf argbuf = STRBUF_INIT; - /* rev_argv.argv[0] will be ignored by setup_revisions */ - argv_array_push(&rev_argv, "log_rev_setup"); + /* First argv is NULL */ + vector_push(&vec, NULL, 0); if (!tip) tip = ctx.qry.head; tip = disambiguate_ref(tip, &must_free_tip); - argv_array_push(&rev_argv, tip); + vector_push(&vec, &tip, 0); if (grep && pattern && *pattern) { pattern = xstrdup(pattern); if (!strcmp(grep, "grep") || !strcmp(grep, "author") || !strcmp(grep, "committer")) { - argv_array_pushf(&rev_argv, "--%s=%s", grep, pattern); - } else if (!strcmp(grep, "range")) { + strbuf_addf(&argbuf, "--%s=%s", grep, pattern); + vector_push(&vec, &argbuf.buf, 0); + } + if (!strcmp(grep, "range")) { char *arg; /* Split the pattern at whitespace and add each token * as a revision expression. Do not accept other * rev-list options. Also, replace the previously * pushed tip (it's no longer relevant). */ - argv_array_pop(&rev_argv); + vec.count--; while ((arg = next_token(&pattern))) { if (*arg == '-') { fprintf(stderr, "Bad range expr: %s\n", arg); break; } - argv_array_push(&rev_argv, arg); + vector_push(&vec, &arg, 0); } } } if (commit_graph) { - argv_array_push(&rev_argv, "--graph"); - argv_array_push(&rev_argv, "--color"); + static const char *graph_arg = "--graph"; + static const char *color_arg = "--color"; + vector_push(&vec, &graph_arg, 0); + vector_push(&vec, &color_arg, 0); graph_set_column_colors(column_colors_html, COLUMN_COLORS_HTML_MAX); } - if (commit_sort == 1) - argv_array_push(&rev_argv, "--date-order"); - else if (commit_sort == 2) - argv_array_push(&rev_argv, "--topo-order"); + if (commit_sort == 1) { + static const char *date_order_arg = "--date-order"; + vector_push(&vec, &date_order_arg, 0); + } else if (commit_sort == 2) { + static const char *topo_order_arg = "--topo-order"; + vector_push(&vec, &topo_order_arg, 0); + } if (path) { - argv_array_push(&rev_argv, "--"); - argv_array_push(&rev_argv, path); + static const char *double_dash_arg = "--"; + vector_push(&vec, &double_dash_arg, 0); + vector_push(&vec, &path, 0); } + /* Make sure the vector is NULL-terminated */ + vector_push(&vec, NULL, 0); + vec.count--; + init_revisions(&rev, NULL); rev.abbrev = DEFAULT_ABBREV; rev.commit_format = CMIT_FMT_DEFAULT; rev.verbose_header = 1; rev.show_root_diff = 0; - setup_revisions(rev_argv.argc, rev_argv.argv, &rev, NULL); + setup_revisions(vec.count, vec.data, &rev, NULL); load_ref_decorations(DECORATE_FULL_REFS); rev.show_decorations = 1; rev.grep_filter.regflags |= REG_ICASE; @@ -430,4 +441,5 @@ void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern /* If we allocated tip then it is safe to cast away const. */ if (must_free_tip) free((char*) tip); + strbuf_release(&argbuf); } diff --git a/ui-patch.c b/ui-patch.c index 6878a46..fbb92cc 100644 --- a/ui-patch.c +++ b/ui-patch.c @@ -1,6 +1,6 @@ /* ui-patch.c: generate patch view * - * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> + * Copyright (C) 2007 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -11,75 +11,127 @@ #include "html.h" #include "ui-shared.h" -void cgit_print_patch(const char *new_rev, const char *old_rev, - const char *prefix) +static void print_line(char *line, int len) +{ + char c = line[len-1]; + + line[len-1] = '\0'; + htmlf("%s\n", line); + line[len-1] = c; +} + +static void header(unsigned char *sha1, char *path1, int mode1, + unsigned char *sha2, char *path2, int mode2) +{ + char *abbrev1, *abbrev2; + int subproject; + + subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); + htmlf("diff --git a/%s b/%s\n", path1, path2); + + if (mode1 == 0) + htmlf("new file mode %.6o\n", mode2); + + if (mode2 == 0) + htmlf("deleted file mode %.6o\n", mode1); + + if (!subproject) { + abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); + abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); + htmlf("index %s..%s", abbrev1, abbrev2); + free(abbrev1); + free(abbrev2); + if (mode1 != 0 && mode2 != 0) { + htmlf(" %.6o", mode1); + if (mode2 != mode1) + htmlf("..%.6o", mode2); + } + + if (is_null_sha1(sha1)) { + path1 = "dev/null"; + htmlf("\n--- /%s\n", path1); + } else + htmlf("\n--- a/%s\n", path1); + + if (is_null_sha1(sha2)) { + path2 = "dev/null"; + htmlf("+++ /%s\n", path2); + } else + htmlf("+++ b/%s\n", path2); + } +} + +static void filepair_cb(struct diff_filepair *pair) +{ + unsigned long old_size = 0; + unsigned long new_size = 0; + int binary = 0; + + header(pair->one->sha1, pair->one->path, pair->one->mode, + pair->two->sha1, pair->two->path, pair->two->mode); + if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { + if (S_ISGITLINK(pair->one->mode)) + print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); + if (S_ISGITLINK(pair->two->mode)) + print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); + return; + } + if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, + &new_size, &binary, 0, 0, print_line)) + html("Error running diff"); + if (binary) + html("Binary files differ\n"); +} + +void cgit_print_patch(char *hex, const char *prefix) { - struct rev_info rev; struct commit *commit; - unsigned char new_rev_sha1[20], old_rev_sha1[20]; - char rev_range[2 * 40 + 3]; - char *rev_argv[] = { NULL, "--reverse", "--format=email", rev_range }; + struct commitinfo *info; + unsigned char sha1[20], old_sha1[20]; char *patchname; - if (!new_rev) - new_rev = ctx.qry.head; + if (!hex) + hex = ctx.qry.head; - if (get_sha1(new_rev, new_rev_sha1)) { - cgit_print_error("Bad object id: %s", new_rev); + if (get_sha1(hex, sha1)) { + cgit_print_error("Bad object id: %s", hex); return; } - commit = lookup_commit_reference(new_rev_sha1); + commit = lookup_commit_reference(sha1); if (!commit) { - cgit_print_error("Bad commit reference: %s", new_rev); + cgit_print_error("Bad commit reference: %s", hex); return; } + info = cgit_parse_commit(commit); - if (old_rev) { - if (get_sha1(old_rev, old_rev_sha1)) { - cgit_print_error("Bad object id: %s", old_rev); - return; - } - if (!lookup_commit_reference(old_rev_sha1)) { - cgit_print_error("Bad commit reference: %s", old_rev); - return; - } - } else if (commit->parents && commit->parents->item) { - hashcpy(old_rev_sha1, commit->parents->item->object.sha1); - } else { - hashclr(old_rev_sha1); - } + if (commit->parents && commit->parents->item) + hashcpy(old_sha1, commit->parents->item->object.sha1); + else + hashclr(old_sha1); - if (is_null_sha1(old_rev_sha1)) { - memcpy(rev_range, sha1_to_hex(new_rev_sha1), 41); - } else { - sprintf(rev_range, "%s..%s", sha1_to_hex(old_rev_sha1), - sha1_to_hex(new_rev_sha1)); - } - - patchname = fmt("%s.patch", rev_range); + patchname = fmt("%s.patch", sha1_to_hex(sha1)); ctx.page.mimetype = "text/plain"; ctx.page.filename = patchname; - cgit_print_http_headers(); - - if (ctx.cfg.noplainemail) { - rev_argv[2] = "--format=format:From %H Mon Sep 17 00:00:00 " - "2001%nFrom: %an%nDate: %aD%n%w(78,0,1)Subject: " - "%s%n%n%w(0)%b"; + cgit_print_http_headers(&ctx); + htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1)); + htmlf("From: %s", info->author); + if (!ctx.cfg.noplainemail) { + htmlf(" %s", info->author_email); } - - init_revisions(&rev, NULL); - rev.abbrev = DEFAULT_ABBREV; - rev.verbose_header = 1; - rev.diff = 1; - rev.show_root_diff = 1; - rev.max_parents = 1; - rev.diffopt.output_format |= DIFF_FORMAT_PATCH; - setup_revisions(ARRAY_SIZE(rev_argv), (const char **)rev_argv, &rev, - NULL); - prepare_revision_walk(&rev); - - while ((commit = get_revision(&rev)) != NULL) { - log_tree_commit(&rev, commit); - printf("-- \ncgit %s\n\n", cgit_version); + html("\n"); + html("Date: "); + cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n", ctx.cfg.local_time); + htmlf("Subject: %s\n\n", info->subject); + if (info->msg && *info->msg) { + htmlf("%s", info->msg); + if (info->msg[strlen(info->msg) - 1] != '\n') + html("\n"); } + html("---\n"); + if (prefix) + htmlf("(limited to '%s')\n\n", prefix); + cgit_diff_tree(old_sha1, sha1, filepair_cb, prefix, 0); + html("--\n"); + htmlf("cgit %s\n", cgit_version); + cgit_free_commitinfo(info); } diff --git a/ui-patch.h b/ui-patch.h index 7a6cacd..1641cea 100644 --- a/ui-patch.h +++ b/ui-patch.h @@ -1,7 +1,6 @@ #ifndef UI_PATCH_H #define UI_PATCH_H -extern void cgit_print_patch(const char *new_rev, const char *old_rev, - const char *prefix); +extern void cgit_print_patch(char *hex, const char *prefix); #endif /* UI_PATCH_H */ diff --git a/ui-plain.c b/ui-plain.c index 30fff89..9c86542 100644 --- a/ui-plain.c +++ b/ui-plain.c @@ -1,6 +1,6 @@ /* ui-plain.c: functions for output of plain blobs by path * - * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> + * Copyright (C) 2008 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -83,27 +83,22 @@ static int print_object(const unsigned char *sha1, const char *path) mime = string_list_lookup(&ctx.cfg.mimetypes, ext); if (mime) { ctx.page.mimetype = (char *)mime->util; - ctx.page.charset = NULL; } else { ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext); - if (ctx.page.mimetype) { + if (ctx.page.mimetype) freemime = 1; - ctx.page.charset = NULL; - } } } if (!ctx.page.mimetype) { - if (buffer_is_binary(buf, size)) { + if (buffer_is_binary(buf, size)) ctx.page.mimetype = "application/octet-stream"; - ctx.page.charset = NULL; - } else { + else ctx.page.mimetype = "text/plain"; - } } ctx.page.filename = path; ctx.page.size = size; ctx.page.etag = sha1_to_hex(sha1); - cgit_print_http_headers(); + cgit_print_http_headers(&ctx); html_raw(buf, size); /* If we allocated this, then casting away const is safe. */ if (freemime) @@ -128,7 +123,7 @@ static void print_dir(const unsigned char *sha1, const char *base, fullpath = buildpath(base, baselen, path); slash = (fullpath[0] == '/' ? "" : "/"); ctx.page.etag = sha1_to_hex(sha1); - cgit_print_http_headers(); + cgit_print_http_headers(&ctx); htmlf("<html><head><title>%s", slash); html_txt(fullpath); htmlf("\n\n

%s", slash); @@ -206,14 +201,14 @@ static int basedir_len(const char *path) return 0; } -void cgit_print_plain(void) +void cgit_print_plain(struct cgit_context *ctx) { - const char *rev = ctx.qry.sha1; + const char *rev = ctx->qry.sha1; unsigned char sha1[20]; struct commit *commit; struct pathspec_item path_items = { - .match = ctx.qry.path, - .len = ctx.qry.path ? strlen(ctx.qry.path) : 0 + .match = ctx->qry.path, + .len = ctx->qry.path ? strlen(ctx->qry.path) : 0 }; struct pathspec paths = { .nr = 1, @@ -224,7 +219,7 @@ void cgit_print_plain(void) }; if (!rev) - rev = ctx.qry.head; + rev = ctx->qry.head; if (get_sha1(rev, sha1)) { html_status(404, "Not found", 0); diff --git a/ui-plain.h b/ui-plain.h index 5bff07b..4373118 100644 --- a/ui-plain.h +++ b/ui-plain.h @@ -1,6 +1,6 @@ #ifndef UI_PLAIN_H #define UI_PLAIN_H -extern void cgit_print_plain(void); +extern void cgit_print_plain(struct cgit_context *ctx); #endif /* UI_PLAIN_H */ diff --git a/ui-refs.c b/ui-refs.c index 147b665..0ae0612 100644 --- a/ui-refs.c +++ b/ui-refs.c @@ -1,6 +1,6 @@ /* ui-refs.c: browse symbolic refs * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -77,9 +77,7 @@ static int print_branch(struct refinfo *ref) if (ref->object->type == OBJ_COMMIT) { cgit_commit_link(info->subject, NULL, NULL, name, NULL, NULL, 0); html(""); - cgit_open_filter(ctx.repo->email_filter, info->author_email, "refs"); html_txt(info->author); - cgit_close_filter(ctx.repo->email_filter); html(""); cgit_print_age(info->commit->date, -1, NULL); } else { @@ -156,15 +154,10 @@ static int print_tag(struct refinfo *ref) cgit_object_link(obj); html(""); if (info) { - if (info->tagger) { - cgit_open_filter(ctx.repo->email_filter, info->tagger_email, "refs"); - html_txt(info->tagger); - cgit_close_filter(ctx.repo->email_filter); - } + if (info->tagger) + html(info->tagger); } else if (ref->object->type == OBJ_COMMIT) { - cgit_open_filter(ctx.repo->email_filter, ref->commit->author_email, "refs"); - html_txt(ref->commit->author); - cgit_close_filter(ctx.repo->email_filter); + html(ref->commit->author); } html(""); if (info) { @@ -247,9 +240,9 @@ void cgit_print_refs() html(""); - if (ctx.qry.path && !prefixcmp(ctx.qry.path, "heads")) + if (ctx.qry.path && !strncmp(ctx.qry.path, "heads", 5)) cgit_print_branches(0); - else if (ctx.qry.path && !prefixcmp(ctx.qry.path, "tags")) + else if (ctx.qry.path && !strncmp(ctx.qry.path, "tags", 4)) cgit_print_tags(0); else { cgit_print_branches(0); diff --git a/ui-repolist.c b/ui-repolist.c index 477a949..2ab6e9e 100644 --- a/ui-repolist.c +++ b/ui-repolist.c @@ -1,6 +1,7 @@ /* ui-repolist.c: functions for generating the repolist page * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006 Lars Hjemli + * Copyright (C) 2012 Jason A. Donenfeld * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -106,9 +107,7 @@ static int is_in_url(struct cgit_repo *repo) static void print_sort_header(const char *title, const char *sort) { - html(""); htmlf(""); htmlf("
"); html_txt(ctx.repo->owner); - html(""); html(""); } print_modtime(ctx.repo); @@ -339,7 +332,13 @@ void cgit_print_site_readme() { if (!ctx.cfg.root_readme) return; - cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme); + if (ctx.cfg.about_filter) { + ctx.cfg.about_filter->argv[1] = ctx.cfg.root_readme; + cgit_open_filter(ctx.cfg.about_filter); + } html_include(ctx.cfg.root_readme); - cgit_close_filter(ctx.cfg.about_filter); + if (ctx.cfg.about_filter) { + cgit_close_filter(ctx.cfg.about_filter); + ctx.cfg.about_filter->argv[1] = NULL; + } } diff --git a/ui-shared.c b/ui-shared.c index 1ede2b0..7ab2ab1 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -1,6 +1,6 @@ /* ui-shared.c: common web output functions * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -73,14 +73,6 @@ const char *cgit_rooturl() return ctx.cfg.script_name; } -const char *cgit_loginurl() -{ - static const char *login_url = 0; - if (!login_url) - login_url = fmtalloc("%s?p=login", cgit_rooturl()); - return login_url; -} - char *cgit_repourl(const char *reponame) { if (ctx.cfg.virtual_root) @@ -128,7 +120,7 @@ const char *cgit_repobasename(const char *reponame) /* strip trailing slashes */ while (p && rvbuf[p] == '/') rvbuf[p--] = 0; /* strip trailing .git */ - if (p >= 3 && !prefixcmp(&rvbuf[p-3], ".git")) { + if (p >= 3 && !strncmp(&rvbuf[p-3], ".git", 4)) { p -= 3; rvbuf[p--] = 0; } /* strip more trailing slashes if any */ @@ -147,7 +139,7 @@ static void site_url(const char *page, const char *search, const char *sort, int if (ctx.cfg.virtual_root) html_attr(ctx.cfg.virtual_root); else - html_url_path(ctx.cfg.script_name); + html(ctx.cfg.script_name); if (page) { htmlf("?p=%s", page); @@ -227,7 +219,7 @@ static char *repolink(const char *title, const char *class, const char *page, html_url_path(path); } } else { - html_url_path(ctx.cfg.script_name); + html(ctx.cfg.script_name); html("?url="); html_url_arg(ctx.repo->url); if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') @@ -436,58 +428,59 @@ void cgit_stats_link(const char *name, const char *title, const char *class, reporevlink("stats", name, title, class, head, NULL, path); } -static void cgit_self_link(char *name, const char *title, const char *class) -{ - if (!strcmp(ctx.qry.page, "repolist")) - cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort, - ctx.qry.ofs); - else if (!strcmp(ctx.qry.page, "summary")) - cgit_summary_link(name, title, class, ctx.qry.head); - else if (!strcmp(ctx.qry.page, "tag")) - cgit_tag_link(name, title, class, ctx.qry.head, - ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL); - else if (!strcmp(ctx.qry.page, "tree")) - cgit_tree_link(name, title, class, ctx.qry.head, - ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL, - ctx.qry.path); - else if (!strcmp(ctx.qry.page, "plain")) - cgit_plain_link(name, title, class, ctx.qry.head, - ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL, - ctx.qry.path); - else if (!strcmp(ctx.qry.page, "log")) - cgit_log_link(name, title, class, ctx.qry.head, - ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL, - ctx.qry.path, ctx.qry.ofs, - ctx.qry.grep, ctx.qry.search, - ctx.qry.showmsg); - else if (!strcmp(ctx.qry.page, "commit")) - cgit_commit_link(name, title, class, ctx.qry.head, - ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL, - ctx.qry.path, 0); - else if (!strcmp(ctx.qry.page, "patch")) - cgit_patch_link(name, title, class, ctx.qry.head, - ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL, - ctx.qry.path); - else if (!strcmp(ctx.qry.page, "refs")) - cgit_refs_link(name, title, class, ctx.qry.head, - ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL, - ctx.qry.path); - else if (!strcmp(ctx.qry.page, "snapshot")) - cgit_snapshot_link(name, title, class, ctx.qry.head, - ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL, - ctx.qry.path); - else if (!strcmp(ctx.qry.page, "diff")) - cgit_diff_link(name, title, class, ctx.qry.head, - ctx.qry.sha1, ctx.qry.sha2, - ctx.qry.path, 0); - else if (!strcmp(ctx.qry.page, "stats")) - cgit_stats_link(name, title, class, ctx.qry.head, - ctx.qry.path); +static void cgit_self_link(char *name, const char *title, const char *class, + struct cgit_context *ctx) +{ + if (!strcmp(ctx->qry.page, "repolist")) + cgit_index_link(name, title, class, ctx->qry.search, ctx->qry.sort, + ctx->qry.ofs); + else if (!strcmp(ctx->qry.page, "summary")) + cgit_summary_link(name, title, class, ctx->qry.head); + else if (!strcmp(ctx->qry.page, "tag")) + cgit_tag_link(name, title, class, ctx->qry.head, + ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL); + else if (!strcmp(ctx->qry.page, "tree")) + cgit_tree_link(name, title, class, ctx->qry.head, + ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL, + ctx->qry.path); + else if (!strcmp(ctx->qry.page, "plain")) + cgit_plain_link(name, title, class, ctx->qry.head, + ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL, + ctx->qry.path); + else if (!strcmp(ctx->qry.page, "log")) + cgit_log_link(name, title, class, ctx->qry.head, + ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL, + ctx->qry.path, ctx->qry.ofs, + ctx->qry.grep, ctx->qry.search, + ctx->qry.showmsg); + else if (!strcmp(ctx->qry.page, "commit")) + cgit_commit_link(name, title, class, ctx->qry.head, + ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL, + ctx->qry.path, 0); + else if (!strcmp(ctx->qry.page, "patch")) + cgit_patch_link(name, title, class, ctx->qry.head, + ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL, + ctx->qry.path); + else if (!strcmp(ctx->qry.page, "refs")) + cgit_refs_link(name, title, class, ctx->qry.head, + ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL, + ctx->qry.path); + else if (!strcmp(ctx->qry.page, "snapshot")) + cgit_snapshot_link(name, title, class, ctx->qry.head, + ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL, + ctx->qry.path); + else if (!strcmp(ctx->qry.page, "diff")) + cgit_diff_link(name, title, class, ctx->qry.head, + ctx->qry.sha1, ctx->qry.sha2, + ctx->qry.path, 0); + else if (!strcmp(ctx->qry.page, "stats")) + cgit_stats_link(name, title, class, ctx->qry.head, + ctx->qry.path); else { /* Don't known how to make link for this page */ - repolink(title, class, ctx.qry.page, ctx.qry.head, ctx.qry.path); + repolink(title, class, ctx->qry.page, ctx->qry.head, ctx->qry.path); html(">"); html_txt(name); html(""); @@ -596,8 +589,6 @@ void cgit_print_age(time_t t, time_t max_relative, const char *format) return; time(&now); secs = now - t; - if (secs < 0) - secs = 0; if (secs > max_relative && max_relative >= 0) { cgit_print_date(t, format, ctx.cfg.local_time); @@ -633,39 +624,37 @@ void cgit_print_age(time_t t, time_t max_relative, const char *format) secs * 1.0 / TM_YEAR); } -void cgit_print_http_headers(void) +void cgit_print_http_headers(struct cgit_context *ctx) { - if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1")) + if (ctx->env.no_http && !strcmp(ctx->env.no_http, "1")) return; - if (ctx.page.status) - htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg); - if (ctx.page.mimetype && ctx.page.charset) - htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype, - ctx.page.charset); - else if (ctx.page.mimetype) - htmlf("Content-Type: %s\n", ctx.page.mimetype); - if (ctx.page.size) - htmlf("Content-Length: %zd\n", ctx.page.size); - if (ctx.page.filename) + if (ctx->page.status) + htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg); + if (ctx->page.mimetype && ctx->page.charset) + htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, + ctx->page.charset); + else if (ctx->page.mimetype) + htmlf("Content-Type: %s\n", ctx->page.mimetype); + if (ctx->page.size) + htmlf("Content-Length: %zd\n", ctx->page.size); + if (ctx->page.filename) htmlf("Content-Disposition: inline; filename=\"%s\"\n", - ctx.page.filename); - if (!ctx.env.authenticated) - html("Cache-Control: no-cache, no-store\n"); - htmlf("Last-Modified: %s\n", http_date(ctx.page.modified)); - htmlf("Expires: %s\n", http_date(ctx.page.expires)); - if (ctx.page.etag) - htmlf("ETag: \"%s\"\n", ctx.page.etag); + ctx->page.filename); + htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); + htmlf("Expires: %s\n", http_date(ctx->page.expires)); + if (ctx->page.etag) + htmlf("ETag: \"%s\"\n", ctx->page.etag); html("\n"); - if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD")) + if (ctx->env.request_method && !strcmp(ctx->env.request_method, "HEAD")) exit(0); } -void cgit_print_docstart(void) +void cgit_print_docstart(struct cgit_context *ctx) { - if (ctx.cfg.embedded) { - if (ctx.cfg.header) - html_include(ctx.cfg.header); + if (ctx->cfg.embedded) { + if (ctx->cfg.header) + html_include(ctx->cfg.header); return; } @@ -674,37 +663,37 @@ void cgit_print_docstart(void) html("\n"); html("\n"); html(""); - html_txt(ctx.page.title); + html_txt(ctx->page.title); html("\n"); htmlf("\n", cgit_version); - if (ctx.cfg.robots && *ctx.cfg.robots) - htmlf("\n", ctx.cfg.robots); + if (ctx->cfg.robots && *ctx->cfg.robots) + htmlf("\n", ctx->cfg.robots); html("\n"); - if (ctx.cfg.favicon) { + if (ctx->cfg.favicon) { html("\n"); } - if (host && ctx.repo && ctx.qry.head) { + if (host && ctx->repo && ctx->qry.head) { struct strbuf sb = STRBUF_INIT; - strbuf_addf(&sb, "h=%s", ctx.qry.head); + strbuf_addf(&sb, "h=%s", ctx->qry.head); html("\n"); strbuf_release(&sb); } - if (ctx.cfg.head_include) - html_include(ctx.cfg.head_include); + if (ctx->cfg.head_include) + html_include(ctx->cfg.head_include); html("\n"); html("\n"); - if (ctx.cfg.header) - html_include(ctx.cfg.header); + if (ctx->cfg.header) + html_include(ctx->cfg.header); } void cgit_print_docend() @@ -768,47 +757,47 @@ void cgit_add_hidden_formfields(int incl_head, int incl_search, } } -static const char *hc(const char *page) +static const char *hc(struct cgit_context *ctx, const char *page) { - return strcmp(ctx.qry.page, page) ? NULL : "active"; + return strcmp(ctx->qry.page, page) ? NULL : "active"; } -static void cgit_print_path_crumbs(char *path) +static void cgit_print_path_crumbs(struct cgit_context *ctx, char *path) { - char *old_path = ctx.qry.path; + char *old_path = ctx->qry.path; char *p = path, *q, *end = path + strlen(path); - ctx.qry.path = NULL; - cgit_self_link("root", NULL, NULL); - ctx.qry.path = p = path; + ctx->qry.path = NULL; + cgit_self_link("root", NULL, NULL, ctx); + ctx->qry.path = p = path; while (p < end) { if (!(q = strchr(p, '/'))) q = end; *q = '\0'; html_txt("/"); - cgit_self_link(p, NULL, NULL); + cgit_self_link(p, NULL, NULL, ctx); if (q < end) *q = '/'; p = q + 1; } - ctx.qry.path = old_path; + ctx->qry.path = old_path; } -static void print_header(void) +static void print_header(struct cgit_context *ctx) { char *logo = NULL, *logo_link = NULL; html("\n"); html("\n"); - if (ctx.repo && ctx.repo->logo && *ctx.repo->logo) - logo = ctx.repo->logo; + if (ctx->repo && ctx->repo->logo && *ctx->repo->logo) + logo = ctx->repo->logo; else - logo = ctx.cfg.logo; - if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link) - logo_link = ctx.repo->logo_link; + logo = ctx->cfg.logo; + if (ctx->repo && ctx->repo->logo_link && *ctx->repo->logo_link) + logo_link = ctx->repo->logo_link; else - logo_link = ctx.cfg.logo_link; + logo_link = ctx->cfg.logo_link; if (logo && *logo) { html("\n"); html("\n"); } -void cgit_print_pageheader(void) +void cgit_print_pageheader(struct cgit_context *ctx) { html("
"); - if (!ctx.env.authenticated || !ctx.cfg.noheader) - print_header(); + if (!ctx->cfg.noheader) + print_header(ctx); html("
\n"); - if (ctx.env.authenticated && ctx.repo) { - if (ctx.repo->readme.nr) + if (ctx->repo) { + cgit_summary_link("summary", NULL, hc(ctx, "summary"), + ctx->qry.head); + cgit_refs_link("refs", NULL, hc(ctx, "refs"), ctx->qry.head, + ctx->qry.sha1, NULL); + cgit_log_link("log", NULL, hc(ctx, "log"), ctx->qry.head, + NULL, ctx->qry.vpath, 0, NULL, NULL, + ctx->qry.showmsg); + cgit_tree_link("tree", NULL, hc(ctx, "tree"), ctx->qry.head, + ctx->qry.sha1, ctx->qry.vpath); + cgit_commit_link("commit", NULL, hc(ctx, "commit"), + ctx->qry.head, ctx->qry.sha1, ctx->qry.vpath, 0); + cgit_diff_link("diff", NULL, hc(ctx, "diff"), ctx->qry.head, + ctx->qry.sha1, ctx->qry.sha2, ctx->qry.vpath, 0); + if (ctx->repo->max_stats) + cgit_stats_link("stats", NULL, hc(ctx, "stats"), + ctx->qry.head, ctx->qry.vpath); + if (ctx->repo->readme.nr) reporevlink("about", "about", NULL, - hc("about"), ctx.qry.head, NULL, + hc(ctx, "about"), ctx->qry.head, NULL, NULL); - cgit_summary_link("summary", NULL, hc("summary"), - ctx.qry.head); - cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head, - ctx.qry.sha1, NULL); - cgit_log_link("log", NULL, hc("log"), ctx.qry.head, - NULL, ctx.qry.vpath, 0, NULL, NULL, - ctx.qry.showmsg); - cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head, - ctx.qry.sha1, ctx.qry.vpath); - cgit_commit_link("commit", NULL, hc("commit"), - ctx.qry.head, ctx.qry.sha1, ctx.qry.vpath, 0); - cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head, - ctx.qry.sha1, ctx.qry.sha2, ctx.qry.vpath, 0); - if (ctx.repo->max_stats) - cgit_stats_link("stats", NULL, hc("stats"), - ctx.qry.head, ctx.qry.vpath); html(""); html("
\n"); cgit_add_hidden_formfields(1, 0, "log"); html("\n"); html("\n"); html("\n"); html("
\n"); - } else if (ctx.env.authenticated) { - site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0); - if (ctx.cfg.root_readme) - site_link("about", "about", NULL, hc("about"), + } else { + site_link(NULL, "index", NULL, hc(ctx, "repolist"), NULL, NULL, 0); + if (ctx->cfg.root_readme) + site_link("about", "about", NULL, hc(ctx, "about"), NULL, NULL, 0); html("
"); html("
\n"); html("\n"); html("\n"); html("
"); } html("
\n"); - if (ctx.env.authenticated && ctx.qry.vpath) { + if (ctx->qry.vpath) { html("
"); html("path: "); - cgit_print_path_crumbs(ctx.qry.vpath); + cgit_print_path_crumbs(ctx, ctx->qry.vpath); html("
"); } html("
"); diff --git a/ui-shared.h b/ui-shared.h index 3e7a91b..5987e77 100644 --- a/ui-shared.h +++ b/ui-shared.h @@ -4,7 +4,6 @@ extern const char *cgit_httpscheme(); extern const char *cgit_hosturl(); extern const char *cgit_rooturl(); -extern const char *cgit_loginurl(); extern char *cgit_repourl(const char *reponame); extern char *cgit_fileurl(const char *reponame, const char *pagename, const char *filename, const char *query); @@ -59,10 +58,10 @@ __attribute__((format (printf,1,0))) extern void cgit_vprint_error(const char *fmt, va_list ap); extern void cgit_print_date(time_t secs, const char *format, int local_time); extern void cgit_print_age(time_t t, time_t max_relative, const char *format); -extern void cgit_print_http_headers(void); -extern void cgit_print_docstart(void); +extern void cgit_print_http_headers(struct cgit_context *ctx); +extern void cgit_print_docstart(struct cgit_context *ctx); extern void cgit_print_docend(); -extern void cgit_print_pageheader(void); +extern void cgit_print_pageheader(struct cgit_context *ctx); extern void cgit_print_filemode(unsigned short mode); extern void cgit_print_snapshot_links(const char *repo, const char *head, const char *hex, int snapshots); diff --git a/ui-snapshot.c b/ui-snapshot.c index 582dc31..42b7489 100644 --- a/ui-snapshot.c +++ b/ui-snapshot.c @@ -1,6 +1,7 @@ /* ui-snapshot.c: generate snapshot of a commit * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006 Lars Hjemli + * Copyright (C) 2012 Jason A. Donenfeld * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -58,12 +59,13 @@ static int write_compressed_tar_archive(const char *hex, char *filter_argv[]) { int rv; - struct cgit_exec_filter f; - cgit_exec_filter_init(&f, filter_argv[0], filter_argv); + struct cgit_filter f; - cgit_open_filter(&f.base); + f.cmd = filter_argv[0]; + f.argv = filter_argv; + cgit_open_filter(&f); rv = write_tar_archive(hex, prefix); - cgit_close_filter(&f.base); + cgit_close_filter(&f); return rv; } @@ -97,9 +99,14 @@ const struct cgit_snapshot_format cgit_snapshot_formats[] = { static const struct cgit_snapshot_format *get_format(const char *filename) { const struct cgit_snapshot_format *fmt; + int fl, sl; + fl = strlen(filename); for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) { - if (!suffixcmp(filename, fmt->suffix)) + sl = strlen(fmt->suffix); + if (sl >= fl) + continue; + if (!strcmp(fmt->suffix, filename + fl - sl)) return fmt; } return NULL; @@ -121,7 +128,7 @@ static int make_snapshot(const struct cgit_snapshot_format *format, } ctx.page.mimetype = xstrdup(format->mimetype); ctx.page.filename = xstrdup(filename); - cgit_print_http_headers(); + cgit_print_http_headers(&ctx); format->write_func(hex, prefix); return 0; } @@ -183,9 +190,9 @@ static void show_error(char *fmt, ...) va_list ap; ctx.page.mimetype = "text/html"; - cgit_print_http_headers(); - cgit_print_docstart(); - cgit_print_pageheader(); + cgit_print_http_headers(&ctx); + cgit_print_docstart(&ctx); + cgit_print_pageheader(&ctx); va_start(ap, fmt); cgit_vprint_error(fmt, ap); va_end(ap); diff --git a/ui-ssdiff.c b/ui-ssdiff.c index 08cf513..cbe60bd 100644 --- a/ui-ssdiff.c +++ b/ui-ssdiff.c @@ -230,9 +230,9 @@ static void print_ssdiff_line(char *class, struct diff_filespec *old_file = cgit_get_current_old_file(); char *lineno_str = fmt("n%d", old_line_no); char *id_str = fmt("id=%s#%s", is_null_sha1(old_file->sha1)?"HEAD":sha1_to_hex(old_rev_sha1), lineno_str); - html("
%s", lineno_str, lineno_str + 1); + htmlf("' id='%s' name='%s'>%s", lineno_str, lineno_str, lineno_str + 1); html("", class); } else if (old_line) @@ -251,9 +251,9 @@ static void print_ssdiff_line(char *class, struct diff_filespec *new_file = cgit_get_current_new_file(); char *lineno_str = fmt("n%d", new_line_no); char *id_str = fmt("id=%s#%s", is_null_sha1(new_file->sha1)?"HEAD":sha1_to_hex(new_rev_sha1), lineno_str); - html("%s", lineno_str, lineno_str + 1); + htmlf("' id='%s' name='%s'>%s", lineno_str, lineno_str, lineno_str + 1); html("", class); } else if (new_line) diff --git a/ui-stats.c b/ui-stats.c index bc27308..28b794f 100644 --- a/ui-stats.c +++ b/ui-stats.c @@ -9,6 +9,8 @@ #define SZ_FMT "%zu" #endif +#define MONTHS 6 + struct authorstat { long total; struct string_list list; @@ -209,12 +211,13 @@ static int cmp_total_commits(const void *a1, const void *a2) /* Walk the commit DAG and collect number of commits per author per * timeperiod into a nested string_list collection. */ -static struct string_list collect_stats(struct cgit_period *period) +static struct string_list collect_stats(struct cgit_context *ctx, + struct cgit_period *period) { struct string_list authors; struct rev_info rev; struct commit *commit; - const char *argv[] = {NULL, ctx.qry.head, NULL, NULL, NULL, NULL}; + const char *argv[] = {NULL, ctx->qry.head, NULL, NULL, NULL, NULL}; int argc = 3; time_t now; long i; @@ -228,9 +231,9 @@ static struct string_list collect_stats(struct cgit_period *period) period->dec(tm); strftime(tmp, sizeof(tmp), "%Y-%m-%d", tm); argv[2] = xstrdup(fmt("--since=%s", tmp)); - if (ctx.qry.path) { + if (ctx->qry.path) { argv[3] = "--"; - argv[4] = ctx.qry.path; + argv[4] = ctx->qry.path; argc += 2; } init_revisions(&rev, NULL); @@ -359,30 +362,30 @@ static void print_authors(struct string_list *authors, int top, * for each author is another string_list which is used to calculate the * number of commits per time-interval. */ -void cgit_show_stats(void) +void cgit_show_stats(struct cgit_context *ctx) { struct string_list authors; struct cgit_period *period; int top, i; const char *code = "w"; - if (ctx.qry.period) - code = ctx.qry.period; + if (ctx->qry.period) + code = ctx->qry.period; i = cgit_find_stats_period(code, &period); if (!i) { cgit_print_error("Unknown statistics type: %c", code[0]); return; } - if (i > ctx.repo->max_stats) { + if (i > ctx->repo->max_stats) { cgit_print_error("Statistics type disabled: %s", period->name); return; } - authors = collect_stats(period); + authors = collect_stats(ctx, period); qsort(authors.items, authors.nr, sizeof(struct string_list_item), cmp_total_commits); - top = ctx.qry.ofs; + top = ctx->qry.ofs; if (!top) top = 10; @@ -391,10 +394,10 @@ void cgit_show_stats(void) html("
"); cgit_add_hidden_formfields(1, 0, "stats"); html(""); - if (ctx.repo->max_stats > 1) { + if (ctx->repo->max_stats > 1) { html(""); html(""); @@ -413,9 +416,9 @@ void cgit_show_stats(void) html(""); html(""); htmlf("

Commits per author per %s", period->name); - if (ctx.qry.path) { + if (ctx->qry.path) { html(" (path '"); - html_txt(ctx.qry.path); + html_txt(ctx->qry.path); html("')"); } html("

"); diff --git a/ui-stats.h b/ui-stats.h index 341ab13..f0761ba 100644 --- a/ui-stats.h +++ b/ui-stats.h @@ -23,6 +23,6 @@ struct cgit_period { extern int cgit_find_stats_period(const char *expr, struct cgit_period **period); extern const char *cgit_find_stats_periodname(int idx); -extern void cgit_show_stats(void); +extern void cgit_show_stats(struct cgit_context *ctx); #endif /* UI_STATS_H */ diff --git a/ui-summary.c b/ui-summary.c index ddd8f1b..d8500d6 100644 --- a/ui-summary.c +++ b/ui-summary.c @@ -1,6 +1,7 @@ /* ui-summary.c: functions for generating repo summary page * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006 Lars Hjemli + * Copyright (C) 2010-2013 Jason A. Donenfeld * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -116,7 +117,7 @@ static char* append_readme_path(const char *filename, const char *ref, const cha if (!ref) { resolved_base = realpath(base_dir, NULL); resolved_full = realpath(full_path, NULL); - if (!resolved_base || !resolved_full || prefixcmp(resolved_full, resolved_base)) { + if (!resolved_base || !resolved_full || strncmp(resolved_base, resolved_full, strlen(resolved_base))) { free(full_path); full_path = NULL; } @@ -151,13 +152,19 @@ void cgit_print_repo_readme(char *path) * filesystem, while applying the about-filter. */ html("
"); - cgit_open_filter(ctx.repo->about_filter, filename); + if (ctx.repo->about_filter) { + ctx.repo->about_filter->argv[1] = filename; + cgit_open_filter(ctx.repo->about_filter); + } if (ref) cgit_print_file(filename, ref, 1); else html_include(filename); - cgit_close_filter(ctx.repo->about_filter); - + if (ctx.repo->about_filter) { + cgit_close_filter(ctx.repo->about_filter); + ctx.repo->about_filter->argv[1] = NULL; + free(ref); + } html("
"); if (free_filename) free(filename); diff --git a/ui-tag.c b/ui-tag.c index c1d1738..aea7958 100644 --- a/ui-tag.c +++ b/ui-tag.c @@ -1,6 +1,6 @@ /* ui-tag.c: display a tag * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2007 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -77,13 +77,11 @@ void cgit_print_tag(char *revname) } if (info->tagger) { html("
\n"); } html("
Period:
tagged by"); - cgit_open_filter(ctx.repo->email_filter, info->tagger_email, "tag"); html_txt(info->tagger); if (info->tagger_email && !ctx.cfg.noplainemail) { html(" "); html_txt(info->tagger_email); } - cgit_close_filter(ctx.repo->email_filter); html("
tagged object"); diff --git a/ui-tree.c b/ui-tree.c index e4c3d22..aa5dee9 100644 --- a/ui-tree.c +++ b/ui-tree.c @@ -1,6 +1,6 @@ /* ui-tree.c: functions for tree output * - * Copyright (C) 2006-2014 cgit Development Team + * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) @@ -21,7 +21,8 @@ struct walk_tree_context { static void print_text_buffer(const char *name, char *buf, unsigned long size) { unsigned long lineno, idx; - const char *numberfmt = "%1$d\n"; + const char *numberfmt = + "%1$d\n"; html("\n"); @@ -45,12 +46,13 @@ static void print_text_buffer(const char *name, char *buf, unsigned long size) } if (ctx.repo->source_filter) { - char *filter_arg = xstrdup(name); html("
");
-		cgit_open_filter(ctx.repo->source_filter, filter_arg);
+		ctx.repo->source_filter->argv[1] = xstrdup(name);
+		cgit_open_filter(ctx.repo->source_filter);
 		html_raw(buf, size);
 		cgit_close_filter(ctx.repo->source_filter);
-		free(filter_arg);
+		free(ctx.repo->source_filter->argv[1]);
+		ctx.repo->source_filter->argv[1] = NULL;
 		html("
\n"); return; } diff --git a/vector.c b/vector.c new file mode 100644 index 0000000..0863908 --- /dev/null +++ b/vector.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include "vector.h" + +static int grow(struct vector *vec, int gently) +{ + size_t new_alloc; + void *new_data; + + new_alloc = vec->alloc * 3 / 2; + if (!new_alloc) + new_alloc = 8; + new_data = realloc(vec->data, new_alloc * vec->size); + if (!new_data) { + if (gently) + return ENOMEM; + perror("vector.c:grow()"); + exit(1); + } + vec->data = new_data; + vec->alloc = new_alloc; + return 0; +} + +int vector_push(struct vector *vec, const void *data, int gently) +{ + int rc; + + if (vec->count == vec->alloc && (rc = grow(vec, gently))) + return rc; + if (data) + memmove(vec->data + vec->count * vec->size, data, vec->size); + else + memset(vec->data + vec->count * vec->size, 0, vec->size); + vec->count++; + return 0; +} diff --git a/vector.h b/vector.h new file mode 100644 index 0000000..c64eb1f --- /dev/null +++ b/vector.h @@ -0,0 +1,17 @@ +#ifndef CGIT_VECTOR_H +#define CGIT_VECTOR_H + +#include + +struct vector { + size_t size; + size_t count; + size_t alloc; + void *data; +}; + +#define VECTOR_INIT(type) {sizeof(type), 0, 0, NULL} + +int vector_push(struct vector *vec, const void *data, int gently); + +#endif /* CGIT_VECTOR_H */