]> gitweb.ps.run Git - ps-cgit/commitdiff
Merge branch 'fh/filter-api'
authorLars Hjemli <hjemli@gmail.com>
Mon, 23 May 2011 21:28:38 +0000 (23:28 +0200)
committerLars Hjemli <hjemli@gmail.com>
Mon, 23 May 2011 21:28:38 +0000 (23:28 +0200)
Conflicts:
cgit.c

1  2 
cgit.c
cgit.h
cgitrc.5.txt
shared.c
ui-repolist.c
ui-tree.c

diff --combined cgit.c
index eb964ac082f3be22305915040269c57715e0a88d,5d6e4886dd2fe2b31df0fc0124f0e53d9115ce9d..6be3754d47d1c75300189ebab68e0a1c6488a139
--- 1/cgit.c
--- 2/cgit.c
+++ b/cgit.c
@@@ -26,20 -26,31 +26,33 @@@ void add_mimetype(const char *name, con
        item->util = xstrdup(value);
  }
  
- struct cgit_filter *new_filter(const char *cmd, int extra_args)
+ 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:
+                       extra_args = 1;
+                       break;
+               case ABOUT:
+               case COMMIT:
+               default:
+                       extra_args = 0;
+                       break;
+       }
        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;
  }
  
@@@ -83,11 -94,11 +96,11 @@@ void repo_config(struct cgit_repo *repo
                repo->logo_link = xstrdup(value);
        else if (ctx.cfg.enable_filter_overrides) {
                if (!strcmp(name, "about-filter"))
-                       repo->about_filter = new_filter(value, 0);
+                       repo->about_filter = new_filter(value, ABOUT);
                else if (!strcmp(name, "commit-filter"))
-                       repo->commit_filter = new_filter(value, 0);
+                       repo->commit_filter = new_filter(value, COMMIT);
                else if (!strcmp(name, "source-filter"))
-                       repo->source_filter = new_filter(value, 1);
+                       repo->source_filter = new_filter(value, SOURCE);
        }
  }
  
@@@ -147,8 -158,6 +160,8 @@@ void config_cb(const char *name, const 
                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-http-clone"))
 +              ctx.cfg.enable_http_clone = atoi(value);
        else if (!strcmp(name, "enable-index-links"))
                ctx.cfg.enable_index_links = atoi(value);
        else if (!strcmp(name, "enable-commit-graph"))
        else if (!strcmp(name, "cache-dynamic-ttl"))
                ctx.cfg.cache_dynamic_ttl = atoi(value);
        else if (!strcmp(name, "about-filter"))
-               ctx.cfg.about_filter = new_filter(value, 0);
+               ctx.cfg.about_filter = new_filter(value, ABOUT);
        else if (!strcmp(name, "commit-filter"))
-               ctx.cfg.commit_filter = new_filter(value, 0);
+               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"))
        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);
