]> granicus.if.org Git - cgit/commitdiff
Grep the source, Luke lh/grep
authorLars Hjemli <hjemli@gmail.com>
Wed, 22 Jun 2011 08:49:57 +0000 (08:49 +0000)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 16 Oct 2012 21:57:00 +0000 (23:57 +0200)
This is a PoC for grepping the blobs in the current tree.

Signed-off-by: Lars Hjemli <larsh@hjemli.net>
cmd.c
ui-shared.c
ui-shared.h
ui-tree.c
ui-tree.h

diff --git a/cmd.c b/cmd.c
index 5a3d1574556e5a09510b891482ef38b22b0ca8a6..9a040a22a79646875c97d51d181c5d89bada048b 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -127,7 +127,10 @@ static void tag_fn(struct cgit_context *ctx)
 
 static void tree_fn(struct cgit_context *ctx)
 {
-       cgit_print_tree(ctx->qry.sha1, ctx->qry.path);
+       if (ctx->qry.grep)
+               cgit_grep(ctx->qry.sha1, ctx->qry.path, ctx->qry.grep);
+       else
+               cgit_print_tree(ctx->qry.sha1, ctx->qry.path);
 }
 
 #define def_cmd(name, want_repo, want_layout, want_vpath, is_clone) \
index 43166af4d568125306ff3f88d2f6f97c54d2405e..026c2cbf5d5af4bb4d9ce4bdc4f9d89ba493fe5b 100644 (file)
@@ -246,6 +246,61 @@ static char *repolink(const char *title, const char *class, const char *page,
        return fmt("%s", delim);
 }
 
+static char *repolink2(const char *title, const char *class, const char *page,
+                      const char *head, const char *path, int lineno)
+{
+       char *delim = "?";
+
+       html("<a");
+       if (title) {
+               html(" title='");
+               html_attr(title);
+               html("'");
+       }
+       if (class) {
+               html(" class='");
+               html_attr(class);
+               html("'");
+       }
+       html(" href='");
+       if (ctx.cfg.virtual_root) {
+               html_url_path(ctx.cfg.virtual_root);
+               if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
+                       html("/");
+               html_url_path(ctx.repo->url);
+               if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
+                       html("/");
+               if (page) {
+                       html_url_path(page);
+                       html("/");
+                       if (path)
+                               html_url_path(path);
+               }
+       } else {
+               html(ctx.cfg.script_name);
+               html("?url=");
+               html_url_arg(ctx.repo->url);
+               if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
+                       html("/");
+               if (page) {
+                       html_url_arg(page);
+                       html("/");
+                       if (path)
+                               html_url_arg(path);
+               }
+               delim = "&amp;";
+       }
+       if (head && strcmp(head, ctx.repo->defbranch)) {
+               html(delim);
+               html("h=");
+               html_url_arg(head);
+               delim = "&amp;";
+       }
+       if (lineno)
+               htmlf("#n%d", lineno);
+       return fmt("%s", delim);
+}
+
 static void reporevlink(const char *page, const char *name, const char *title,
                        const char *class, const char *head, const char *rev,
                        const char *path)
@@ -263,6 +318,23 @@ static void reporevlink(const char *page, const char *name, const char *title,
        html("</a>");
 }
 
