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) {
16 o = deref_tag(o, path, 0);
18 fprintf(info_ref_fp, "%s %s^{}\n",
19 sha1_to_hex(o->sha1), path);
24 static int update_info_refs(int force)
26 char *path0 = strdup(git_path("info/refs"));
27 int len = strlen(path0);
28 char *path1 = xmalloc(len + 2);
31 strcpy(path1 + len, "+");
33 safe_create_leading_directories(path0);
34 info_ref_fp = fopen(path1, "w");
36 return error("unable to update %s", path0);
37 for_each_ref(add_info_ref);
46 static struct pack_info {
53 unsigned char (*head)[20];
54 char dep[0]; /* more */
57 static const char *objdir;
60 static struct object *parse_object_cheap(const unsigned char *sha1)
64 if ((o = parse_object(sha1)) == NULL)
66 if (o->type == commit_type) {
67 struct commit *commit = (struct commit *)o;
69 commit->buffer = NULL;
70 } else if (o->type == tree_type) {
71 struct tree *tree = (struct tree *)o;
72 struct tree_entry_list *e, *n;
73 for (e = tree->entries; e; e = n) {
84 static struct pack_info *find_pack_by_name(const char *name)
87 for (i = 0; i < num_pack; i++) {
88 struct packed_git *p = info[i]->p;
89 /* skip "/pack/" after ".git/objects" */
90 if (!strcmp(p->pack_name + objdirlen + 6, name))
96 static struct pack_info *find_pack_by_old_num(int old_num)
99 for (i = 0; i < num_pack; i++)
100 if (info[i]->old_num == old_num)
105 static int add_head_def(struct pack_info *this, unsigned char *sha1)
107 if (this->nr_alloc <= this->nr_heads) {
108 this->nr_alloc = alloc_nr(this->nr_alloc);
109 this->head = xrealloc(this->head, this->nr_alloc * 20);
111 memcpy(this->head[this->nr_heads++], sha1, 20);
115 /* Returns non-zero when we detect that the info in the
116 * old file is useless.
118 static int parse_pack_def(const char *line, int old_cnt)
120 struct pack_info *i = find_pack_by_name(line + 2);
122 i->old_num = old_cnt;
126 /* The file describes a pack that is no longer here;
127 * dependencies between packs needs to be recalculated.
133 /* Returns non-zero when we detect that the info in the
134 * old file is useless.
136 static int parse_depend_def(char *line)
140 struct pack_info *this, *that;
143 num = strtoul(cp, &ep, 10);
145 return error("invalid input %s", line);
146 this = find_pack_by_old_num(num);
149 while (ep && *(cp = ep)) {
150 num = strtoul(cp, &ep, 10);
153 that = find_pack_by_old_num(num);
155 /* The pack this one depends on does not
156 * exist; this should not happen because
157 * we write out the list of packs first and
158 * then dependency information, but it means
159 * the file is useless anyway.
162 this->dep[that->new_num] = 1;
167 /* Returns non-zero when we detect that the info in the
168 * old file is useless.
170 static int parse_head_def(char *line)
172 unsigned char sha1[20];
175 struct pack_info *this;
179 num = strtoul(cp, &ep, 10);
180 if (ep == cp || *ep++ != ' ')
181 return error("invalid input ix %s", line);
182 this = find_pack_by_old_num(num);
184 return 1; /* You know the drill. */
185 if (get_sha1_hex(ep, sha1) || ep[40] != ' ')
186 return error("invalid input sha1 %s (%s)", line, ep);
187 if ((o = parse_object_cheap(sha1)) == NULL)
188 return error("no such object: %s", line);
189 return add_head_def(this, sha1);
192 /* Returns non-zero when we detect that the info in the
193 * old file is useless.
195 static int read_pack_info_file(const char *infofile)
201 fp = fopen(infofile, "r");
203 return 1; /* nonexisting is not an error. */
205 while (fgets(line, sizeof(line), fp)) {
206 int len = strlen(line);
207 if (line[len-1] == '\n')
211 case 'P': /* P name */
212 if (parse_pack_def(line, old_cnt++))
215 case 'D': /* D ix dep-ix1 dep-ix2... */
216 if (parse_depend_def(line))
219 case 'T': /* T ix sha1 type */
220 if (parse_head_def(line))
224 error("unrecognized: %s", line);
235 /* We sort the packs according to the date of the latest commit. That
236 * in turn indicates how young the pack is, and in general we would
237 * want to depend on younger packs.
239 static unsigned long get_latest_commit_date(struct packed_git *p)
241 unsigned char sha1[20];
243 int num = num_packed_objects(p);
245 unsigned long latest = 0;
247 for (i = 0; i < num; i++) {
248 if (nth_packed_object_sha1(p, i, sha1))
249 die("corrupt pack file %s?", p->pack_name);
250 if ((o = parse_object_cheap(sha1)) == NULL)
251 die("cannot parse %s", sha1_to_hex(sha1));
252 if (o->type == commit_type) {
253 struct commit *commit = (struct commit *)o;
254 if (latest < commit->date)
255 latest = commit->date;
261 static int compare_info(const void *a_, const void *b_)
263 struct pack_info * const* a = a_;
264 struct pack_info * const* b = b_;
266 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
267 /* Keep the order in the original */
268 return (*a)->old_num - (*b)->old_num;
269 else if (0 <= (*a)->old_num)
270 /* Only A existed in the original so B is obviously newer */
272 else if (0 <= (*b)->old_num)
273 /* The other way around. */
276 if ((*a)->latest < (*b)->latest)
278 else if ((*a)->latest == (*b)->latest)
284 static void init_pack_info(const char *infofile, int force)
286 struct packed_git *p;
291 objdir = get_object_directory();
292 objdirlen = strlen(objdir);
294 prepare_packed_git();
295 for (p = packed_git; p; p = p->next) {
296 /* we ignore things on alternate path since they are
297 * not available to the pullers in general.
299 if (strncmp(p->pack_name, objdir, objdirlen) ||
300 strncmp(p->pack_name + objdirlen, "/pack/", 6))
305 info = xcalloc(num_pack, sizeof(struct pack_info *));
306 for (i = 0, p = packed_git; p; p = p->next) {
307 if (strncmp(p->pack_name, objdir, objdirlen) ||
308 p->pack_name[objdirlen] != '/')
310 info[i] = xcalloc(1, sizeof(struct pack_info) + num_pack);
312 info[i]->old_num = -1;
316 if (infofile && !force)
317 stale = read_pack_info_file(infofile);
321 for (i = 0; i < num_pack; i++) {
323 info[i]->old_num = -1;
324 memset(info[i]->dep, 0, num_pack);
325 info[i]->nr_heads = 0;
327 if (info[i]->old_num < 0)
328 info[i]->latest = get_latest_commit_date(info[i]->p);
331 qsort(info, num_pack, sizeof(info[0]), compare_info);
332 for (i = 0; i < num_pack; i++)
333 info[i]->new_num = i;
335 /* we need to fix up the dependency information
339 for (i = 0; i < num_pack; i++) {
342 if (info[i]->old_num < 0)
345 dep_temp = xmalloc(num_pack);
346 memset(dep_temp, 0, num_pack);
347 for (old = 0; old < num_pack; old++) {
348 struct pack_info *base;
349 if (!info[i]->dep[old])
351 base = find_pack_by_old_num(old);
353 die("internal error renumbering");
354 dep_temp[base->new_num] = 1;
356 memcpy(info[i]->dep, dep_temp, num_pack);
361 static void write_pack_info_file(FILE *fp)
364 for (i = 0; i < num_pack; i++)
365 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
367 for (i = 0; i < num_pack; i++) {
368 fprintf(fp, "D %1d", i);
369 for (j = 0; j < num_pack; j++) {
370 if ((i == j) || !(info[i]->dep[j]))
372 fprintf(fp, " %1d", j);
377 for (i = 0; i < num_pack; i++) {
378 struct pack_info *this = info[i];
379 for (j = 0; j < this->nr_heads; j++) {
380 struct object *o = lookup_object(this->head[j]);
381 fprintf(fp, "T %1d %s %s\n",
382 i, sha1_to_hex(this->head[j]), o->type);
388 #define REFERENCED 01
392 static void show(struct object *o, int pack_ix)
395 * We are interested in objects that are not referenced,
396 * and objects that are referenced but not internal.
398 if (o->flags & EMITTED)
401 if (!(o->flags & REFERENCED))
402 add_head_def(info[pack_ix], o->sha1);
403 else if ((o->flags & REFERENCED) && !(o->flags & INTERNAL)) {
406 /* Which pack contains this object? That is what
407 * pack_ix can depend on. We earlier sorted info
408 * array from youngest to oldest, so try newer packs
409 * first to favor them here.
411 for (i = num_pack - 1; 0 <= i; i--) {
412 struct packed_git *p = info[i]->p;
413 struct pack_entry ent;
414 if (find_pack_entry_one(o->sha1, &ent, p)) {
415 info[pack_ix]->dep[i] = 1;
423 static void find_pack_info_one(int pack_ix)
425 unsigned char sha1[20];
427 struct object_list *ref;
429 struct packed_git *p = info[pack_ix]->p;
430 int num = num_packed_objects(p);
432 /* Scan objects, clear flags from all the edge ones and
433 * internal ones, possibly marked in the previous round.
435 for (i = 0; i < num; i++) {
436 if (nth_packed_object_sha1(p, i, sha1))
437 die("corrupt pack file %s?", p->pack_name);
438 if ((o = lookup_object(sha1)) == NULL)
439 die("cannot parse %s", sha1_to_hex(sha1));
440 for (ref = o->refs; ref; ref = ref->next)
441 ref->item->flags = 0;
445 /* Mark all the internal ones */
446 for (i = 0; i < num; i++) {
447 if (nth_packed_object_sha1(p, i, sha1))
448 die("corrupt pack file %s?", p->pack_name);
449 if ((o = lookup_object(sha1)) == NULL)
450 die("cannot find %s", sha1_to_hex(sha1));
451 for (ref = o->refs; ref; ref = ref->next)
452 ref->item->flags |= REFERENCED;
453 o->flags |= INTERNAL;
456 for (i = 0; i < num; i++) {
457 if (nth_packed_object_sha1(p, i, sha1))
458 die("corrupt pack file %s?", p->pack_name);
459 if ((o = lookup_object(sha1)) == NULL)
460 die("cannot find %s", sha1_to_hex(sha1));
463 for (ref = o->refs; ref; ref = ref->next)
464 show(ref->item, pack_ix);
469 static void find_pack_info(void)
472 for (i = 0; i < num_pack; i++) {
473 /* The packed objects are cast in stone, and a head
474 * in a pack will stay as head, so is the set of missing
475 * objects. If the repo has been reorganized and we
476 * are missing some packs available back then, we have
477 * already discarded the info read from the file, so
478 * we will find (old_num < 0) in that case.
480 if (0 <= info[i]->old_num)
482 find_pack_info_one(i);
486 static int update_info_packs(int force)
488 char infofile[PATH_MAX];
493 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
494 strcpy(name, infofile);
495 strcpy(name + namelen, "+");
497 init_pack_info(infofile, force);
500 safe_create_leading_directories(name);
501 fp = fopen(name, "w");
503 return error("cannot open %s", name);
504 write_pack_info_file(fp);
506 rename(name, infofile);
511 int update_server_info(int force)
513 /* We would add more dumb-server support files later,
514 * including index of available pack files and their
515 * intended audiences.
519 errs = errs | update_info_refs(force);
520 errs = errs | update_info_packs(force);