[PATCH v2] guess default branch from HEAD

Lars Hjemli hjemli at gmail.com
Thu Apr 7 11:44:46 CEST 2011


On Wed, Mar 30, 2011 at 18:00, Julius Plenz <plenz at cis.fu-berlin.de> wrote:
> @@ -105,6 +107,25 @@ static void add_repo(const char *base, const char *path, repo_config_fn fn)
>                        *p = '\0';
>        repo->name = repo->url;
>        repo->path = xstrdup(path);
> +
> +       fd = open(fmt("%s/HEAD", repo->path), O_RDONLY);
> +       if (fd != -1) {
> +               int len;
> +               memset(buffer, 0, sizeof(buffer)-1);
> +               len = read_in_full(fd, buffer, sizeof(buffer)-1);
> +               if(!memcmp(buffer, "ref: refs/heads/", 16)) {
> +                       repo->defbranch = xstrndup(buffer+16, len-17);
> +               } else if(strlen(buffer) == 41) { /* probably contains a SHA1 sum */
> +                       memset(buffer, 0, sizeof(buffer)-1);
> +                       readlink(fmt("%s/HEAD", repo->path), buffer, sizeof(buffer)-1);
> +                       char *ref_start;
> +                       ref_start = memmem(buffer, sizeof(buffer)-1, "refs/heads/", 11);
> +                       if(ref_start)
> +                               repo->defbranch = xstrdup(ref_start+11);
> +               }
> +               close(fd);
> +       }
> +

Thanks.

This seems like a workable solution, but with two issues:
* The feature only works for repos added by scan-path
* I prefer to declare variables at the start of the function

Both issues can thus be solved by a simple refactoring.

--
larsh


More information about the CGit mailing list