diff --git a/server-info.c b/server-info.c
index 2cd1432249b6de61f9ee4d40dc30258a32e84d94..0d1312ca56d52daa3fc692984d4d3abaf3425791 100644 (file)
--- a/server-info.c
+++ b/server-info.c
/* refs */
static FILE *info_ref_fp;
-static int add_info_ref(const char *path, const unsigned char *sha1)
+static int add_info_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
{
struct object *o = parse_object(sha1);
+ if (!o)
+ return -1;
fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path);
- if (o->type == tag_type) {
+ if (o->type == OBJ_TAG) {
o = deref_tag(o, path, 0);
if (o)
fprintf(info_ref_fp, "%s %s^{}\n",
static int update_info_refs(int force)
{
- char *path0 = strdup(git_path("info/refs"));
+ char *path0 = xstrdup(git_path("info/refs"));
int len = strlen(path0);
char *path1 = xmalloc(len + 2);
info_ref_fp = fopen(path1, "w");
if (!info_ref_fp)
return error("unable to update %s", path0);
- for_each_ref(add_info_ref);
+ for_each_ref(add_info_ref, NULL);
fclose(info_ref_fp);
+ adjust_shared_perm(path1);
rename(path1, path0);
free(path0);
free(path1);
/* packs */
static struct pack_info {
- unsigned long latest;
struct packed_git *p;
int old_num;
int new_num;
static const char *objdir;
static int objdirlen;
-static struct object *parse_object_cheap(const unsigned char *sha1)
-{
- struct object *o;
-
- if ((o = parse_object(sha1)) == NULL)
- return NULL;
- if (o->type == commit_type) {
- struct commit *commit = (struct commit *)o;
- free(commit->buffer);
- commit->buffer = NULL;
- } else if (o->type == tree_type) {
- struct tree *tree = (struct tree *)o;
- struct tree_entry_list *e, *n;
- for (e = tree->entries; e; e = n) {
- free(e->name);
- e->name = NULL;
- n = e->next;
- free(e);
- }
- tree->entries = NULL;
- }
- return o;
-}
-
static struct pack_info *find_pack_by_name(const char *name)
{
int i;
return NULL;
}
-static struct pack_info *find_pack_by_old_num(int old_num)
-{
- int i;
- for (i = 0; i < num_pack; i++)
- if (info[i]->old_num == old_num)
- return info[i];
- return NULL;
-}
-
-static int add_head_def(struct pack_info *this, unsigned char *sha1)
-{
- if (this->nr_alloc <= this->nr_heads) {
- this->nr_alloc = alloc_nr(this->nr_alloc);
- this->head = xrealloc(this->head, this->nr_alloc * 20);
- }
- memcpy(this->head[this->nr_heads++], sha1, 20);
- return 0;
-}
-
/* Returns non-zero when we detect that the info in the
* old file is useless.
*/
}
}
-/* Returns non-zero when we detect that the info in the old file is useless.
- */
-static int parse_head_def(char *line)
-{
- unsigned char sha1[20];
- unsigned long num;
- char *cp, *ep;
- struct pack_info *this;
- struct object *o;
-
- cp = line + 2;
- num = strtoul(cp, &ep, 10);
- if (ep == cp || *ep++ != ' ')
- return error("invalid input ix %s", line);
- this = find_pack_by_old_num(num);
- if (!this)
- return 1; /* You know the drill. */
- if (get_sha1_hex(ep, sha1) || ep[40] != ' ')
- return error("invalid input sha1 %s (%s)", line, ep);
- if ((o = parse_object_cheap(sha1)) == NULL)
- return error("no such object: %s", line);
- return add_head_def(this, sha1);
-}
-
/* Returns non-zero when we detect that the info in the
* old file is useless.
*/
fp = fopen(infofile, "r");
if (!fp)
- return 1; /* nonexisting is not an error. */
+ return 1; /* nonexistent is not an error. */
while (fgets(line, sizeof(line), fp)) {
int len = strlen(line);
if (line[len-1] == '\n')
- line[len-1] = 0;
+ line[--len] = 0;
+
+ if (!len)
+ continue;
switch (line[0]) {
case 'P': /* P name */
case 'D': /* we used to emit D but that was misguided. */
goto out_stale;
break;
- case 'T': /* T ix sha1 type */
- if (parse_head_def(line))
- goto out_stale;
+ case 'T': /* we used to emit T but nobody uses it. */
+ goto out_stale;
break;
default:
error("unrecognized: %s", line);
return 1;
}
-/* We sort the packs according to the date of the latest commit. That
- * in turn indicates how young the pack is, and in general we would
- * want to depend on younger packs.
- */
-static unsigned long get_latest_commit_date(struct packed_git *p)
-{
- unsigned char sha1[20];
- struct object *o;
- int num = num_packed_objects(p);
- int i;
- unsigned long latest = 0;
-
- for (i = 0; i < num; i++) {
- if (nth_packed_object_sha1(p, i, sha1))
- die("corrupt pack file %s?", p->pack_name);
- if ((o = parse_object_cheap(sha1)) == NULL)
- die("cannot parse %s", sha1_to_hex(sha1));
- if (o->type == commit_type) {
- struct commit *commit = (struct commit *)o;
- if (latest < commit->date)
- latest = commit->date;
- }
- }
- return latest;
-}
-
static int compare_info(const void *a_, const void *b_)
{
struct pack_info * const* a = a_;
/* The other way around. */
return 1;
- if ((*a)->latest < (*b)->latest)
- return -1;
- else if ((*a)->latest == (*b)->latest)
+ /* then it does not matter but at least keep the comparison stable */
+ if ((*a)->p == (*b)->p)
return 0;
+ else if ((*a)->p < (*b)->p)
+ return -1;
else
return 1;
}
/* we ignore things on alternate path since they are
* not available to the pullers in general.
*/
- if (strncmp(p->pack_name, objdir, objdirlen) ||
- strncmp(p->pack_name + objdirlen, "/pack/", 6))
+ if (!p->pack_local)
continue;
i++;
}
num_pack = i;
info = xcalloc(num_pack, sizeof(struct pack_info *));
for (i = 0, p = packed_git; p; p = p->next) {
- if (strncmp(p->pack_name, objdir, objdirlen) ||
- p->pack_name[objdirlen] != '/')
+ if (!p->pack_local)
continue;
info[i] = xcalloc(1, sizeof(struct pack_info));
info[i]->p = p;
info[i]->old_num = -1;
info[i]->nr_heads = 0;
}
- if (info[i]->old_num < 0)
- info[i]->latest = get_latest_commit_date(info[i]->p);
}
/* renumber them */
static void write_pack_info_file(FILE *fp)
{
- int i, j;
+ int i;
for (i = 0; i < num_pack; i++)
fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
- for (i = 0; i < num_pack; i++) {
- struct pack_info *this = info[i];
- for (j = 0; j < this->nr_heads; j++) {
- struct object *o = lookup_object(this->head[j]);
- fprintf(fp, "T %1d %s %s\n",
- i, sha1_to_hex(this->head[j]), o->type);
- }
- }
-
-}
-
-#define REFERENCED 01
-#define EMITTED 04
-
-static void show(struct object *o, int pack_ix)
-{
- /*
- * We are interested in objects that are not referenced,
- */
- if (o->flags & EMITTED)
- return;
-
- if (!(o->flags & REFERENCED))
- add_head_def(info[pack_ix], o->sha1);
- o->flags |= EMITTED;
-}
-
-static void find_pack_info_one(int pack_ix)
-{
- unsigned char sha1[20];
- struct object *o;
- int i;
- struct packed_git *p = info[pack_ix]->p;
- int num = num_packed_objects(p);
-
- /* Scan objects, clear flags from all the edge ones and
- * internal ones, possibly marked in the previous round.
- */
- for (i = 0; i < num; i++) {
- if (nth_packed_object_sha1(p, i, sha1))
- die("corrupt pack file %s?", p->pack_name);
- if ((o = lookup_object(sha1)) == NULL)
- die("cannot parse %s", sha1_to_hex(sha1));
- if (o->refs) {
- struct object_refs *refs = o->refs;
- int j;
- for (j = 0; j < refs->count; j++)
- refs->ref[j]->flags = 0;
- }
- o->flags = 0;
- }
-
- /* Mark all the referenced ones */
- for (i = 0; i < num; i++) {
- if (nth_packed_object_sha1(p, i, sha1))
- die("corrupt pack file %s?", p->pack_name);
- if ((o = lookup_object(sha1)) == NULL)
- die("cannot find %s", sha1_to_hex(sha1));
- if (o->refs) {
- struct object_refs *refs = o->refs;
- int j;
- for (j = 0; j < refs->count; j++)
- refs->ref[j]->flags |= REFERENCED;
- }
- }
-
- for (i = 0; i < num; i++) {
- if (nth_packed_object_sha1(p, i, sha1))
- die("corrupt pack file %s?", p->pack_name);
- if ((o = lookup_object(sha1)) == NULL)
- die("cannot find %s", sha1_to_hex(sha1));
- show(o, pack_ix);
- }
-
-}
-
-static void find_pack_info(void)
-{
- int i;
- for (i = 0; i < num_pack; i++) {
- /* The packed objects are cast in stone, and a head
- * in a pack will stay as head, so is the set of missing
- * objects. If the repo has been reorganized and we
- * are missing some packs available back then, we have
- * already discarded the info read from the file, so
- * we will find (old_num < 0) in that case.
- */
- if (0 <= info[i]->old_num)
- continue;
- find_pack_info_one(i);
- }
+ fputc('\n', fp);
}
static int update_info_packs(int force)
strcpy(name + namelen, "+");
init_pack_info(infofile, force);
- find_pack_info();
safe_create_leading_directories(name);
fp = fopen(name, "w");
return error("cannot open %s", name);
write_pack_info_file(fp);
fclose(fp);
+ adjust_shared_perm(name);
rename(name, infofile);
return 0;
}