8 static FILE *info_ref_fp;
10 static int add_info_ref(const char *path, const unsigned char *sha1)
12 struct object *o = parse_object(sha1);
14 fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path);
15 if (o->type == tag_type) {
17 fprintf(info_ref_fp, "%s %s^{}\n",
18 sha1_to_hex(o->sha1), path);
23 static int update_info_refs(int force)
25 char *path0 = strdup(git_path("info/refs"));
26 int len = strlen(path0);
27 char *path1 = xmalloc(len + 2);
30 strcpy(path1 + len, "+");
32 safe_create_leading_directories(path0);
33 info_ref_fp = fopen(path1, "w");
35 return error("unable to update %s", path0);
36 for_each_ref(add_info_ref);
45 static struct pack_info {
52 unsigned char (*head)[20];
53 char dep[0]; /* more */
56 static const char *objdir;
59 static struct object *parse_object_cheap(const unsigned char *sha1)
63 if ((o = parse_object(sha1)) == NULL)
65 if (o->type == commit_type) {
66 struct commit *commit = (struct commit *)o;
68 commit->buffer = NULL;
69 } else if (o->type == tree_type) {
70 struct tree *tree = (struct tree *)o;
71 struct tree_entry_list *e, *n;
72 for (e = tree->entries; e; e = n) {
83 static struct pack_info *find_pack_by_name(const char *name)
86 for (i = 0; i < num_pack; i++) {
87 struct packed_git *p = info[i]->p;
88 /* skip "/pack/" after ".git/objects" */
89 if (!strcmp(p->pack_name + objdirlen + 6, name))
95 static struct pack_info *find_pack_by_old_num(int old_num)
98 for (i = 0; i < num_pack; i++)
99 if (info[i]->old_num == old_num)
104 static int add_head_def(struct pack_info *this, unsigned char *sha1)
106 if (this->nr_alloc <= this->nr_heads) {
107 this->nr_alloc = alloc_nr(this->nr_alloc);
108 this->head = xrealloc(this->head, this->nr_alloc * 20);
110 memcpy(this->head[this->nr_heads++], sha1, 20);
114 /* Returns non-zero when we detect that the info in the
115 * old file is useless.
117 static int parse_pack_def(const char *line, int old_cnt)
119 struct pack_info *i = find_pack_by_name(line + 2);
121 i->old_num = old_cnt;
125 /* The file describes a pack that is no longer here;
126 * dependencies between packs needs to be recalculated.
132 /* Returns non-zero when we detect that the info in the
133 * old file is useless.
135 static int parse_depend_def(char *line)
139 struct pack_info *this, *that;
142 num = strtoul(cp, &ep, 10);
144 return error("invalid input %s", line);
145 this = find_pack_by_old_num(num);
148 while (ep && *(cp = ep)) {
149 num = strtoul(cp, &ep, 10);
152 that = find_pack_by_old_num(num);
154 /* The pack this one depends on does not
155 * exist; this should not happen because
156 * we write out the list of packs first and
157 * then dependency information, but it means
158 * the file is useless anyway.
161 this->dep[that->new_num] = 1;
166 /* Returns non-zero when we detect that the info in the
167 * old file is useless.
169 static int parse_head_def(char *line)
171 unsigned char sha1[20];
174 struct pack_info *this;
178 num = strtoul(cp, &ep, 10);
179 if (ep == cp || *ep++ != ' ')
180 return error("invalid input ix %s", line);
181 this = find_pack_by_old_num(num);
183 return 1; /* You know the drill. */
184 if (get_sha1_hex(ep, sha1) || ep[40] != ' ')
185 return error("invalid input sha1 %s (%s)", line, ep);
186 if ((o = parse_object_cheap(sha1)) == NULL)
187 return error("no such object: %s", line);
188 return add_head_def(this, sha1);
191 /* Returns non-zero when we detect that the info in the
192 * old file is useless.
194 static int read_pack_info_file(const char *infofile)
200 fp = fopen(infofile, "r");
202 return 1; /* nonexisting is not an error. */
204 while (fgets(line, sizeof(line), fp)) {
205 int len = strlen(line);
206 if (line[len-1] == '\n')
210 case 'P': /* P name */
211 if (parse_pack_def(line, old_cnt++))
214 case 'D': /* D ix dep-ix1 dep-ix2... */
215 if (parse_depend_def(line))
218 case 'T': /* T ix sha1 type */
219 if (parse_head_def(line))
223 error("unrecognized: %s", line);
234 /* We sort the packs according to the date of the latest commit. That
235 * in turn indicates how young the pack is, and in general we would
236 * want to depend on younger packs.
238 static unsigned long get_latest_commit_date(struct packed_git *p)
240 unsigned char sha1[20];
242 int num = num_packed_objects(p);
244 unsigned long latest = 0;
246 for (i = 0; i < num; i++) {
247 if (nth_packed_object_sha1(p, i, sha1))
248 die("corrupt pack file %s?", p->pack_name);
249 if ((o = parse_object_cheap(sha1)) == NULL)
250 die("cannot parse %s", sha1_to_hex(sha1));
251 if (o->type == commit_type) {
252 struct commit *commit = (struct commit *)o;
253 if (latest < commit->date)
254 latest = commit->date;
260 static int compare_info(const void *a_, const void *b_)
262 struct pack_info * const* a = a_;
263 struct pack_info * const* b = b_;
265 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
266 /* Keep the order in the original */
267 return (*a)->old_num - (*b)->old_num;
268 else if (0 <= (*a)->old_num)
269 /* Only A existed in the original so B is obviously newer */
271 else if (0 <= (*b)->old_num)
272 /* The other way around. */
275 if ((*a)->latest < (*b)->latest)
277 else if ((*a)->latest == (*b)->latest)
283 static void init_pack_info(const char *infofile, int force)
285 struct packed_git *p;
290 objdir = get_object_directory();
291 objdirlen = strlen(objdir);
293 prepare_packed_git();
294 for (p = packed_git; p; p = p->next) {
295 /* we ignore things on alternate path since they are
296 * not available to the pullers in general.
298 if (strncmp(p->pack_name, objdir, objdirlen) ||
299 strncmp(p->pack_name + objdirlen, "/pack/", 6))
304 info = xcalloc(num_pack, sizeof(struct pack_info *));
305 for (i = 0, p = packed_git; p; p = p->next) {
306 if (strncmp(p->pack_name, objdir, objdirlen) ||
307 p->pack_name[objdirlen] != '/')
309 info[i] = xcalloc(1, sizeof(struct pack_info) + num_pack);
311 info[i]->old_num = -1;
315 if (infofile && !force)
316 stale = read_pack_info_file(infofile);
320 for (i = 0; i < num_pack; i++) {
322 info[i]->old_num = -1;
323 memset(info[i]->dep, 0, num_pack);
324 info[i]->nr_heads = 0;
326 if (info[i]->old_num < 0)
327 info[i]->latest = get_latest_commit_date(info[i]->p);
330 qsort(info, num_pack, sizeof(info[0]), compare_info);
331 for (i = 0; i < num_pack; i++)
332 info[i]->new_num = i;
334 /* we need to fix up the dependency information
338 for (i = 0; i < num_pack; i++) {
341 if (info[i]->old_num < 0)
344 dep_temp = xmalloc(num_pack);
345 memset(dep_temp, 0, num_pack);
346 for (old = 0; old < num_pack; old++) {
347 struct pack_info *base;
348 if (!info[i]->dep[old])
350 base = find_pack_by_old_num(old);
352 die("internal error renumbering");
353 dep_temp[base->new_num] = 1;
355 memcpy(info[i]->dep, dep_temp, num_pack);
360 static void write_pack_info_file(FILE *fp)
363 for (i = 0; i < num_pack; i++)
364 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
366 for (i = 0; i < num_pack; i++) {
367 fprintf(fp, "D %1d", i);
368 for (j = 0; j < num_pack; j++) {
369 if ((i == j) || !(info[i]->dep[j]))
371 fprintf(fp, " %1d", j);
376 for (i = 0; i < num_pack; i++) {
377 struct pack_info *this = info[i];
378 for (j = 0; j < this->nr_heads; j++) {
379 struct object *o = lookup_object(this->head[j]);
380 fprintf(fp, "T %1d %s %s\n",
381 i, sha1_to_hex(this->head[j]), o->type);
387 #define REFERENCED 01
391 static void show(struct object *o, int pack_ix)
394 * We are interested in objects that are not referenced,
395 * and objects that are referenced but not internal.
397 if (o->flags & EMITTED)
400 if (!(o->flags & REFERENCED))
401 add_head_def(info[pack_ix], o->sha1);
402 else if ((o->flags & REFERENCED) && !(o->flags & INTERNAL)) {
405 /* Which pack contains this object? That is what
406 * pack_ix can depend on. We earlier sorted info
407 * array from youngest to oldest, so try newer packs
408 * first to favor them here.
410 for (i = num_pack - 1; 0 <= i; i--) {
411 struct packed_git *p = info[i]->p;
412 struct pack_entry ent;
413 if (find_pack_entry_one(o->sha1, &ent, p)) {
414 info[pack_ix]->dep[i] = 1;
422 static void find_pack_info_one(int pack_ix)
424 unsigned char sha1[20];
426 struct object_list *ref;
428 struct packed_git *p = info[pack_ix]->p;
429 int num = num_packed_objects(p);
431 /* Scan objects, clear flags from all the edge ones and
432 * internal ones, possibly marked in the previous round.
434 for (i = 0; i < num; i++) {
435 if (nth_packed_object_sha1(p, i, sha1))
436 die("corrupt pack file %s?", p->pack_name);
437 if ((o = lookup_object(sha1)) == NULL)
438 die("cannot parse %s", sha1_to_hex(sha1));
439 for (ref = o->refs; ref; ref = ref->next)
440 ref->item->flags = 0;
444 /* Mark all the internal ones */
445 for (i = 0; i < num; i++) {
446 if (nth_packed_object_sha1(p, i, sha1))
447 die("corrupt pack file %s?", p->pack_name);
448 if ((o = lookup_object(sha1)) == NULL)
449 die("cannot find %s", sha1_to_hex(sha1));
450 for (ref = o->refs; ref; ref = ref->next)
451 ref->item->flags |= REFERENCED;
452 o->flags |= INTERNAL;
455 for (i = 0; i < num; i++) {
456 if (nth_packed_object_sha1(p, i, sha1))
457 die("corrupt pack file %s?", p->pack_name);
458 if ((o = lookup_object(sha1)) == NULL)
459 die("cannot find %s", sha1_to_hex(sha1));
462 for (ref = o->refs; ref; ref = ref->next)
463 show(ref->item, pack_ix);
468 static void find_pack_info(void)
471 for (i = 0; i < num_pack; i++) {
472 /* The packed objects are cast in stone, and a head
473 * in a pack will stay as head, so is the set of missing
474 * objects. If the repo has been reorganized and we
475 * are missing some packs available back then, we have
476 * already discarded the info read from the file, so
477 * we will find (old_num < 0) in that case.
479 if (0 <= info[i]->old_num)
481 find_pack_info_one(i);
485 static int update_info_packs(int force)
487 char infofile[PATH_MAX];
492 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
493 strcpy(name, infofile);
494 strcpy(name + namelen, "+");
496 init_pack_info(infofile, force);
499 safe_create_leading_directories(name);
500 fp = fopen(name, "w");
502 return error("cannot open %s", name);
503 write_pack_info_file(fp);
505 rename(name, infofile);
510 int update_server_info(int force)
512 /* We would add more dumb-server support files later,
513 * including index of available pack files and their
514 * intended audiences.
518 errs = errs | update_info_refs(force);
519 errs = errs | update_info_packs(force);