aboutsummaryrefslogtreecommitdiffstats
path: root/ui-shared.c
diff options
context:
space:
mode:
authorGravatar John Keeping <john@keeping.me.uk>2013-04-06 18:28:57 (JST)
committerGravatar Jason A. Donenfeld <Jason@zx2c4.com>2013-04-08 23:12:52 (JST)
commitfb3655df3bf85bd405c5921bbd4b3a54c705c839 (patch)
tree419a962a0b82f5ba3023791549044ff462229250 /ui-shared.c
parent42d5476f258e7909682f1b611da00d64507d45c6 (diff)
downloadcgit-fb3655df3bf85bd405c5921bbd4b3a54c705c839.zip
cgit-fb3655df3bf85bd405c5921bbd4b3a54c705c839.tar.gz
use struct strbuf instead of static buffers
Use "struct strbuf" from Git to remove the limit on file path length. Notes on scan-tree: This is slightly involved since I decided to pass the strbuf into add_repo() and modify if whenever a new file name is required, which should avoid any extra allocations within that function. The pattern there is to append the filename, use it and then reset the buffer to its original length (retaining a trailing '/'). Notes on ui-snapshot: Since write_archive modifies the argv array passed to it we copy the argv_array values into a new array of char* and then free the original argv_array structure and the new array without worrying about what the values now look like. Signed-off-by: John Keeping <john@keeping.me.uk>
Diffstat (limited to 'ui-shared.c')
-rw-r--r--ui-shared.c63
1 files changed, 35 insertions, 28 deletions
diff --git a/ui-shared.c b/ui-shared.c
index b93b77a..519eef7 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -62,7 +62,7 @@ const char *cgit_hosturl()
62 return NULL; 62 return NULL;
63 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80) 63 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
64 return ctx.env.server_name; 64 return ctx.env.server_name;
65 return xstrdup(fmt("%s:%s", ctx.env.server_name, ctx.env.server_port)); 65 return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
66} 66}
67 67
68const char *cgit_rooturl() 68const char *cgit_rooturl()
@@ -75,31 +75,30 @@ const char *cgit_rooturl()
75 75
76char *cgit_repourl(const char *reponame) 76char *cgit_repourl(const char *reponame)
77{ 77{
78 if (ctx.cfg.virtual_root) { 78 if (ctx.cfg.virtual_root)
79 return fmt("%s%s/", ctx.cfg.virtual_root, reponame); 79 return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
80 } else { 80 else
81 return fmt("?r=%s", reponame); 81 return fmtalloc("?r=%s", reponame);
82 }
83} 82}
84 83
85char *cgit_fileurl(const char *reponame, const char *pagename, 84char *cgit_fileurl(const char *reponame, const char *pagename,
86 const char *filename, const char *query) 85 const char *filename, const char *query)
87{ 86{
88 char *tmp; 87 struct strbuf sb = STRBUF_INIT;
89 char *delim; 88 char *delim;
90 89
91 if (ctx.cfg.virtual_root) { 90 if (ctx.cfg.virtual_root) {
92 tmp = fmt("%s%s/%s/%s", ctx.cfg.virtual_root, reponame, 91 strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
93 pagename, (filename ? filename:"")); 92 pagename, (filename ? filename:""));
94 delim = "?"; 93 delim = "?";
95 } else { 94 } else {
96 tmp = fmt("?url=%s/%s/%s", reponame, pagename, 95 strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
97 (filename ? filename : "")); 96 (filename ? filename : ""));
98 delim = "&amp;"; 97 delim = "&amp;";
99 } 98 }
100 if (query) 99 if (query)
101 tmp = fmt("%s%s%s", tmp, delim, query); 100 strbuf_addf(&sb, "%s%s", delim, query);
102 return tmp; 101 return strbuf_detach(&sb, NULL);
103} 102}
104 103
105char *cgit_pageurl(const char *reponame, const char *pagename, 104char *cgit_pageurl(const char *reponame, const char *pagename,
@@ -548,21 +547,21 @@ void cgit_submodule_link(const char *class, char *path, const char *rev)
548 htmlf("class='%s' ", class); 547 htmlf("class='%s' ", class);
549 html("href='"); 548 html("href='");
550 if (item) { 549 if (item) {
551 html_attr(fmt(item->util, rev)); 550 html_attrf(item->util, rev);
552 } else if (ctx.repo->module_link) { 551 } else if (ctx.repo->module_link) {
553 dir = strrchr(path, '/'); 552 dir = strrchr(path, '/');
554 if (dir) 553 if (dir)
555 dir++; 554 dir++;
556 else 555 else
557 dir = path; 556 dir = path;
558 html_attr(fmt(ctx.repo->module_link, dir, rev)); 557 html_attrf(ctx.repo->module_link, dir, rev);
559 } else { 558 } else {
560 html("#"); 559 html("#");
561 } 560 }
562 html("'>"); 561 html("'>");
563 html_txt(path); 562 html_txt(path);
564 html("</a>"); 563 html("</a>");
565 html_txt(fmt(" @ %.7s", rev)); 564 html_txtf(" @ %.7s", rev);
566 if (item && tail) 565 if (item && tail)
567 path[len - 1] = tail; 566 path[len - 1] = tail;
568} 567}
@@ -678,12 +677,16 @@ void cgit_print_docstart(struct cgit_context *ctx)
678 html("'/>\n"); 677 html("'/>\n");
679 } 678 }
680 if (host && ctx->repo && ctx->qry.head) { 679 if (host && ctx->repo && ctx->qry.head) {
680 struct strbuf sb = STRBUF_INIT;
681 strbuf_addf(&sb, "h=%s", ctx->qry.head);
682
681 html("<link rel='alternate' title='Atom feed' href='"); 683 html("<link rel='alternate' title='Atom feed' href='");
682 html(cgit_httpscheme()); 684 html(cgit_httpscheme());
683 html_attr(cgit_hosturl()); 685 html_attr(cgit_hosturl());
684 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.vpath, 686 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.vpath,
685 fmt("h=%s", ctx->qry.head))); 687 sb.buf));
686 html("' type='application/atom+xml'/>\n"); 688 html("' type='application/atom+xml'/>\n");
689 strbuf_release(&sb);
687 } 690 }
688 if (ctx->cfg.head_include) 691 if (ctx->cfg.head_include)
689 html_include(ctx->cfg.head_include); 692 html_include(ctx->cfg.head_include);
@@ -725,13 +728,14 @@ static int print_branch_option(const char *refname, const unsigned char *sha1,
725void cgit_add_hidden_formfields(int incl_head, int incl_search, 728void cgit_add_hidden_formfields(int incl_head, int incl_search,
726 const char *page) 729 const char *page)
727{ 730{
728 char *url;
729
730 if (!ctx.cfg.virtual_root) { 731 if (!ctx.cfg.virtual_root) {
731 url = fmt("%s/%s", ctx.qry.repo, page); 732 struct strbuf url = STRBUF_INIT;
733
734 strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
732 if (ctx.qry.vpath) 735 if (ctx.qry.vpath)
733 url = fmt("%s/%s", url, ctx.qry.vpath); 736 strbuf_addf(&url, "/%s", ctx.qry.vpath);
734 html_hidden("url", url); 737 html_hidden("url", url.buf);
738 strbuf_release(&url);
735 } 739 }
736 740
737 if (incl_head && ctx.qry.head && ctx.repo->defbranch && 741 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
@@ -926,20 +930,23 @@ void cgit_print_snapshot_links(const char *repo, const char *head,
926 const char *hex, int snapshots) 930 const char *hex, int snapshots)
927{ 931{
928 const struct cgit_snapshot_format* f; 932 const struct cgit_snapshot_format* f;
929 char *prefix; 933 struct strbuf filename = STRBUF_INIT;
930 char *filename; 934 size_t prefixlen;
931 unsigned char sha1[20]; 935 unsigned char sha1[20];
932 936
933 if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 && 937 if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
934 (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1])) 938 (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
935 hex++; 939 hex++;
936 prefix = xstrdup(fmt("%s-%s", cgit_repobasename(repo), hex)); 940 strbuf_addf(&filename, "%s-%s", cgit_repobasename(repo), hex);
941 prefixlen = filename.len;
937 for (f = cgit_snapshot_formats; f->suffix; f++) { 942 for (f = cgit_snapshot_formats; f->suffix; f++) {
938 if (!(snapshots & f->bit)) 943 if (!(snapshots & f->bit))
939 continue; 944 continue;
940 filename = fmt("%s%s", prefix, f->suffix); 945 strbuf_setlen(&filename, prefixlen);
941 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename); 946 strbuf_addstr(&filename, f->suffix);
947 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
948 filename.buf);
942 html("<br/>"); 949 html("<br/>");
943 } 950 }
944 free(prefix); 951 strbuf_release(&filename);
945} 952}