]> gitweb.ps.run Git - ps-cgit/blobdiff - ui-shared.c
Free reflists after usage
[ps-cgit] / ui-shared.c
index 3e9282f35c0e032251b26ab00bd7f35e3bb7aee0..77a302d0c5bba8a54c29a6551b15cea6f02c9205 100644 (file)
@@ -23,7 +23,7 @@ static char *http_date(time_t t)
                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        struct tm *tm = gmtime(&t);
        return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
-                  tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
+                  tm->tm_mday, month[tm->tm_mon], 1900 + tm->tm_year,
                   tm->tm_hour, tm->tm_min, tm->tm_sec);
 }
 
@@ -83,7 +83,7 @@ char *cgit_fileurl(const char *reponame, const char *pagename,
        } else {
                tmp = fmt("?url=%s/%s/%s", reponame, pagename,
                          (filename ? filename : ""));
-               delim = "&";
+               delim = "&";
        }
        if (query)
                tmp = fmt("%s%s%s", tmp, delim, query);
@@ -93,7 +93,7 @@ char *cgit_fileurl(const char *reponame, const char *pagename,
 char *cgit_pageurl(const char *reponame, const char *pagename,
                   const char *query)
 {
-       return cgit_fileurl(reponame,pagename,0,query);
+       return cgit_fileurl(reponame, pagename, 0, query);
 }
 
 const char *cgit_repobasename(const char *reponame)
@@ -102,37 +102,25 @@ const char *cgit_repobasename(const char *reponame)
        static char rvbuf[1024];
        int p;
        const char *rv;
-       strncpy(rvbuf,reponame,sizeof(rvbuf));
-       if(rvbuf[sizeof(rvbuf)-1])
+       strncpy(rvbuf, reponame, sizeof(rvbuf));
+       if (rvbuf[sizeof(rvbuf)-1])
                die("cgit_repobasename: truncated repository name '%s'", reponame);
        p = strlen(rvbuf)-1;
        /* strip trailing slashes */
-       while(p && rvbuf[p]=='/') rvbuf[p--]=0;
+       while (p && rvbuf[p] == '/') rvbuf[p--] = 0;
        /* strip trailing .git */
-       if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
+       if (p >= 3 && !strncmp(&rvbuf[p-3], ".git", 4)) {
                p -= 3; rvbuf[p--] = 0;
        }
        /* strip more trailing slashes if any */
-       while( p && rvbuf[p]=='/') rvbuf[p--]=0;
+       while ( p && rvbuf[p] == '/') rvbuf[p--] = 0;
        /* find last slash in the remaining string */
        rv = strrchr(rvbuf,'/');
-       if(rv)
+       if (rv)
                return ++rv;
        return rvbuf;
 }
 
