[PATCH 1/5] guess default branch from HEAD

Lars Hjemli hjemli at gmail.com
Thu Mar 10 18:22:59 CET 2011


On Thu, Mar 10, 2011 at 17:03, Julius Plenz <plenz at cis.fu-berlin.de> wrote:
> This is a saner alternative than hardcoding the default branch to be
> "master". The add_repo() function will now check for a symbolic ref in
> repo_path/HEAD. If there is a suitable one, overwrite repo->defbranch
> with it.

I agree with the motivation, but...

> --- a/scan-tree.c
> +++ b/scan-tree.c
> @@ -75,6 +75,8 @@ static void add_repo(const char *base, const char *path, repo_config_fn fn)
>        char *rel, *p, *slash;
>        int n;
>        size_t size;
> +       char buffer[256];
> +       int fd;
>
>        if (stat(path, &st)) {
>                fprintf(stderr, "Error accessing %s: %s (%d)\n",
> @@ -105,6 +107,17 @@ 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);
> +               close(fd);
> +       }
> +

...since git supports fs links, you'll need to lstat() and then either
readlink() or read_in_full(). And if you readlink(), you'll obviously
need to parse the result differently.

--
larsh




More information about the CGit mailing list