]> gitweb.ps.run Git - ps-cgit/blobdiff - cgit.c
Only guess default branch when a repo page is requested
[ps-cgit] / cgit.c
diff --git a/cgit.c b/cgit.c
index 6be3754d47d1c75300189ebab68e0a1c6488a139..e3fbbf4a07c00cf117ae912359a3f503971b591d 100644 (file)
--- a/cgit.c
+++ b/cgit.c
@@ -244,6 +244,8 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.robots = xstrdup(value);
        else if (!strcmp(name, "clone-prefix"))
                ctx.cfg.clone_prefix = xstrdup(value);
+       else if (!strcmp(name, "clone-url"))
+               ctx.cfg.clone_url = xstrdup(value);
        else if (!strcmp(name, "local-time"))
                ctx.cfg.local_time = atoi(value);
        else if (!prefixcmp(name, "mimetype."))
@@ -414,18 +416,53 @@ char *find_default_branch(struct cgit_repo *repo)
        return ref;
 }
 
+static char *guess_defbranch(const char *repo_path)
+{
+       int fd, len;
+       char buffer[256];
+       char *ref_start;
+       char *head;
+
+       head = fmt("%s/HEAD", repo_path);
+       fd = open(head, O_RDONLY);
+       if (fd == -1)
+               return xstrdup("master");
+
+       memset(buffer, 0, sizeof(buffer));
+       len = read_in_full(fd, buffer, sizeof(buffer) - 1);
+       close(fd);
+
+       if(!memcmp(buffer, "ref: refs/heads/", 16))
+               return xstrndup(buffer + 16, len - 17);
+
+       if(strlen(buffer) == 41) {
+               /* probably contains a SHA1 sum */
+               memset(buffer, 0, sizeof(buffer));
+               if(readlink(head, buffer, sizeof(buffer)-1)) {
+                       ref_start = memmem(buffer, sizeof(buffer)-1, "refs/heads/", 11);
+                       if(ref_start)
+                               return xstrdup(ref_start+11);
+               }
+       }
+       return xstrdup("master");
+}
+
 static int prepare_repo_cmd(struct cgit_context *ctx)
 {
        char *tmp;
        unsigned char sha1[20];
        int nongit = 0;
+       int rc;
 
        setenv("GIT_DIR", ctx->repo->path, 1);
        setup_git_directory_gently(&nongit);
        if (nongit) {
+               rc = errno;
                ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
                                      "config error");
-               tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
+               tmp = fmt("Failed to open %s: %s",
+                         ctx->repo->name,
+                         rc ? strerror(rc) : "Not a valid git repository");
                ctx->repo = NULL;
                cgit_print_http_headers(ctx);
                cgit_print_docstart(ctx);
@@ -436,10 +473,12 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
        }
        ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
 
+       if (!ctx->repo->defbranch)
+               ctx->repo->defbranch = guess_defbranch(ctx->repo->path);
+
        if (!ctx->qry.head) {
                ctx->qry.nohead = 1;
                ctx->qry.head = find_default_branch(ctx->repo);
-               ctx->repo->defbranch = ctx->qry.head;
        }
 
        if (!ctx->qry.head) {
@@ -463,6 +502,7 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
                cgit_print_docend();
                return 1;
        }
+       cgit_prepare_repo_env(ctx->repo);
        return 0;
 }