- 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, 0);
- cgit_print_error(fmt("Unable to scan repository: %s",
- strerror(errno)));
+ 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));