Add --tags documentation, scraped from JC mail.
[git/debian.git] / server-info.c
blob0cba8e19f7582fa5771ed6ab94a76e1d87efc53a
1 #include "cache.h"
2 #include "refs.h"
3 #include "object.h"
4 #include "commit.h"
5 #include "tag.h"
7 /* refs */
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);
17 if (o)
18 fprintf(info_ref_fp, "%s %s^{}\n",
19 sha1_to_hex(o->sha1), path);
21 return 0;
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);
30 strcpy(path1, path0);
31 strcpy(path1 + len, "+");
33 safe_create_leading_directories(path0);
34 info_ref_fp = fopen(path1, "w");
35 if (!info_ref_fp)
36 return error("unable to update %s", path0);
37 for_each_ref(add_info_ref);
38 fclose(info_ref_fp);
39 rename(path1, path0);
40 free(path0);
41 free(path1);
42 return 0;
45 /* packs */
46 static struct pack_info {
47 unsigned long latest;
48 struct packed_git *p;
49 int old_num;
50 int new_num;
51 int nr_alloc;
52 int nr_heads;
53 unsigned char (*head)[20];
54 char dep[0]; /* more */
55 } **info;
56 static int num_pack;
57 static const char *objdir;
58 static int objdirlen;
60 static struct object *parse_object_cheap(const unsigned char *sha1)
62 struct object *o;
64 if ((o = parse_object(sha1)) == NULL)
65 return NULL;
66 if (o->type == commit_type) {
67 struct commit *commit = (struct commit *)o;
68 free(commit->buffer);
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) {
74 free(e->name);
75 e->name = NULL;
76 n = e->next;
77 free(e);
79 tree->entries = NULL;
81 return o;
84 static struct pack_info *find_pack_by_name(const char *name)
86 int i;
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))
91 return info[i];
93 return NULL;
96 static struct pack_info *find_pack_by_old_num(int old_num)
98 int i;
99 for (i = 0; i < num_pack; i++)
100 if (info[i]->old_num == old_num)
101 return info[i];
102 return NULL;
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);
112 return 0;
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);
121 if (i) {
122 i->old_num = old_cnt;
123 return 0;
125 else {
126 /* The file describes a pack that is no longer here;
127 * dependencies between packs needs to be recalculated.
129 return 1;
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)
138 unsigned long num;
139 char *cp, *ep;
140 struct pack_info *this, *that;
142 cp = line + 2;
143 num = strtoul(cp, &ep, 10);
144 if (ep == cp)
145 return error("invalid input %s", line);
146 this = find_pack_by_old_num(num);
147 if (!this)
148 return 0;
149 while (ep && *(cp = ep)) {
150 num = strtoul(cp, &ep, 10);
151 if (ep == cp)
152 break;
153 that = find_pack_by_old_num(num);
154 if (!that)
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.
161 return 1;
162 this->dep[that->new_num] = 1;
164 return 0;
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];
173 unsigned long num;
174 char *cp, *ep;
175 struct pack_info *this;
176 struct object *o;
178 cp = line + 2;
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);
183 if (!this)
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)
197 FILE *fp;
198 char line[1000];
199 int old_cnt = 0;
201 fp = fopen(infofile, "r");
202 if (!fp)
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')
208 line[len-1] = 0;
210 switch (line[0]) {
211 case 'P': /* P name */
212 if (parse_pack_def(line, old_cnt++))
213 goto out_stale;
214 break;
215 case 'D': /* D ix dep-ix1 dep-ix2... */
216 if (parse_depend_def(line))
217 goto out_stale;
218 break;
219 case 'T': /* T ix sha1 type */
220 if (parse_head_def(line))
221 goto out_stale;
222 break;
223 default:
224 error("unrecognized: %s", line);
225 break;
228 fclose(fp);
229 return 0;
230 out_stale:
231 fclose(fp);
232 return 1;
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];
242 struct object *o;
243 int num = num_packed_objects(p);
244 int i;
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;
258 return latest;
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 */
271 return -1;
272 else if (0 <= (*b)->old_num)
273 /* The other way around. */
274 return 1;
276 if ((*a)->latest < (*b)->latest)
277 return -1;
278 else if ((*a)->latest == (*b)->latest)
279 return 0;
280 else
281 return 1;
284 static void init_pack_info(const char *infofile, int force)
286 struct packed_git *p;
287 int stale;
288 int i = 0;
289 char *dep_temp;
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))
301 continue;
302 i++;
304 num_pack = i;
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] != '/')
309 continue;
310 info[i] = xcalloc(1, sizeof(struct pack_info) + num_pack);
311 info[i]->p = p;
312 info[i]->old_num = -1;
313 i++;
316 if (infofile && !force)
317 stale = read_pack_info_file(infofile);
318 else
319 stale = 1;
321 for (i = 0; i < num_pack; i++) {
322 if (stale) {
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
336 * for the old ones.
338 dep_temp = NULL;
339 for (i = 0; i < num_pack; i++) {
340 int old;
342 if (info[i]->old_num < 0)
343 continue;
344 if (! dep_temp)
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])
350 continue;
351 base = find_pack_by_old_num(old);
352 if (!base)
353 die("internal error renumbering");
354 dep_temp[base->new_num] = 1;
356 memcpy(info[i]->dep, dep_temp, num_pack);
358 free(dep_temp);
361 static void write_pack_info_file(FILE *fp)
363 int i, j;
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]))
371 continue;
372 fprintf(fp, " %1d", j);
374 fputc('\n', fp);
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
389 #define INTERNAL 02
390 #define EMITTED 04
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)
399 return;
401 if (!(o->flags & REFERENCED))
402 add_head_def(info[pack_ix], o->sha1);
403 else if ((o->flags & REFERENCED) && !(o->flags & INTERNAL)) {
404 int i;
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;
416 break;
420 o->flags |= EMITTED;
423 static void find_pack_info_one(int pack_ix)
425 unsigned char sha1[20];
426 struct object *o;
427 struct object_list *ref;
428 int i;
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;
442 o->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));
462 show(o, pack_ix);
463 for (ref = o->refs; ref; ref = ref->next)
464 show(ref->item, pack_ix);
469 static void find_pack_info(void)
471 int i;
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)
481 continue;
482 find_pack_info_one(i);
486 static int update_info_packs(int force)
488 char infofile[PATH_MAX];
489 char name[PATH_MAX];
490 int namelen;
491 FILE *fp;
493 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
494 strcpy(name, infofile);
495 strcpy(name + namelen, "+");
497 init_pack_info(infofile, force);
498 find_pack_info();
500 safe_create_leading_directories(name);
501 fp = fopen(name, "w");
502 if (!fp)
503 return error("cannot open %s", name);
504 write_pack_info_file(fp);
505 fclose(fp);
506 rename(name, infofile);
507 return 0;
510 /* public */
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.
517 int errs = 0;
519 errs = errs | update_info_refs(force);
520 errs = errs | update_info_packs(force);
522 return errs;