From mboxrd@z Thu Jan 1 00:00:00 1970 From: hjemli at gmail.com (Lars Hjemli) Date: Thu, 10 Mar 2011 18:22:59 +0100 Subject: [PATCH 1/5] guess default branch from HEAD In-Reply-To: <1299773004-27380-1-git-send-email-plenz@cis.fu-berlin.de> References: <1299773004-27380-1-git-send-email-plenz@cis.fu-berlin.de> Message-ID: On Thu, Mar 10, 2011 at 17:03, Julius Plenz 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