#include "cache.h"
#include "refs.h"
#include "object.h"
#include "tag.h"
/* ISSYMREF=01 and ISPACKED=02 are public interfaces */
#define REF_KNOWS_PEELED 04
struct ref_list {
struct ref_list *next;
unsigned char flag; /* ISSYMREF? ISPACKED? */
unsigned char sha1[20];
unsigned char peeled[20];
char name[FLEX_ARRAY];
};
static const char *parse_ref_line(char *line, unsigned char *sha1)
{
/*
* 42: the answer to everything.
*
* In this case, it happens to be the answer to
* 40 (length of sha1 hex representation)
* +1 (space in between hex and name)
* +1 (newline at the end of the line)
*/
int len = strlen(line) - 42;
if (len <= 0)
return NULL;
if (get_sha1_hex(line, sha1) < 0)
return NULL;
if (!isspace(line[40]))
return NULL;
line += 41;
if (isspace(*line))
return NULL;
if (line[len] != '\n')
return NULL;
line[len] = 0;
return line;
}
static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
int flag, struct ref_list *list,
struct ref_list **new_entry)
{
int len;
struct ref_list **p = &list, *entry;
/* Find the place to insert the ref into.. */
while ((entry = *p) != NULL) {
int cmp = strcmp(entry->name, name);
if (cmp > 0)
break;
/* Same as existing entry? */
if (!cmp) {
if (new_entry)
*new_entry = entry;
return list;
}
p = &entry->next;
}
/* Allocate it and add it in.. */
len = strlen(name) + 1;
entry = xmalloc(sizeof(struct ref_list) + len);
hashcpy(entry->sha1, sha1);
hashclr(entry->peeled);
memcpy(entry->name, name, len);
entry->flag = flag;
entry->next = *p;
*p = entry;
if (new_entry)
*new_entry = entry;
return list;
}
/*
* Future: need to be in "struct repository"
* when doing a full libification.
*/
struct cached_refs {
char did_loose;
char did_packed;
struct ref_list *loose;
struct ref_list *packed;
} cached_refs;
static void free_ref_list(struct ref_list *list)
{
struct ref_list *next;
for ( ; list; list = next) {
next = list->next;
free(list);
}
}
static void invalidate_cached_refs(void)
{
struct cached_refs *ca = &cached_refs;
if (ca->did_loose && ca->loose)
free_ref_list(ca->loose);
if (ca->did_packed && ca->packed)
free_ref_list(ca->packed);
ca->loose = ca->packed = NULL;
ca->did_loose = ca->did_packed = 0;
}
static void read_packed_refs(FILE *f, struct cached_refs *cached_refs)
{
struct ref_list *list = NULL;
struct ref_list *last = NULL;
char refline[PATH_MAX];
int flag = REF_ISPACKED;
while (fgets(refline, sizeof(refline), f)) {
unsigned char sha1[20];
const char *name;
static const char header[] = "# pack-refs with:";
if (!strncmp(refline, header, sizeof(header)-1)) {
const char *traits = refline + sizeof(header) - 1;
if (strstr(traits, " peeled "))
flag |= REF_KNOWS_PEELED;
/* perhaps other traits later as well */
continue;
}
name = parse_ref_line(refline, sha1);
if (name) {
list = add_ref(name, sha1, flag, list, &last);
continue;
}
if (last &&
refline[0] == '^' &&
strlen(refline) == 42 &&
refline[41] == '\n' &&
!get_sha1_hex(refline + 1, sha1))
hashcpy(last->peeled, sha1);
}
cached_refs->packed = list;
}
static struct ref_list *get_packed_refs(void)
{
if (!cached_refs.did_packed) {
FILE *f = fopen(git_path("packed-refs"), "r");
cached_refs.packed = NULL;
if (f) {
read_packed_refs(f, &cached_refs);
fclose(f);
}
cached_refs.did_packed = 1;
}
return cached_refs.packed;
}
static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
{
DIR *dir = opendir(git_path("%s", base));
if (dir) {
struct dirent *de;
int baselen = strlen(base);
char *ref = xmalloc(baselen + 257);
memcpy(ref, base, baselen);
if (baselen && base[baselen-1] != '/')
ref[baselen++] = '/';
while ((de = readdir(dir)) != NULL) {
unsigned char sha1[20];
struct stat st;
int flag;
int namelen;
if (de->d_name[0] == '.')
continue;
namelen = strlen(de->d_name);
if (namelen >
|