+static void reporevlink2(const char *page, const char *name, const char *title,
+                        const char *class, const char *head, const char *rev,
+                        const char *path, int lineno)
+{
+       char *delim;
+
+       delim = repolink2(title, class, page, head, path, lineno);
+       if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
+               html(delim);
+               html("id=");
+               html_url_arg(rev);
+       }
+       html("'>");
+       html_txt(name);
+       html("</a>");
+}
+
 void cgit_summary_link(const char *name, const char *title, const char *class,
                       const char *head)
 {
@@ -281,6 +353,13 @@ void cgit_tree_link(const char *name, const char *title, const char *class,
        reporevlink("tree", name, title, class, head, rev, path);
 }
 
+void cgit_tree_link2(const char *name, const char *title, const char *class,
+                    const char *head, const char *rev, const char *path,
+                    int lineno)
+{
+       reporevlink2("tree", name, title, class, head, rev, path, lineno);
+}
+
 void cgit_plain_link(const char *name, const char *title, const char *class,
                     const char *head, const char *rev, const char *path)
 {
@@ -875,6 +954,9 @@ static void print_header(struct cgit_context *ctx)
 
 void cgit_print_pageheader(struct cgit_context *ctx)
 {
+       int tree;
+       char *page;
+
        html("<div id='cgit'>");
        if (!ctx->cfg.noheader)
                print_header(ctx);
@@ -903,16 +985,23 @@ void cgit_print_pageheader(struct cgit_context *ctx)
                                    NULL);
                html("</td><td class='form'>");
                html("<form class='right' method='get' action='");
-               if (ctx->cfg.virtual_root)
-                       html_url_path(cgit_fileurl(ctx->qry.repo, "log",
+               if (ctx->cfg.virtual_root) {
+                       tree = !strcmp(ctx->qry.page, "tree");
+                       page = tree ? "tree" : "log";
+                       html_url_path(cgit_fileurl(ctx->qry.repo, page,
                                                   ctx->qry.vpath, NULL));
+               }
                html("'>\n");
-               cgit_add_hidden_formfields(1, 0, "log");
+               cgit_add_hidden_formfields(1, 0, page);
                html("<select name='qt'>\n");
-               html_option("grep", "log msg", ctx->qry.grep);
-               html_option("author", "author", ctx->qry.grep);
-               html_option("committer", "committer", ctx->qry.grep);
-               html_option("range", "range", ctx->qry.grep);
+               if (tree) {
+                       html_option("grep", "source", ctx->qry.grep);
+               } else {
+                       html_option("grep", "log msg", ctx->qry.grep);
+                       html_option("author", "author", ctx->qry.grep);
+                       html_option("committer", "committer", ctx->qry.grep);
+                       html_option("range", "range", ctx->qry.grep);
+               }
                html("</select>\n");
                html("<input class='txt' type='text' size='10' name='q' value='");
                html_attr(ctx->qry.search);
index 87a7dac82f85d5d8f9dc391f5fe9a1ffe940c222..35dca9c11b448b9c374c6098e012a2070f49e821 100644 (file)
@@ -20,6 +20,9 @@ extern void cgit_tag_link(const char *name, const char *title,
 extern void cgit_tree_link(const char *name, const char *title,
                           const char *class, const char *head,
                           const char *rev, const char *path);
+extern void cgit_tree_link2(const char *name, const char *title,
+                          const char *class, const char *head,
+                          const char *rev, const char *path, int lno);
 extern void cgit_plain_link(const char *name, const char *title,
                            const char *class, const char *head,
                            const char *rev, const char *path);
index b1adcc7b21e5044f489f751c21852400a329f322..249e99e488a0a1aa18a07954299594699da72038 100644 (file)
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -277,3 +277,122 @@ void cgit_print_tree(const char *rev, char *path)
        read_tree_recursive(commit->tree, "", 0, 0, paths, walk_tree, NULL);
        ls_tail();
 }
+
+struct grep_data {
+       regex_t re;
+       char *path;
+       int max_hits;
+};
+
+static int grep_blob(const unsigned char *sha1, const char *base, int baselen,
+                     const char *pathname, struct grep_data *grep)
+{
+       enum object_type type;
+       char *buf, *line, *lf, *fullpath;
+       unsigned long size;
+       int hits, lno, rc;
+
+       type = sha1_object_info(sha1, &size);
+       if (type == OBJ_BAD) {
+               cgit_print_error(fmt("Bad object name: %s",
+                                    sha1_to_hex(sha1)));
+               return -1;
+       }
+
+       buf = read_sha1_file(sha1, &type, &size);
+       if (!buf) {
+               cgit_print_error(fmt("Error reading object %s",
+                                    sha1_to_hex(sha1)));
+               return -1;
+       }
+       hits = 0;
+       line = buf;
+       lno = 1;
+       while (size) {
+               lf = strchr(line, '\n');
+               if (lf)
+                       *lf = 0;
+               rc = regexec(&grep->re, line, 0, NULL, 0);
+               if (!rc) {
+                       if (!hits) {
+                               fullpath = fmt("%.*s%s", baselen, base,
+                                              pathname);
+                               htmlf("<strong>%s</strong><pre>", fullpath);
+                       }
+                       htmlf("%6d: ", lno);
+                       while (isspace(*line))
+                               line++;
+                       cgit_tree_link2(line, NULL, NULL, ctx.qry.head,
+                                       ctx.qry.sha1, fullpath, lno);
+                       html("\n");
+                       ++hits;
+                       if (!(--grep->max_hits))
+                               break;
+               }
+               if (!lf)
+                       break;
+               size -= (lf - line);
+               *lf = '\n';
+               line = lf + 1;
+               lno++;
+       }
+       if (hits)
+               html("</pre>");
+       if (!grep->max_hits) {
+               cgit_print_error("Too many matches found.");
+               return -1;
+       }
+       return 0;
+}
+
+static int grep_tree(const unsigned char *sha1, const char *base, int baselen,
+                    const char *pathname, unsigned mode, int stage,
+                    void *cbdata)
+{
+       int rc;
+       char *fullpath;
+       struct grep_data *grep;
+
+       grep = cbdata;
+       fullpath = fmt("%.*s%s", baselen, base, pathname);
+       if (S_ISDIR(mode)) {
+               rc = grep->path ? prefixcmp(grep->path, fullpath) : 0;
+               if (!rc)
+                       return READ_TREE_RECURSIVE;
+               return (rc > 0 ? -1 : 0);
+       } else if (S_ISREG(mode)) {
+               rc = grep->path ? prefixcmp(fullpath, grep->path) : 0;
+               if (!rc)
+                       return grep_blob(sha1, base, baselen, pathname, grep);
+               return (rc < 0 ? -1 : 0);
+       }
+       return 0;
+}
+
+void cgit_grep(const char *rev, char *path, char *grep)
+{
+       unsigned char sha1[20];
+       struct commit *commit;
+       struct grep_data data;
+
+       if (!rev)
+               rev = ctx.qry.head;
+
+       curr_rev = xstrdup(rev);
+       if (get_sha1(rev, sha1)) {
+               cgit_print_error(fmt("Invalid revision name: %s", rev));
+               return;
+       }
+       commit = lookup_commit_reference(sha1);
+       if (!commit || parse_commit(commit)) {
+               cgit_print_error(fmt("Invalid commit reference: %s", rev));
+               return;
+       }
+       data.path = path;
+       data.max_hits = 100;
+       if (regcomp(&data.re, ctx.qry.search, REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
+               cgit_print_error("Invalid regex");
+               return;
+       }
+       read_tree_recursive(commit->tree, "", 0, 0, NULL, grep_tree, &data);
+}
index bbd34e3566a528e4bfecb1b66ac4b391472648d7..2e0851f2d53f69f6930889581b8000167f05b3db 100644 (file)
--- a/ui-tree.h
+++ b/ui-tree.h
@@ -2,5 +2,6 @@
 #define UI_TREE_H
 
 extern void cgit_print_tree(const char *rev, char *path);
+extern void cgit_grep(const char *rev, char *path, char *grep);
 
 #endif /* UI_TREE_H */