- if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
- cgit_read_config("info/cgit", cgit_repo_config_cb)) {
- char *title = fmt("%s - %s", cgit_root_title, "Bad request");
- cgit_print_docstart(title, item);
- cgit_print_pageheader(title);
- cgit_print_error(fmt("Unable to scan repository: %s",
- strerror(errno)));
+ struct refmatch *info;
+
+ info = (struct refmatch *)cb_data;
+ if (!strcmp(refname, info->req_ref))
+ info->match = 1;
+ if (!info->first_ref)
+ info->first_ref = xstrdup(refname);
+ return info->match;
+}
+
+char *find_default_branch(struct cgit_repo *repo)
+{
+ struct refmatch info;
+
+ info.req_ref = repo->defbranch;
+ info.first_ref = NULL;
+ info.match = 0;
+ for_each_branch_ref(find_current_ref, &info);
+ if (info.match)
+ return info.req_ref;
+ else
+ return info.first_ref;
+}
+
+static void cgit_print_repo_page()
+{
+ char *tmp;
+ int show_search;
+ unsigned char sha1[20];
+ int nongit = 0;
+
+ setenv("GIT_DIR", ctx.repo->path, 1);
+ setup_git_directory_gently(&nongit);
+ if (nongit) {
+ ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
+ "config error");
+ tmp = fmt("Not a git repository: '%s'", ctx.repo->path);
+ ctx.repo = NULL;
+ cgit_print_http_headers(&ctx);
+ cgit_print_docstart(&ctx);
+ cgit_print_pageheader(&ctx);
+ cgit_print_error(tmp);
+ cgit_print_docend();
+ return;
+ }
+
+ ctx.page.title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc);
+ show_search = 0;
+
+ if (!ctx.qry.head) {
+ ctx.qry.head = xstrdup(find_default_branch(ctx.repo));
+ ctx.repo->defbranch = ctx.qry.head;
+ }
+
+ if (!ctx.qry.head) {
+ cgit_print_http_headers(&ctx);
+ cgit_print_docstart(&ctx);
+ cgit_print_pageheader(&ctx);
+ cgit_print_error("Repository seems to be empty");
+ cgit_print_docend();
+ return;
+ }
+
+ if (get_sha1(ctx.qry.head, sha1)) {
+ tmp = xstrdup(ctx.qry.head);
+ ctx.qry.head = ctx.repo->defbranch;
+ cgit_print_http_headers(&ctx);
+ cgit_print_docstart(&ctx);
+ cgit_print_pageheader(&ctx);
+ cgit_print_error(fmt("Invalid branch: %s", tmp));