Do not quote SP.
[git.git] / server-info.c
bloba9e5607f2f5668411e5742243372bf570c2bda72
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 fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path);
13 return 0;
16 static int update_info_refs(int force)
18 char *path0 = strdup(git_path("info/refs"));
19 int len = strlen(path0);
20 char *path1 = xmalloc(len + 2);
22 strcpy(path1, path0);
23 strcpy(path1 + len, "+");
25 safe_create_leading_directories(path0);
26 info_ref_fp = fopen(path1, "w");
27 if (!info_ref_fp)
28 return error("unable to update %s", path0);
29 for_each_ref(add_info_ref);
30 fclose(info_ref_fp);
31 rename(path1, path0);
32 free(path0);
33 free(path1);
34 return 0;
37 /* packs */
38 static struct pack_info {
39 unsigned long latest;
40 struct packed_git *p;
41 int old_num;
42 int new_num;
43 int nr_alloc;
44 int nr_heads;
45 unsigned char (*head)[20];
46 char dep[0]; /* more */
47 } **info;
48 static int num_pack;
49 static const char *objdir;
50 static int objdirlen;
52 static struct object *parse_object_cheap(const unsigned char *sha1)
54 struct object *o;
56 if ((o = parse_object(sha1)) == NULL)
57 return NULL;
58 if (o->type == commit_type) {
59 struct commit *commit = (struct commit *)o;
60 free(commit->buffer);
61 commit->buffer = NULL;
63 return o;
66 static struct pack_info *find_pack_by_name(const char *name)
68 int i;
69 for (i = 0; i < num_pack; i++) {
70 struct packed_git *p = info[i]->p;
71 /* skip "/pack/" after ".git/objects" */
72 if (!strcmp(p->pack_name + objdirlen + 6, name))
73 return info[i];
75 return NULL;
78 static struct pack_info *find_pack_by_old_num(int old_num)
80 int i;
81 for (i = 0; i < num_pack; i++)
82 if (info[i]->old_num == old_num)
83 return info[i];
84 return NULL;
87 static int add_head_def(struct pack_info *this, unsigned char *sha1)
89 if (this->nr_alloc <= this->nr_heads) {
90 this->nr_alloc = alloc_nr(this->nr_alloc);
91 this->head = xrealloc(this->head, this->nr_alloc * 20);
93 memcpy(this->head[this->nr_heads++], sha1, 20);
94 return 0;
97 /* Returns non-zero when we detect that the info in the
98 * old file is useless.
100 static int parse_pack_def(const char *line, int old_cnt)
102 struct pack_info *i = find_pack_by_name(line + 2);
103 if (i) {
104 i->old_num = old_cnt;
105 return 0;
107 else {
108 /* The file describes a pack that is no longer here;
109 * dependencies between packs needs to be recalculated.
111 return 1;
115 /* Returns non-zero when we detect that the info in the
116 * old file is useless.
118 static int parse_depend_def(char *line)
120 unsigned long num;
121 char *cp, *ep;
122 struct pack_info *this, *that;
124 cp = line + 2;
125 num = strtoul(cp, &ep, 10);
126 if (ep == cp)
127 return error("invalid input %s", line);
128 this = find_pack_by_old_num(num);
129 if (!this)
130 return 0;
131 while (ep && *(cp = ep)) {
132 num = strtoul(cp, &ep, 10);
133 if (ep == cp)
134 break;
135 that = find_pack_by_old_num(num);
136 if (!that)
137 /* The pack this one depends on does not
138 * exist; this should not happen because
139 * we write out the list of packs first and
140 * then dependency information, but it means
141 * the file is useless anyway.
143 return 1;
144 this->dep[that->new_num] = 1;
146 return 0;
149 /* Returns non-zero when we detect that the info in the
150 * old file is useless.
152 static int parse_head_def(char *line)
154 unsigned char sha1[20];
155 unsigned long num;
156 char *cp, *ep;
157 struct pack_info *this;
158 struct object *o;
160 cp = line + 2;
161 num = strtoul(cp, &ep, 10);
162 if (ep == cp || *ep++ != ' ')
163 return error("invalid input ix %s", line);
164 this = find_pack_by_old_num(num);
165 if (!this)
166 return 1; /* You know the drill. */
167 if (get_sha1_hex(ep, sha1) || ep[40] != ' ')
168 return error("invalid input sha1 %s (%s)", line, ep);
169 if ((o = parse_object_cheap(sha1)) == NULL)
170 return error("no such object: %s", line);
171 return add_head_def(this, sha1);
174 /* Returns non-zero when we detect that the info in the
175 * old file is useless.
177 static int read_pack_info_file(const char *infofile)
179 FILE *fp;
180 char line[1000];
181 int old_cnt = 0;
183 fp = fopen(infofile, "r");
184 if (!fp)
185 return 1; /* nonexisting is not an error. */
187 while (fgets(line, sizeof(line), fp)) {
188 int len = strlen(line);
189 if (line[len-1] == '\n')
190 line[len-1] = 0;
192 switch (line[0]) {
193 case 'P': /* P name */
194 if (parse_pack_def(line, old_cnt++))
195 goto out_stale;
196 break;
197 case 'D': /* D ix dep-ix1 dep-ix2... */
198 if (parse_depend_def(line))
199 goto out_stale;
200 break;
201 case 'T': /* T ix sha1 type */
202 if (parse_head_def(line))
203 goto out_stale;
204 break;
205 default:
206 error("unrecognized: %s", line);
207 break;
210 fclose(fp);
211 return 0;
212 out_stale:
213 fclose(fp);
214 return 1;
217 /* We sort the packs according to the date of the latest commit. That
218 * in turn indicates how young the pack is, and in general we would
219 * want to depend on younger packs.
221 static unsigned long get_latest_commit_date(struct packed_git *p)
223 unsigned char sha1[20];
224 struct object *o;
225 int num = num_packed_objects(p);
226 int i;
227 unsigned long latest = 0;
229 for (i = 0; i < num; i++) {
230 if (nth_packed_object_sha1(p, i, sha1))
231 die("corrupt pack file %s?", p->pack_name);
232 if ((o = parse_object_cheap(sha1)) == NULL)
233 die("cannot parse %s", sha1_to_hex(sha1));
234 if (o->type == commit_type) {
235 struct commit *commit = (struct commit *)o;
236 if (latest < commit->date)
237 latest = commit->date;
240 return latest;
243 static int compare_info(const void *a_, const void *b_)
245 struct pack_info * const* a = a_;
246 struct pack_info * const* b = b_;
248 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
249 /* Keep the order in the original */
250 return (*a)->old_num - (*b)->old_num;
251 else if (0 <= (*a)->old_num)
252 /* Only A existed in the original so B is obviously newer */
253 return -1;
254 else if (0 <= (*b)->old_num)
255 /* The other way around. */
256 return 1;
258 if ((*a)->latest < (*b)->latest)
259 return -1;
260 else if ((*a)->latest == (*b)->latest)
261 return 0;
262 else
263 return 1;
266 static void init_pack_info(const char *infofile, int force)
268 struct packed_git *p;
269 int stale;
270 int i = 0;
271 char *dep_temp;
273 objdir = get_object_directory();
274 objdirlen = strlen(objdir);
276 prepare_packed_git();
277 for (p = packed_git; p; p = p->next) {
278 /* we ignore things on alternate path since they are
279 * not available to the pullers in general.
281 if (strncmp(p->pack_name, objdir, objdirlen) ||
282 strncmp(p->pack_name + objdirlen, "/pack/", 6))
283 continue;
284 i++;
286 num_pack = i;
287 info = xcalloc(num_pack, sizeof(struct pack_info *));
288 for (i = 0, p = packed_git; p; p = p->next) {
289 if (strncmp(p->pack_name, objdir, objdirlen) ||
290 p->pack_name[objdirlen] != '/')
291 continue;
292 info[i] = xcalloc(1, sizeof(struct pack_info) + num_pack);
293 info[i]->p = p;
294 info[i]->old_num = -1;
295 i++;
298 if (infofile && !force)
299 stale = read_pack_info_file(infofile);
300 else
301 stale = 1;
303 for (i = 0; i < num_pack; i++) {
304 if (stale) {
305 info[i]->old_num = -1;
306 memset(info[i]->dep, 0, num_pack);
307 info[i]->nr_heads = 0;
309 if (info[i]->old_num < 0)
310 info[i]->latest = get_latest_commit_date(info[i]->p);
313 qsort(info, num_pack, sizeof(info[0]), compare_info);
314 for (i = 0; i < num_pack; i++)
315 info[i]->new_num = i;
317 /* we need to fix up the dependency information
318 * for the old ones.
320 dep_temp = NULL;
321 for (i = 0; i < num_pack; i++) {
322 int old;
324 if (info[i]->old_num < 0)
325 continue;
326 if (! dep_temp)
327 dep_temp = xmalloc(num_pack);
328 memset(dep_temp, 0, num_pack);
329 for (old = 0; old < num_pack; old++) {
330 struct pack_info *base;
331 if (!info[i]->dep[old])
332 continue;
333 base = find_pack_by_old_num(old);
334 if (!base)
335 die("internal error renumbering");
336 dep_temp[base->new_num] = 1;
338 memcpy(info[i]->dep, dep_temp, num_pack);
340 free(dep_temp);
343 static void write_pack_info_file(FILE *fp)
345 int i, j;
346 for (i = 0; i < num_pack; i++)
347 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
349 for (i = 0; i < num_pack; i++) {
350 fprintf(fp, "D %1d", i);
351 for (j = 0; j < num_pack; j++) {
352 if ((i == j) || !(info[i]->dep[j]))
353 continue;
354 fprintf(fp, " %1d", j);
356 fputc('\n', fp);
359 for (i = 0; i < num_pack; i++) {
360 struct pack_info *this = info[i];
361 for (j = 0; j < this->nr_heads; j++) {
362 struct object *o = lookup_object(this->head[j]);
363 fprintf(fp, "T %1d %s %s\n",
364 i, sha1_to_hex(this->head[j]), o->type);
370 #define REFERENCED 01
371 #define INTERNAL 02
372 #define EMITTED 04
374 static void show(struct object *o, int pack_ix)
377 * We are interested in objects that are not referenced,
378 * and objects that are referenced but not internal.
380 if (o->flags & EMITTED)
381 return;
383 if (!(o->flags & REFERENCED))
384 add_head_def(info[pack_ix], o->sha1);
385 else if ((o->flags & REFERENCED) && !(o->flags & INTERNAL)) {
386 int i;
388 /* Which pack contains this object? That is what
389 * pack_ix can depend on. We earlier sorted info
390 * array from youngest to oldest, so try newer packs
391 * first to favor them here.
393 for (i = num_pack - 1; 0 <= i; i--) {
394 struct packed_git *p = info[i]->p;
395 struct pack_entry ent;
396 if (find_pack_entry_one(o->sha1, &ent, p)) {
397 info[pack_ix]->dep[i] = 1;
398 break;
402 o->flags |= EMITTED;
405 static void find_pack_info_one(int pack_ix)
407 unsigned char sha1[20];
408 struct object *o;
409 struct object_list *ref;
410 int i;
411 struct packed_git *p = info[pack_ix]->p;
412 int num = num_packed_objects(p);
414 /* Scan objects, clear flags from all the edge ones and
415 * internal ones, possibly marked in the previous round.
417 for (i = 0; i < num; i++) {
418 if (nth_packed_object_sha1(p, i, sha1))
419 die("corrupt pack file %s?", p->pack_name);
420 if ((o = lookup_object(sha1)) == NULL)
421 die("cannot parse %s", sha1_to_hex(sha1));
422 for (ref = o->refs; ref; ref = ref->next)
423 ref->item->flags = 0;
424 o->flags = 0;
427 /* Mark all the internal ones */
428 for (i = 0; i < num; i++) {
429 if (nth_packed_object_sha1(p, i, sha1))
430 die("corrupt pack file %s?", p->pack_name);
431 if ((o = lookup_object(sha1)) == NULL)
432 die("cannot find %s", sha1_to_hex(sha1));
433 for (ref = o->refs; ref; ref = ref->next)
434 ref->item->flags |= REFERENCED;
435 o->flags |= INTERNAL;
438 for (i = 0; i < num; i++) {
439 if (nth_packed_object_sha1(p, i, sha1))
440 die("corrupt pack file %s?", p->pack_name);
441 if ((o = lookup_object(sha1)) == NULL)
442 die("cannot find %s", sha1_to_hex(sha1));
444 show(o, pack_ix);
445 for (ref = o->refs; ref; ref = ref->next)
446 show(ref->item, pack_ix);
451 static void find_pack_info(void)
453 int i;
454 for (i = 0; i < num_pack; i++) {
455 /* The packed objects are cast in stone, and a head
456 * in a pack will stay as head, so is the set of missing
457 * objects. If the repo has been reorganized and we
458 * are missing some packs available back then, we have
459 * already discarded the info read from the file, so
460 * we will find (old_num < 0) in that case.
462 if (0 <= info[i]->old_num)
463 continue;
464 find_pack_info_one(i);
468 static int update_info_packs(int force)
470 char infofile[PATH_MAX];
471 char name[PATH_MAX];
472 int namelen;
473 FILE *fp;
475 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
476 strcpy(name, infofile);
477 strcpy(name + namelen, "+");
479 init_pack_info(infofile, force);
480 find_pack_info();
482 safe_create_leading_directories(name);
483 fp = fopen(name, "w");
484 if (!fp)
485 return error("cannot open %s", name);
486 write_pack_info_file(fp);
487 fclose(fp);
488 rename(name, infofile);
489 return 0;
492 /* public */
493 int update_server_info(int force)
495 /* We would add more dumb-server support files later,
496 * including index of available pack files and their
497 * intended audiences.
499 int errs = 0;
501 errs = errs | update_info_refs(force);
502 errs = errs | update_info_packs(force);
504 return errs;