+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;
+}
+
+void cgit_add_hidden_formfields(int incl_head, int incl_search,
+ const char *page)
+{
+ if (!ctx.cfg.virtual_root) {
+ struct strbuf url = STRBUF_INIT;
+
+ strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
+ if (ctx.qry.vpath)
+ strbuf_addf(&url, "/%s", ctx.qry.vpath);
+ html_hidden("url", url.buf);
+ strbuf_release(&url);
+ }
+
+ if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
+ strcmp(ctx.qry.head, ctx.repo->defbranch))
+ html_hidden("h", ctx.qry.head);
+
+ if (ctx.qry.sha1)
+ html_hidden("id", ctx.qry.sha1);
+ if (ctx.qry.sha2)
+ html_hidden("id2", ctx.qry.sha2);
+ if (ctx.qry.showmsg)
+ html_hidden("showmsg", "1");
+
+ if (incl_search) {
+ if (ctx.qry.grep)
+ html_hidden("qt", ctx.qry.grep);
+ if (ctx.qry.search)
+ html_hidden("q", ctx.qry.search);
+ }
+}
+
+static const char *hc(struct cgit_context *ctx, const char *page)
+{
+ return strcmp(ctx->qry.page, page) ? NULL : "active";
+}
+
+static void cgit_print_path_crumbs(struct cgit_context *ctx, char *path)
+{
+ char *old_path = ctx->qry.path;
+ char *p = path, *q, *end = path + strlen(path);
+
+ ctx->qry.path = NULL;
+ cgit_self_link("root", NULL, NULL, ctx);
+ ctx->qry.path = p = path;
+ while (p < end) {
+ if (!(q = strchr(p, '/')))
+ q = end;
+ *q = '\0';
+ html_txt("/");
+ cgit_self_link(p, NULL, NULL, ctx);
+ if (q < end)
+ *q = '/';
+ p = q + 1;
+ }
+ ctx->qry.path = old_path;
+}
+
+static void print_header(struct cgit_context *ctx)
+{
+ char *logo = NULL, *logo_link = NULL;
+
+ html("<table id='header'>\n");
+ html("<tr>\n");
+
+ if (ctx->repo && ctx->repo->logo && *ctx->repo->logo)
+ logo = ctx->repo->logo;
+ else
+ logo = ctx->cfg.logo;
+ if (ctx->repo && ctx->repo->logo_link && *ctx->repo->logo_link)
+ logo_link = ctx->repo->logo_link;
+ else
+ logo_link = ctx->cfg.logo_link;
+ if (logo && *logo) {
+ html("<td class='logo' rowspan='2'><a href='");
+ if (logo_link && *logo_link)
+ html_attr(logo_link);
+ else
+ html_attr(cgit_rooturl());
+ html("'><img src='");
+ html_attr(logo);
+ html("' alt='cgit logo'/></a></td>\n");
+ }
+
+ html("<td class='main'>");
+ if (ctx->repo) {
+ cgit_index_link("index", NULL, NULL, NULL, NULL, 0);
+ html(" : ");
+ cgit_summary_link(ctx->repo->name, ctx->repo->name, NULL, NULL);
+ html("</td><td class='form'>");
+ html("<form method='get' action=''>\n");
+ cgit_add_hidden_formfields(0, 1, ctx->qry.page);
+ html("<select name='h' onchange='this.form.submit();'>\n");
+ for_each_branch_ref(print_branch_option, ctx->qry.head);
+ html("</select> ");
+ html("<input type='submit' name='' value='switch'/>");
+ html("</form>");
+ } else
+ html_txt(ctx->cfg.root_title);
+ html("</td></tr>\n");
+
+ html("<tr><td class='sub'>");
+ if (ctx->repo) {
+ html_txt(ctx->repo->desc);
+ html("</td><td class='sub right'>");
+ html_txt(ctx->repo->owner);
+ } else {
+ if (ctx->cfg.root_desc)
+ html_txt(ctx->cfg.root_desc);
+ else if (ctx->cfg.index_info)
+ html_include(ctx->cfg.index_info);
+ }
+ html("</td></tr></table>\n");
+}
+
+void cgit_print_pageheader(struct cgit_context *ctx)
+{
+ html("<div id='cgit'>");
+ if (!ctx->cfg.noheader)
+ print_header(ctx);
+
+ html("<table class='tabs'><tr><td>\n");
+ if (ctx->repo) {
+ cgit_summary_link("summary", NULL, hc(ctx, "summary"),
+ ctx->qry.head);
+ cgit_refs_link("refs", NULL, hc(ctx, "refs"), ctx->qry.head,
+ ctx->qry.sha1, NULL);
+ cgit_log_link("log", NULL, hc(ctx, "log"), ctx->qry.head,
+ NULL, ctx->qry.vpath, 0, NULL, NULL,
+ ctx->qry.showmsg);
+ cgit_tree_link("tree", NULL, hc(ctx, "tree"), ctx->qry.head,
+ ctx->qry.sha1, ctx->qry.vpath);
+ cgit_commit_link("commit", NULL, hc(ctx, "commit"),
+ ctx->qry.head, ctx->qry.sha1, ctx->qry.vpath, 0);
+ cgit_diff_link("diff", NULL, hc(ctx, "diff"), ctx->qry.head,
+ ctx->qry.sha1, ctx->qry.sha2, ctx->qry.vpath, 0);
+ if (ctx->repo->max_stats)
+ cgit_stats_link("stats", NULL, hc(ctx, "stats"),
+ ctx->qry.head, ctx->qry.vpath);
+ if (ctx->repo->readme.nr)
+ reporevlink("about", "about", NULL,
+ hc(ctx, "about"), ctx->qry.head, NULL,
+ 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",
+ ctx->qry.vpath, NULL));
+ html("'>\n");
+ cgit_add_hidden_formfields(1, 0, "log");
+ 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);
+ html("</select>\n");
+ html("<input class='txt' type='text' size='10' name='q' value='");
+ html_attr(ctx->qry.search);
+ html("'/>\n");
+ html("<input type='submit' value='search'/>\n");
+ html("</form>\n");
+ } else {
+ site_link(NULL, "index", NULL, hc(ctx, "repolist"), NULL, NULL, 0);
+ if (ctx->cfg.root_readme)
+ site_link("about", "about", NULL, hc(ctx, "about"),
+ NULL, NULL, 0);
+ html("</td><td class='form'>");
+ html("<form method='get' action='");
+ html_attr(cgit_rooturl());
+ html("'>\n");
+ html("<input type='text' name='q' size='10' value='");
+ html_attr(ctx->qry.search);
+ html("'/>\n");
+ html("<input type='submit' value='search'/>\n");
+ html("</form>");
+ }
+ html("</td></tr></table>\n");
+ if (ctx->qry.vpath) {
+ html("<div class='path'>");
+ html("path: ");
+ cgit_print_path_crumbs(ctx, ctx->qry.vpath);
+ html("</div>");
+ }
+ html("<div class='content'>");
+}
+
+void cgit_print_filemode(unsigned short mode)
+{
+ if (S_ISDIR(mode))
+ html("d");
+ else if (S_ISLNK(mode))
+ html("l");
+ else if (S_ISGITLINK(mode))
+ html("m");
+ else
+ html("-");
+ html_fileperm(mode >> 6);
+ html_fileperm(mode >> 3);
+ html_fileperm(mode);
+}
+
+void cgit_print_snapshot_links(const char *repo, const char *head,
+ const char *hex, int snapshots)
+{
+ const struct cgit_snapshot_format* f;
+ struct strbuf filename = STRBUF_INIT;
+ size_t prefixlen;
+ unsigned char sha1[20];
+
+ if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
+ (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
+ hex++;
+ strbuf_addf(&filename, "%s-%s", cgit_repobasename(repo), hex);
+ prefixlen = filename.len;
+ for (f = cgit_snapshot_formats; f->suffix; f++) {
+ if (!(snapshots & f->bit))
+ continue;
+ strbuf_setlen(&filename, prefixlen);
+ strbuf_addstr(&filename, f->suffix);
+ cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
+ filename.buf);
+ html("<br/>");
+ }
+ strbuf_release(&filename);
+}
+
+static void print_line_raw(char *line, int len)
+{
+ char c = line[len-1];
+
+ line[len-1] = '\0';
+ htmlf("%s\n", line);
+ line[len-1] = c;
+}
+
+static void header_raw(unsigned char *sha1, char *path1, int mode1,
+ unsigned char *sha2, char *path2, int mode2)
+{
+ char *abbrev1, *abbrev2;
+ int subproject;
+
+ subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
+ htmlf("diff --git a/%s b/%s\n", path1, path2);
+
+ if (mode1 == 0)
+ htmlf("new file mode %.6o\n", mode2);
+
+ if (mode2 == 0)
+ htmlf("deleted file mode %.6o\n", mode1);
+
+ if (!subproject) {
+ abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
+ abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
+ htmlf("index %s..%s", abbrev1, abbrev2);
+ free(abbrev1);
+ free(abbrev2);
+ if (mode1 != 0 && mode2 != 0) {
+ htmlf(" %.6o", mode1);
+ if (mode2 != mode1)
+ htmlf("..%.6o", mode2);