-char *cgit_currurl()
-{
-       if (!ctx.cfg.virtual_root)
-               return ctx.cfg.script_name;
-       else if (ctx.qry.page)
-               return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page);
-       else if (ctx.qry.repo)
-               return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo);
-       else
-               return fmt("%s/", ctx.cfg.virtual_root);
-}
-
 static void site_url(const char *page, const char *search, const char *sort, int ofs)
 {
        char *delim = "?";
@@ -146,19 +134,19 @@ static void site_url(const char *page, const char *search, const char *sort, int
 
        if (page) {
                htmlf("?p=%s", page);
-               delim = "&";
+               delim = "&";
        }
        if (search) {
                html(delim);
                html("q=");
                html_attr(search);
-               delim = "&";
+               delim = "&";
        }
        if (sort) {
                html(delim);
                html("s=");
                html_attr(sort);
-               delim = "&";
+               delim = "&";
        }
        if (ofs) {
                html(delim);
@@ -294,17 +282,17 @@ void cgit_log_link(const char *name, const char *title, const char *class,
        char *delim;
 
        delim = repolink(title, class, "log", head, path);
-       if (rev && strcmp(rev, ctx.qry.head)) {
+       if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
                html(delim);
                html("id=");
                html_url_arg(rev);
-               delim = "&";
+               delim = "&";
        }
        if (grep && pattern) {
                html(delim);
                html("qt=");
                html_url_arg(grep);
-               delim = "&";
+               delim = "&";
                html(delim);
                html("q=");
                html_url_arg(pattern);
@@ -313,7 +301,7 @@ void cgit_log_link(const char *name, const char *title, const char *class,
                html(delim);
                html("ofs=");
                htmlf("%d", ofs);
-               delim = "&";
+               delim = "&";
        }
        if (showmsg) {
                html(delim);
@@ -338,7 +326,7 @@ void cgit_commit_link(char *name, const char *title, const char *class,
        char *delim;
 
        delim = repolink(title, class, "commit", head, path);
-       if (rev && strcmp(rev, ctx.qry.head)) {
+       if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
                html(delim);
                html("id=");
                html_url_arg(rev);
@@ -361,7 +349,10 @@ void cgit_commit_link(char *name, const char *title, const char *class,
                delim = "&";
        }
        html("'>");
-       html_txt(name);
+       if (name[0] != '\0')
+               html_txt(name);
+       else
+               html_txt("(no commit message)");
        html("</a>");
 }
 
@@ -430,8 +421,8 @@ void cgit_stats_link(const char *name, const char *title, const char *class,
        reporevlink("stats", name, title, class, head, NULL, path);
 }
 
-void cgit_self_link(char *name, const char *title, const char *class,
-                   struct cgit_context *ctx)
+static void cgit_self_link(char *name, const char *title, const char *class,
+                          struct cgit_context *ctx)
 {
        if (!strcmp(ctx->qry.page, "repolist"))
                return cgit_index_link(name, title, class, ctx->qry.search, ctx->qry.sort,
@@ -496,7 +487,7 @@ void cgit_object_link(struct object *obj)
        shortrev = xstrdup(fullrev);
        shortrev[10] = '\0';
        if (obj->type == OBJ_COMMIT) {
-                cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
+               cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
                                 ctx.qry.head, fullrev, NULL, 0);
                return;
        } else if (obj->type == OBJ_TREE)
@@ -509,6 +500,63 @@ void cgit_object_link(struct object *obj)
        reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
 }
 
+static struct string_list_item *lookup_path(struct string_list *list,
+                                           const char *path)
+{
+       struct string_list_item *item;
+
+       while (path && path[0]) {
+               if ((item = string_list_lookup(list, path)))
+                       return item;
+               if (!(path = strchr(path, '/')))
+                       break;
+               path++;
+       }
+       return NULL;
+}
+
+void cgit_submodule_link(const char *class, char *path, const char *rev)
+{
+       struct string_list *list;
+       struct string_list_item *item;
+       char tail, *dir;
+       size_t len;
+
+       tail = 0;
+       list = &ctx.repo->submodules;
+       item = lookup_path(list, path);
+       if (!item) {
+               len = strlen(path);
+               tail = path[len - 1];
+               if (tail == '/') {
+                       path[len - 1] = 0;
+                       item = lookup_path(list, path);
+               }
+       }
+       html("<a ");
+       if (class)
+               htmlf("class='%s' ", class);
+       html("href='");
+       if (item) {
+               html_attr(fmt(item->util, rev));
+       } else if (ctx.repo->module_link) {
+               dir = strrchr(path, '/');
+               if (dir)
+                       dir++;
+               else
+                       dir = path;
+               html_attr(fmt(ctx.repo->module_link, dir, rev));
+       } else {
+               html("#");
+       }
+       html("'>");
+       html_txt(path);
+       html("</a>");
+       html_txt(fmt(" @ %.7s", rev));
+       if (item && tail)
+               path[len - 1] = tail;
+}
+
 void cgit_print_date(time_t secs, const char *format, int local_time)
 {
        char buf[64];
@@ -516,7 +564,7 @@ void cgit_print_date(time_t secs, const char *format, int local_time)
 
        if (!secs)
                return;
-       if(local_time)
+       if (local_time)
                time = localtime(&secs);
        else
                time = gmtime(&secs);
@@ -656,53 +704,14 @@ void cgit_print_docend()
        html("</body>\n</html>\n");
 }
 
-int print_branch_option(const char *refname, const unsigned char *sha1,
-                       int flags, void *cb_data)
+static int print_branch_option(const char *refname, const unsigned char *sha1,
+                              int flags, void *cb_data)
 {
        char *name = (char *)refname;
        html_option(name, name, ctx.qry.head);
        return 0;
 }
 
-int print_archive_ref(const char *refname, const unsigned char *sha1,
-                      int flags, void *cb_data)
-{
-       struct tag *tag;
-       struct taginfo *info;
-       struct object *obj;
-       char buf[256], *url;
-       unsigned char fileid[20];
-       int *header = (int *)cb_data;
-
-       if (prefixcmp(refname, "refs/archives"))
-               return 0;
-       strncpy(buf, refname+14, sizeof(buf));
-       obj = parse_object(sha1);
-       if (!obj)
-               return 1;
-       if (obj->type == OBJ_TAG) {
-               tag = lookup_tag(sha1);
-               if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
-                       return 0;
-               hashcpy(fileid, tag->tagged->sha1);
-       } else if (obj->type != OBJ_BLOB) {
-               return 0;
-       } else {
-               hashcpy(fileid, sha1);
-       }
-       if (!*header) {
-               html("<h1>download</h1>\n");
-               *header = 1;
-       }
-       url = cgit_pageurl(ctx.qry.repo, "blob",
-                          fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
-                              buf));
-       html_link_open(url, NULL, "menu");
-       html_txt(strlpart(buf, 20));
-       html_link_close();
-       return 0;
-}
-
 void cgit_add_hidden_formfields(int incl_head, int incl_search,
                                const char *page)
 {
@@ -908,7 +917,7 @@ void cgit_print_snapshot_links(const char *repo, const char *head,
 {
        const struct cgit_snapshot_format* f;
        char *prefix;
-       char *filename;
+       char *filename;
        unsigned char sha1[20];
 
        if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&