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 {
52 unsigned char (*head)[20];
55 static const char *objdir;
58 static struct object *parse_object_cheap(const unsigned char *sha1)
62 if ((o = parse_object(sha1)) == NULL)
64 if (o->type == commit_type) {
65 struct commit *commit = (struct commit *)o;
67 commit->buffer = NULL;
68 } else if (o->type == tree_type) {
69 struct tree *tree = (struct tree *)o;
70 struct tree_entry_list *e, *n;
71 for (e = tree->entries; e; e = n) {
82 static struct pack_info *find_pack_by_name(const char *name)
85 for (i = 0; i < num_pack; i++) {
86 struct packed_git *p = info[i]->p;
87 /* skip "/pack/" after ".git/objects" */
88 if (!strcmp(p->pack_name + objdirlen + 6, name))
94 static struct pack_info *find_pack_by_old_num(int old_num)
97 for (i = 0; i < num_pack; i++)
98 if (info[i]->old_num == old_num)
103 static int add_head_def(struct pack_info *this, unsigned char *sha1)
105 if (this->nr_alloc <= this->nr_heads) {
106 this->nr_alloc = alloc_nr(this->nr_alloc);
107 this->head = xrealloc(this->head, this->nr_alloc * 20);
109 memcpy(this->head[this->nr_heads++], sha1, 20);
113 /* Returns non-zero when we detect that the info in the
114 * old file is useless.
116 static int parse_pack_def(const char *line, int old_cnt)
118 struct pack_info *i = find_pack_by_name(line + 2);
120 i->old_num = old_cnt;
124 /* The file describes a pack that is no longer here */
129 /* Returns non-zero when we detect that the info in the old file is useless.
131 static int parse_head_def(char *line)
133 unsigned char sha1[20];
136 struct pack_info *this;
140 num = strtoul(cp, &ep, 10);
141 if (ep == cp || *ep++ != ' ')
142 return error("invalid input ix %s", line);
143 this = find_pack_by_old_num(num);
145 return 1; /* You know the drill. */
146 if (get_sha1_hex(ep, sha1) || ep[40] != ' ')
147 return error("invalid input sha1 %s (%s)", line, ep);
148 if ((o = parse_object_cheap(sha1)) == NULL)
149 return error("no such object: %s", line);
150 return add_head_def(this, sha1);
153 /* Returns non-zero when we detect that the info in the
154 * old file is useless.
156 static int read_pack_info_file(const char *infofile)
162 fp = fopen(infofile, "r");
164 return 1; /* nonexisting is not an error. */
166 while (fgets(line, sizeof(line), fp)) {
167 int len = strlen(line);
168 if (line[len-1] == '\n')
172 case 'P': /* P name */
173 if (parse_pack_def(line, old_cnt++))
176 case 'D': /* we used to emit D but that was misguided. */
179 case 'T': /* T ix sha1 type */
180 if (parse_head_def(line))
184 error("unrecognized: %s", line);
195 static int compare_info(const void *a_, const void *b_)
197 struct pack_info * const* a = a_;
198 struct pack_info * const* b = b_;
200 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
201 /* Keep the order in the original */
202 return (*a)->old_num - (*b)->old_num;
203 else if (0 <= (*a)->old_num)
204 /* Only A existed in the original so B is obviously newer */
206 else if (0 <= (*b)->old_num)
207 /* The other way around. */
210 /* then it does not matter but at least keep the comparison stable */
211 return (*a)->p - (*b)->p;
214 static void init_pack_info(const char *infofile, int force)
216 struct packed_git *p;
220 objdir = get_object_directory();
221 objdirlen = strlen(objdir);
223 prepare_packed_git();
224 for (p = packed_git; p; p = p->next) {
225 /* we ignore things on alternate path since they are
226 * not available to the pullers in general.
228 if (strncmp(p->pack_name, objdir, objdirlen) ||
229 strncmp(p->pack_name + objdirlen, "/pack/", 6))
234 info = xcalloc(num_pack, sizeof(struct pack_info *));
235 for (i = 0, p = packed_git; p; p = p->next) {
236 if (strncmp(p->pack_name, objdir, objdirlen) ||
237 p->pack_name[objdirlen] != '/')
239 info[i] = xcalloc(1, sizeof(struct pack_info));
241 info[i]->old_num = -1;
245 if (infofile && !force)
246 stale = read_pack_info_file(infofile);
250 for (i = 0; i < num_pack; i++) {
252 info[i]->old_num = -1;
253 info[i]->nr_heads = 0;
258 qsort(info, num_pack, sizeof(info[0]), compare_info);
259 for (i = 0; i < num_pack; i++)
260 info[i]->new_num = i;
263 static void write_pack_info_file(FILE *fp)
266 for (i = 0; i < num_pack; i++)
267 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
268 for (i = 0; i < num_pack; i++) {
269 struct pack_info *this = info[i];
270 for (j = 0; j < this->nr_heads; j++) {
271 struct object *o = lookup_object(this->head[j]);
272 fprintf(fp, "T %1d %s %s\n",
273 i, sha1_to_hex(this->head[j]), o->type);
279 #define REFERENCED 01
282 static void show(struct object *o, int pack_ix)
285 * We are interested in objects that are not referenced,
287 if (o->flags & EMITTED)
290 if (!(o->flags & REFERENCED))
291 add_head_def(info[pack_ix], o->sha1);
295 static void find_pack_info_one(int pack_ix)
297 unsigned char sha1[20];
300 struct packed_git *p = info[pack_ix]->p;
301 int num = num_packed_objects(p);
303 /* Scan objects, clear flags from all the edge ones and
304 * internal ones, possibly marked in the previous round.
306 for (i = 0; i < num; i++) {
307 if (nth_packed_object_sha1(p, i, sha1))
308 die("corrupt pack file %s?", p->pack_name);
309 if ((o = parse_object_cheap(sha1)) == NULL)
310 die("cannot parse %s", sha1_to_hex(sha1));
312 struct object_refs *refs = o->refs;
314 for (j = 0; j < refs->count; j++)
315 refs->ref[j]->flags = 0;
320 /* Mark all the referenced ones */
321 for (i = 0; i < num; i++) {
322 if (nth_packed_object_sha1(p, i, sha1))
323 die("corrupt pack file %s?", p->pack_name);
324 if ((o = lookup_object(sha1)) == NULL)
325 die("cannot find %s", sha1_to_hex(sha1));
327 struct object_refs *refs = o->refs;
329 for (j = 0; j < refs->count; j++)
330 refs->ref[j]->flags |= REFERENCED;
334 for (i = 0; i < num; i++) {
335 if (nth_packed_object_sha1(p, i, sha1))
336 die("corrupt pack file %s?", p->pack_name);
337 if ((o = lookup_object(sha1)) == NULL)
338 die("cannot find %s", sha1_to_hex(sha1));
344 static void find_pack_info(void)
347 for (i = 0; i < num_pack; i++) {
348 /* The packed objects are cast in stone, and a head
349 * in a pack will stay as head, so is the set of missing
350 * objects. If the repo has been reorganized and we
351 * are missing some packs available back then, we have
352 * already discarded the info read from the file, so
353 * we will find (old_num < 0) in that case.
355 if (0 <= info[i]->old_num)
357 find_pack_info_one(i);
361 static int update_info_packs(int force)
363 char infofile[PATH_MAX];
368 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
369 strcpy(name, infofile);
370 strcpy(name + namelen, "+");
372 init_pack_info(infofile, force);
375 safe_create_leading_directories(name);
376 fp = fopen(name, "w");
378 return error("cannot open %s", name);
379 write_pack_info_file(fp);
381 rename(name, infofile);
386 int update_server_info(int force)
388 /* We would add more dumb-server support files later,
389 * including index of available pack files and their
390 * intended audiences.
394 errs = errs | update_info_refs(force);
395 errs = errs | update_info_packs(force);
397 /* remove leftover rev-cache file if there is any */
398 unlink(git_path("info/rev-cache"));