+               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"))
@@@ -314,7 -323,6 +327,7 @@@ static void prepare_context(struct cgit
        ctx->cfg.logo = "/cgit.png";
        ctx->cfg.local_time = 0;
        ctx->cfg.enable_gitweb_owner = 1;
 +      ctx->cfg.enable_http_clone = 1;
        ctx->cfg.enable_tree_linenumbers = 1;
        ctx->cfg.max_repo_count = 50;
        ctx->cfg.max_commit_count = 50;
@@@ -442,7 -450,7 +455,7 @@@ static int prepare_repo_cmd(struct cgit
                tmp = xstrdup(ctx->qry.head);
                ctx->qry.head = ctx->repo->defbranch;
                ctx->page.status = 404;
 -              ctx->page.statusmsg = "not found";
 +              ctx->page.statusmsg = "Not found";
                cgit_print_http_headers(ctx);
                cgit_print_docstart(ctx);
                cgit_print_pageheader(ctx);
@@@ -461,8 -469,6 +474,8 @@@ static void process_request(void *cbdat
        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(ctx);
                cgit_print_docstart(ctx);
                cgit_print_pageheader(ctx);
                return;
        }
  
 +      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).
@@@ -767,11 -768,8 +780,11 @@@ int main(int argc, const char **argv
         * that virtual-root equals SCRIPT_NAME, minus any possibly
         * trailing slashes.
         */
 -      if (!ctx.cfg.virtual_root)
 +      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
diff --combined cgit.h
index ecae4537a6b3cfc72d78e33026c9a053b65b69f4,3b0eaf5c2bb1fe6f68955ab7168303ae533ca6fa..caa9d8e91853ae626f96ff939896f0dfb83a2e31
--- 1/cgit.h
--- 2/cgit.h
+++ b/cgit.h
@@@ -51,6 -51,10 +51,10 @@@ typedef void (*configfn)(const char *na
  typedef void (*filepair_fn)(struct diff_filepair *pair);
  typedef void (*linediff_fn)(char *line, int len);
  
+ typedef enum {
+       ABOUT, COMMIT, SOURCE
+ } filter_type;
  struct cgit_filter {
        char *cmd;
        char **argv;
@@@ -191,7 -195,6 +195,7 @@@ struct cgit_config 
        int embedded;
        int enable_filter_overrides;
        int enable_gitweb_owner;
 +      int enable_http_clone;
        int enable_index_links;
        int enable_commit_graph;
        int enable_log_filecount;
@@@ -315,7 -318,7 +319,7 @@@ extern const char *cgit_repobasename(co
  
  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, struct cgit_repo * repo);
  extern int cgit_close_filter(struct cgit_filter *filter);
  
  extern int readfile(const char *path, char **buf, size_t *size);
diff --combined cgitrc.5.txt
index 875d51f8847519da360b66ff3f1e63e400fc4d24,60539d7ecf947ba7c2aee9d760a8095fef099795..5903a938c8c4cbe9c5b1e110ae8fbbb787f656f9
@@@ -31,7 -31,7 +31,7 @@@ about-filter:
        about pages (both top-level and for each repository). The command will
        get the content of the about-file on its STDIN, and the STDOUT from the
        command will be included verbatim on the about page. Default value:
-       none.
+       none. See also: "FILTER API".
  
  agefile::
        Specifies a path, relative to each repository path, which can be used
@@@ -81,6 -81,7 +81,7 @@@ commit-filter:
        The command will get the message on its STDIN, and the STDOUT from the
        command will be included verbatim as the commit message, i.e. this can
        be used to implement bugtracker integration. Default value: none.
+       See also: "FILTER API".
  
  css::
        Url which specifies the css document to include in all cgit pages.
@@@ -105,11 -106,6 +106,11 @@@ enable-gitweb-owner:
        for the git config value "gitweb.owner" to determine the owner.
        Default value: "1". See also: scan-path.
  
 +enable-http-clone::
 +      If set to "1", cgit will act as an dumb HTTP endpoint for git clones.
 +      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
        each repo in the repository index (specifically, to the "summary",
@@@ -292,9 -288,8 +293,9 @@@ scan-path:
        the result will be cached as a cgitrc include-file in the cache
        directory. If project-list has been defined prior to scan-path,
        scan-path loads only the directories listed in the file pointed to by
 -      project-list. Default value: none. See also: cache-scanrc-ttl,
 -      project-list.
 +      project-list. Be advised that only the global settings taken
 +      before the scan-path directive will be applied to each repository.
 +      Default value: none. See also: cache-scanrc-ttl, project-list.
  
  section::
        The name of the current repository section - all repositories defined
@@@ -314,8 -309,7 +315,8 @@@ side-by-side-diffs:
  snapshots::
        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" and "zip". Default value: none.
 +      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
        and the name of the blob as its only command line argument. The STDOUT
        from the command will be included verbatim as the blob contents, i.e.
        this can be used to implement e.g. syntax highlighting. Default value:
-       none.
+       none. See also: "FILTER API".
  
  summary-branches::
        Specifies the number of branches to display in the repository "summary"
@@@ -356,7 -350,7 +357,7 @@@ REPOSITORY SETTING
  -------------------
  repo.about-filter::
        Override the default about-filter. Default value: none. See also:
-       "enable-filter-overrides".
+       "enable-filter-overrides". See also: "FILTER API".
  
  repo.clone-url::
        A list of space-separated urls which can be used to clone this repo.
  
  repo.commit-filter::
        Override the default commit-filter. Default value: none. See also:
-       "enable-filter-overrides".
+       "enable-filter-overrides". See also: "FILTER API".
  
  repo.defbranch::
        The name of the default branch for this repository. If no such branch
@@@ -435,7 -429,7 +436,7 @@@ repo.section:
  
  repo.source-filter::
        Override the default source-filter. Default value: none. See also:
-       "enable-filter-overrides".
+       "enable-filter-overrides". See also: "FILTER API".
  
  repo.url::
        The relative url used to access the repository. This must be the first
@@@ -455,6 -449,42 +456,42 @@@ Note: the "repo." prefix is dropped fro
  config files, e.g. "repo.desc" becomes "desc".
  
  
+ FILTER API
+ ----------
+ - about filter::
+   This filter is given no arguments.
+   The about text that is to be filtered is available on standard input and the
+   filtered text is expected on standard output.
+ - commit filter::
+   This filter is given no arguments.
+   The commit message text that is to be filtered is available on standard input
+   and the filtered text is expected on standard output.
+ - 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 example) the syntax
+   highlighting mode.
+   The contents of the source file that is to be filtered is available on
+   standard input and the filtered contents is expected on standard output.
+ Also, all filters are handed the following environment variables:
+ - 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 )
+ If a setting is not defined for a repository and the corresponding global
+ setting is also not defined (if applicable), then the corresponding
+ environment variable will be an empty string.
+ Note that under normal circumstance all these environment variables are
+ defined. If however the total size of the defined settings exceed the
+ allocated buffer within cgit then only the environment variables that fit
+ in the allocated buffer are handed to the filter.
  EXAMPLE CGITRC FILE
  -------------------
  
diff --combined shared.c
index 3778a5ba440313ca439190217ffb7b458e337e53,986f965dc872d784b8b96f27003630fd6971933d..be2ae59e0a3fb764b8f3d6977189ff492d831a8c
+++ b/shared.c
@@@ -7,6 -7,8 +7,8 @@@
   */
  
  #include "cgit.h"
+ #include <stdio.h>
+ #include <linux/limits.h>
  
  struct cgit_repolist cgit_repolist;
  struct cgit_context ctx;
@@@ -100,15 -102,23 +102,15 @@@ void *cgit_free_commitinfo(struct commi
  char *trim_end(const char *str, char c)
  {
        int len;
 -      char *s, *t;
  
        if (str == NULL)
                return NULL;
 -      t = (char *)str;
 -      len = strlen(t);
 -      while(len > 0 && t[len - 1] == c)
 +      len = strlen(str);
 +      while(len > 0 && str[len - 1] == c)
                len--;
 -
        if (len == 0)
                return NULL;
 -
 -      c = t[len];
 -      t[len] = '\0';
 -      s = xstrdup(t);
 -      t[len] = c;
 -      return s;
 +      return xstrndup(str, len);
  }
  
  char *strlpart(char *txt, int maxlen)
@@@ -303,6 -313,7 +305,6 @@@ void cgit_diff_tree(const unsigned cha
                    filepair_fn fn, const char *prefix, int ignorews)
  {
        struct diff_options opt;
 -      int ret;
        int prefixlen;
  
        diff_setup(&opt);
        diff_setup_done(&opt);
  
        if (old_sha1 && !is_null_sha1(old_sha1))
 -              ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
 +              diff_tree_sha1(old_sha1, new_sha1, "", &opt);
        else
 -              ret = diff_root_tree_sha1(new_sha1, "", &opt);
 +              diff_root_tree_sha1(new_sha1, "", &opt);
        diffcore_std(&opt);
        diff_flush(&opt);
  }
@@@ -367,7 -378,33 +369,33 @@@ int cgit_parse_snapshots_mask(const cha
        return rv;
  }
  
- int cgit_open_filter(struct cgit_filter *filter)
+ typedef struct {
+       char * name;
+       char * value;
+ } cgit_env_var;
+ static void prepare_env(struct cgit_repo * repo) {
+       cgit_env_var env_vars[] = {
+               { .name = "CGIT_REPO_URL", .value = repo->url },
+               { .name = "CGIT_REPO_NAME", .value = repo->name },
+               { .name = "CGIT_REPO_PATH", .value = repo->path },
+               { .name = "CGIT_REPO_OWNER", .value = repo->owner },
+               { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
+               { .name = "CGIT_REPO_SECTION", .value = repo->section },
+               { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
+       };
+       int env_var_count = ARRAY_SIZE(env_vars);
+       cgit_env_var *p, *q;
+       static char *warn = "cgit warning: failed to set env: %s=%s\n";
+       p = env_vars;
+       q = p + env_var_count;
+       for (; p < q; p++)
+               if (setenv(p->name, p->value, 1))
+                       fprintf(stderr, warn, p->name, p->value);
+ }
+ int cgit_open_filter(struct cgit_filter *filter, struct cgit_repo * repo)
  {
  
        filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
                close(filter->pipe_fh[1]);
                chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
                        "Unable to use pipe as STDIN");
+               if (repo)
+                       prepare_env(repo);
                execvp(filter->cmd, filter->argv);
                die("Unable to exec subprocess %s: %s (%d)", filter->cmd,
                        strerror(errno), errno);
diff --combined ui-repolist.c
index e138f591ae5bc1e6d62ce56d71b24fb745687d3e,05b4548fab2f0a4de2230e5f46ece7c90588a91f..6f304bb36006834da65a319da5163ffe6b1bed60
@@@ -20,7 -20,7 +20,7 @@@ time_t read_agefile(char *path
        if (readfile(path, &buf, &size))
                return -1;
  
 -      if (parse_date(buf, buf2, sizeof(buf2)))
 +      if (parse_date(buf, buf2, sizeof(buf2)) > 0)
                result = strtoul(buf2, NULL, 10);
        else
                result = 0;
@@@ -291,7 -291,7 +291,7 @@@ void cgit_print_site_readme(
        if (!ctx.cfg.root_readme)
                return;
        if (ctx.cfg.about_filter)
-               cgit_open_filter(ctx.cfg.about_filter);
+               cgit_open_filter(ctx.cfg.about_filter, NULL);
        html_include(ctx.cfg.root_readme);
        if (ctx.cfg.about_filter)
                cgit_close_filter(ctx.cfg.about_filter);
diff --combined ui-tree.c
index 442b6be53d2c6ba6f7f4c17cd53d12da198c6f5d,835c166317093ed0d6c1eee7b35a3c8b3d8a5b18..2d8d2f3b83738fb007228f6f84e78dbe881d7f39
+++ b/ui-tree.c
@@@ -45,11 -45,9 +45,11 @@@ static void print_text_buffer(const cha
        if (ctx.repo->source_filter) {
                html("<td class='lines'><pre><code>");
                ctx.repo->source_filter->argv[1] = xstrdup(name);
-               cgit_open_filter(ctx.repo->source_filter);
+               cgit_open_filter(ctx.repo->source_filter, ctx.repo);
                html_raw(buf, size);
                cgit_close_filter(ctx.repo->source_filter);
 +              free(ctx.repo->source_filter->argv[1]);
 +              ctx.repo->source_filter->argv[1] = NULL;
                html("</code></pre></td></tr></table>\n");
                return;
        }