]> git.cameronkatri.com Git - cgit.git/blobdiff - cgit.c
tests: handle paths with whitespace
[cgit.git] / cgit.c
diff --git a/cgit.c b/cgit.c
index fde0757416798d90808809e5b434384d5261f6ed..435ce5a538d95ebec0c5a1397c5b9bdba00d86b8 100644 (file)
--- a/cgit.c
+++ b/cgit.c
@@ -1,6 +1,7 @@
 /* cgit.c: cgi for the git scm
  *
  * Copyright (C) 2006 Lars Hjemli
+ * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com>
  *
  * Licensed under GNU General Public License v2
  *   (see COPYING for full license text)
@@ -21,22 +22,24 @@ void add_mimetype(const char *name, const char *value)
 {
        struct string_list_item *item;
 
-       item = string_list_insert(xstrdup(name), &ctx.cfg.mimetypes);
+       item = string_list_insert(&ctx.cfg.mimetypes, xstrdup(name));
        item->util = xstrdup(value);
 }
 
 struct cgit_filter *new_filter(const char *cmd, int extra_args)
 {
        struct cgit_filter *f;
+       int args_size = 0;
 
        if (!cmd || !cmd[0])
                return NULL;
 
        f = xmalloc(sizeof(struct cgit_filter));
        f->cmd = xstrdup(cmd);
-       f->argv = xmalloc((2 + extra_args) * sizeof(char *));
+       args_size = (2 + extra_args) * sizeof(char *);
+       f->argv = xmalloc(args_size);
+       memset(f->argv, 0, args_size);
        f->argv[0] = f->cmd;
-       f->argv[1] = NULL;
        return f;
 }
 
@@ -56,6 +59,8 @@ void repo_config(struct cgit_repo *repo, const char *name, const char *value)
                repo->defbranch = xstrdup(value);
        else if (!strcmp(name, "snapshots"))
                repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value);
+       else if (!strcmp(name, "enable-commit-graph"))
+               repo->enable_commit_graph = ctx.cfg.enable_commit_graph * atoi(value);
        else if (!strcmp(name, "enable-log-filecount"))
                repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
        else if (!strcmp(name, "enable-log-linecount"))
@@ -70,12 +75,13 @@ void repo_config(struct cgit_repo *repo, const char *name, const char *value)
                repo->module_link= xstrdup(value);
        else if (!strcmp(name, "section"))
                repo->section = xstrdup(value);
-       else if (!strcmp(name, "readme") && value != NULL) {
-               if (*value == '/')
-                       repo->readme = xstrdup(value);
-               else
-                       repo->readme = xstrdup(fmt("%s/%s", repo->path, value));
-       } else if (ctx.cfg.enable_filter_overrides) {
+       else if (!strcmp(name, "readme") && value != NULL)
+               repo->readme = xstrdup(value);
+       else if (!strcmp(name, "logo") && value != NULL)
+               repo->logo = xstrdup(value);
+       else if (!strcmp(name, "logo-link") && value != NULL)
+               repo->logo_link = xstrdup(value);
+       else if (ctx.cfg.enable_filter_overrides) {
                if (!strcmp(name, "about-filter"))
                        repo->about_filter = new_filter(value, 0);
                else if (!strcmp(name, "commit-filter"))
@@ -95,6 +101,8 @@ void config_cb(const char *name, const char *value)
                ctx.repo->path = trim_end(value, '/');
        else if (ctx.repo && !prefixcmp(name, "repo."))
                repo_config(ctx.repo, name + 5, value);
+       else if (!strcmp(name, "readme"))
+               ctx.cfg.readme = xstrdup(value);
        else if (!strcmp(name, "root-title"))
                ctx.cfg.root_title = xstrdup(value);
        else if (!strcmp(name, "root-desc"))
@@ -121,6 +129,8 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.logo_link = xstrdup(value);
        else if (!strcmp(name, "module-link"))
                ctx.cfg.module_link = xstrdup(value);
+       else if (!strcmp(name, "strict-export"))
+               ctx.cfg.strict_export = xstrdup(value);
        else if (!strcmp(name, "virtual-root")) {
                ctx.cfg.virtual_root = trim_end(value, '/');
                if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
@@ -135,8 +145,12 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
        else if (!strcmp(name, "enable-filter-overrides"))
                ctx.cfg.enable_filter_overrides = atoi(value);
+       else if (!strcmp(name, "enable-gitweb-owner"))
+               ctx.cfg.enable_gitweb_owner = atoi(value);
        else if (!strcmp(name, "enable-index-links"))
                ctx.cfg.enable_index_links = atoi(value);
+       else if (!strcmp(name, "enable-commit-graph"))
+               ctx.cfg.enable_commit_graph = atoi(value);
        else if (!strcmp(name, "enable-log-filecount"))
                ctx.cfg.enable_log_filecount = atoi(value);
        else if (!strcmp(name, "enable-log-linecount"))
@@ -181,11 +195,20 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.max_repo_count = atoi(value);
        else if (!strcmp(name, "max-commit-count"))
                ctx.cfg.max_commit_count = atoi(value);
+       else if (!strcmp(name, "project-list"))
+               ctx.cfg.project_list = xstrdup(expand_macros(value));
        else if (!strcmp(name, "scan-path"))
                if (!ctx.cfg.nocache && ctx.cfg.cache_size)
                        process_cached_repolist(expand_macros(value));
+               else if (ctx.cfg.project_list)
+                       scan_projects(expand_macros(value),
+                                     ctx.cfg.project_list, repo_config);
                else
                        scan_tree(expand_macros(value), repo_config);
+       else if (!strcmp(name, "scan-hidden-path"))
+               ctx.cfg.scan_hidden_path = atoi(value);
+       else if (!strcmp(name, "section-from-path"))
+               ctx.cfg.section_from_path = atoi(value);
        else if (!strcmp(name, "source-filter"))
                ctx.cfg.source_filter = new_filter(value, 1);
        else if (!strcmp(name, "summary-log"))
@@ -200,6 +223,8 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.agefile = xstrdup(value);
        else if (!strcmp(name, "renamelimit"))
                ctx.cfg.renamelimit = atoi(value);
+       else if (!strcmp(name, "remove-suffix"))
+               ctx.cfg.remove_suffix = atoi(value);
        else if (!strcmp(name, "robots"))
                ctx.cfg.robots = xstrdup(value);
        else if (!strcmp(name, "clone-prefix"))
@@ -256,10 +281,13 @@ static void querystring_cb(const char *name, const char *value)
                ctx.qry.period = xstrdup(value);
        } else if (!strcmp(name, "ss")) {
                ctx.qry.ssdiff = atoi(value);
+               ctx.qry.has_ssdiff = 1;
        } else if (!strcmp(name, "all")) {
                ctx.qry.show_all = atoi(value);
        } else if (!strcmp(name, "context")) {
                ctx.qry.context = atoi(value);
+       } else if (!strcmp(name, "ignorews")) {
+               ctx.qry.ignorews = atoi(value);
        }
 }
 
@@ -284,6 +312,7 @@ static void prepare_context(struct cgit_context *ctx)
        ctx->cfg.css = "/cgit.css";
        ctx->cfg.logo = "/cgit.png";
        ctx->cfg.local_time = 0;
+       ctx->cfg.enable_gitweb_owner = 1;
        ctx->cfg.enable_tree_linenumbers = 1;
        ctx->cfg.max_repo_count = 50;
        ctx->cfg.max_commit_count = 50;
@@ -293,10 +322,13 @@ static void prepare_context(struct cgit_context *ctx)
        ctx->cfg.max_blob_size = 0;
        ctx->cfg.max_stats = 0;
        ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
+       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.summary_branches = 10;
@@ -372,13 +404,17 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
        char *tmp;
        unsigned char sha1[20];
        int nongit = 0;
+       int rc;
 
        setenv("GIT_DIR", ctx->repo->path, 1);
        setup_git_directory_gently(&nongit);
        if (nongit) {
+               rc = errno;
                ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
                                      "config error");
-               tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
+               tmp = fmt("Failed to open %s: %s",
+                         ctx->repo->name,
+                         rc ? strerror(rc) : "Not a valid git repository");
                ctx->repo = NULL;
                cgit_print_http_headers(ctx);
                cgit_print_docstart(ctx);
@@ -522,6 +558,8 @@ void print_repo(FILE *f, struct cgit_repo *repo)
                fprintf(f, "repo.section=%s\n", repo->section);
        if (repo->clone_url)
                fprintf(f, "repo.clone-url=%s\n", repo->clone_url);
+       fprintf(f, "repo.enable-commit-graph=%d\n",
+               repo->enable_commit_graph);
        fprintf(f, "repo.enable-log-filecount=%d\n",
                repo->enable_log_filecount);
        fprintf(f, "repo.enable-log-linecount=%d\n",
@@ -572,7 +610,10 @@ static int generate_cached_repolist(const char *path, const char *cached_rc)
                return errno;
        }
        idx = cgit_repolist.count;
-       scan_tree(path, repo_config);
+       if (ctx.cfg.project_list)
+               scan_projects(path, ctx.cfg.project_list, repo_config);
+       else
+               scan_tree(path, repo_config);
        print_repolist(f, &cgit_repolist, idx);
        if (rename(locked_rc, cached_rc))
                fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n",
@@ -586,17 +627,25 @@ static void process_cached_repolist(const char *path)
        struct stat st;
        char *cached_rc;
        time_t age;
+       unsigned long hash;
 
-       cached_rc = xstrdup(fmt("%s/rc-%8x", ctx.cfg.cache_root,
-               hash_str(path)));
+       hash = hash_str(path);
+       if (ctx.cfg.project_list)
+               hash += hash_str(ctx.cfg.project_list);
+       cached_rc = xstrdup(fmt("%s/rc-%8lx", ctx.cfg.cache_root, hash));
 
        if (stat(cached_rc, &st)) {
                /* Nothing is cached, we need to scan without forking. And
                 * if we fail to generate a cached repolist, we need to
                 * invoke scan_tree manually.
                 */
-               if (generate_cached_repolist(path, cached_rc))
-                       scan_tree(path, repo_config);
+               if (generate_cached_repolist(path, cached_rc)) {
+                       if (ctx.cfg.project_list)
+                               scan_projects(path, ctx.cfg.project_list,
+                                             repo_config);
+                       else
+                               scan_tree(path, repo_config);
+               }
                return;
        }
 
@@ -710,10 +759,14 @@ int main(int argc, const char **argv)
        http_parse_querystring(ctx.qry.raw, querystring_cb);
 
        /* If virtual-root isn't specified in cgitrc, lets pretend
-        * that virtual-root equals SCRIPT_NAME.
+        * that virtual-root equals SCRIPT_NAME, minus any possibly
+        * trailing slashes.
         */
-       if (!ctx.cfg.virtual_root)
-               ctx.cfg.virtual_root = ctx.cfg.script_name;
+       if (!ctx.cfg.virtual_root && ctx.cfg.script_name) {
+               ctx.cfg.virtual_root = trim_end(ctx.cfg.script_name, '/');
+               if (!ctx.cfg.virtual_root)
+                       ctx.cfg.virtual_root = "";
+        }
 
        /* If no url parameter is specified on the querystring, lets
         * use PATH_INFO as url. This allows cgit to work with virtual