8 static FILE *info_ref_fp
;
9 static unsigned long info_ref_time
;
10 static int info_ref_is_stale
= 0;
12 static int stat_ref(const char *path
, const unsigned char *sha1
)
15 if (!stat(path
, &st
) && info_ref_time
< st
.st_mtime
)
16 info_ref_is_stale
= 1;
20 static int add_info_ref(const char *path
, const unsigned char *sha1
)
22 fprintf(info_ref_fp
, "%s %s\n", sha1_to_hex(sha1
), path
);
26 static int update_info_refs(int force
)
29 char *path0
= strdup(git_path("info/refs"));
30 int len
= strlen(path0
);
31 char *path1
= xmalloc(len
+ 2);
34 strcpy(path1
+ len
, "+");
37 if (stat(path0
, &st
)) {
39 info_ref_is_stale
= 1;
41 return error("cannot stat %s", path0
);
44 info_ref_time
= st
.st_mtime
;
45 for_each_ref(stat_ref
);
47 if (!info_ref_is_stale
)
51 safe_create_leading_directories(path0
);
52 info_ref_fp
= fopen(path1
, "w");
54 return error("unable to update %s", path0
);
55 for_each_ref(add_info_ref
);
71 unsigned char (*head
)[20];
72 char dep
[0]; /* more */
75 static const char *objdir
;
78 static struct object
*parse_object_cheap(const unsigned char *sha1
)
82 if ((o
= parse_object(sha1
)) == NULL
)
84 if (o
->type
== commit_type
) {
85 struct commit
*commit
= (struct commit
*)o
;
87 commit
->buffer
= NULL
;
92 static struct pack_info
*find_pack_by_name(const char *name
)
95 for (i
= 0; i
< num_pack
; i
++) {
96 struct packed_git
*p
= info
[i
]->p
;
97 /* skip "/pack/" after ".git/objects" */
98 if (!strcmp(p
->pack_name
+ objdirlen
+ 6, name
))
104 static struct pack_info
*find_pack_by_old_num(int old_num
)
107 for (i
= 0; i
< num_pack
; i
++)
108 if (info
[i
]->old_num
== old_num
)
113 static int add_head_def(struct pack_info
*this, unsigned char *sha1
)
115 if (this->nr_alloc
<= this->nr_heads
) {
116 this->nr_alloc
= alloc_nr(this->nr_alloc
);
117 this->head
= xrealloc(this->head
, this->nr_alloc
* 20);
119 memcpy(this->head
[this->nr_heads
++], sha1
, 20);
123 /* Returns non-zero when we detect that the info in the
124 * old file is useless.
126 static int parse_pack_def(const char *line
, int old_cnt
)
128 struct pack_info
*i
= find_pack_by_name(line
+ 2);
130 i
->old_num
= old_cnt
;
134 /* The file describes a pack that is no longer here;
135 * dependencies between packs needs to be recalculated.
141 /* Returns non-zero when we detect that the info in the
142 * old file is useless.
144 static int parse_depend_def(char *line
)
148 struct pack_info
*this, *that
;
151 num
= strtoul(cp
, &ep
, 10);
153 return error("invalid input %s", line
);
154 this = find_pack_by_old_num(num
);
157 while (ep
&& *(cp
= ep
)) {
158 num
= strtoul(cp
, &ep
, 10);
161 that
= find_pack_by_old_num(num
);
163 /* The pack this one depends on does not
164 * exist; this should not happen because
165 * we write out the list of packs first and
166 * then dependency information, but it means
167 * the file is useless anyway.
170 this->dep
[that
->new_num
] = 1;
175 /* Returns non-zero when we detect that the info in the
176 * old file is useless.
178 static int parse_head_def(char *line
)
180 unsigned char sha1
[20];
183 struct pack_info
*this;
187 num
= strtoul(cp
, &ep
, 10);
188 if (ep
== cp
|| *ep
++ != ' ')
189 return error("invalid input ix %s", line
);
190 this = find_pack_by_old_num(num
);
192 return 1; /* You know the drill. */
193 if (get_sha1_hex(ep
, sha1
) || ep
[40] != ' ')
194 return error("invalid input sha1 %s (%s)", line
, ep
);
195 if ((o
= parse_object_cheap(sha1
)) == NULL
)
196 return error("no such object: %s", line
);
197 return add_head_def(this, sha1
);
200 /* Returns non-zero when we detect that the info in the
201 * old file is useless.
203 static int read_pack_info_file(const char *infofile
)
209 fp
= fopen(infofile
, "r");
211 return 1; /* nonexisting is not an error. */
213 while (fgets(line
, sizeof(line
), fp
)) {
214 int len
= strlen(line
);
215 if (line
[len
-1] == '\n')
219 case 'P': /* P name */
220 if (parse_pack_def(line
, old_cnt
++))
223 case 'D': /* D ix dep-ix1 dep-ix2... */
224 if (parse_depend_def(line
))
227 case 'T': /* T ix sha1 type */
228 if (parse_head_def(line
))
232 error("unrecognized: %s", line
);
243 /* We sort the packs according to the date of the latest commit. That
244 * in turn indicates how young the pack is, and in general we would
245 * want to depend on younger packs.
247 static unsigned long get_latest_commit_date(struct packed_git
*p
)
249 unsigned char sha1
[20];
251 int num
= num_packed_objects(p
);
253 unsigned long latest
= 0;
255 for (i
= 0; i
< num
; i
++) {
256 if (nth_packed_object_sha1(p
, i
, sha1
))
257 die("corrupt pack file %s?", p
->pack_name
);
258 if ((o
= parse_object_cheap(sha1
)) == NULL
)
259 die("cannot parse %s", sha1_to_hex(sha1
));
260 if (o
->type
== commit_type
) {
261 struct commit
*commit
= (struct commit
*)o
;
262 if (latest
< commit
->date
)
263 latest
= commit
->date
;
269 static int compare_info(const void *a_
, const void *b_
)
271 struct pack_info
* const* a
= a_
;
272 struct pack_info
* const* b
= b_
;
274 if (0 <= (*a
)->old_num
&& 0 <= (*b
)->old_num
)
275 /* Keep the order in the original */
276 return (*a
)->old_num
- (*b
)->old_num
;
277 else if (0 <= (*a
)->old_num
)
278 /* Only A existed in the original so B is obviously newer */
280 else if (0 <= (*b
)->old_num
)
281 /* The other way around. */
284 if ((*a
)->latest
< (*b
)->latest
)
286 else if ((*a
)->latest
== (*b
)->latest
)
292 static void init_pack_info(const char *infofile
, int force
)
294 struct packed_git
*p
;
299 objdir
= get_object_directory();
300 objdirlen
= strlen(objdir
);
302 prepare_packed_git();
303 for (p
= packed_git
; p
; p
= p
->next
) {
304 /* we ignore things on alternate path since they are
305 * not available to the pullers in general.
307 if (strncmp(p
->pack_name
, objdir
, objdirlen
) ||
308 strncmp(p
->pack_name
+ objdirlen
, "/pack/", 6))
313 info
= xcalloc(num_pack
, sizeof(struct pack_info
*));
314 for (i
= 0, p
= packed_git
; p
; p
= p
->next
) {
315 if (strncmp(p
->pack_name
, objdir
, objdirlen
) ||
316 p
->pack_name
[objdirlen
] != '/')
318 info
[i
] = xcalloc(1, sizeof(struct pack_info
) + num_pack
);
320 info
[i
]->old_num
= -1;
324 if (infofile
&& !force
)
325 stale
= read_pack_info_file(infofile
);
329 for (i
= 0; i
< num_pack
; i
++) {
331 info
[i
]->old_num
= -1;
332 memset(info
[i
]->dep
, 0, num_pack
);
333 info
[i
]->nr_heads
= 0;
335 if (info
[i
]->old_num
< 0)
336 info
[i
]->latest
= get_latest_commit_date(info
[i
]->p
);
339 qsort(info
, num_pack
, sizeof(info
[0]), compare_info
);
340 for (i
= 0; i
< num_pack
; i
++)
341 info
[i
]->new_num
= i
;
343 /* we need to fix up the dependency information
347 for (i
= 0; i
< num_pack
; i
++) {
350 if (info
[i
]->old_num
< 0)
353 dep_temp
= xmalloc(num_pack
);
354 memset(dep_temp
, 0, num_pack
);
355 for (old
= 0; old
< num_pack
; old
++) {
356 struct pack_info
*base
;
357 if (!info
[i
]->dep
[old
])
359 base
= find_pack_by_old_num(old
);
361 die("internal error renumbering");
362 dep_temp
[base
->new_num
] = 1;
364 memcpy(info
[i
]->dep
, dep_temp
, num_pack
);
369 static void write_pack_info_file(FILE *fp
)
372 for (i
= 0; i
< num_pack
; i
++)
373 fprintf(fp
, "P %s\n", info
[i
]->p
->pack_name
+ objdirlen
+ 6);
375 for (i
= 0; i
< num_pack
; i
++) {
376 fprintf(fp
, "D %1d", i
);
377 for (j
= 0; j
< num_pack
; j
++) {
378 if ((i
== j
) || !(info
[i
]->dep
[j
]))
380 fprintf(fp
, " %1d", j
);
385 for (i
= 0; i
< num_pack
; i
++) {
386 struct pack_info
*this = info
[i
];
387 for (j
= 0; j
< this->nr_heads
; j
++) {
388 struct object
*o
= lookup_object(this->head
[j
]);
389 fprintf(fp
, "T %1d %s %s\n",
390 i
, sha1_to_hex(this->head
[j
]), o
->type
);
396 #define REFERENCED 01
400 static void show(struct object
*o
, int pack_ix
)
403 * We are interested in objects that are not referenced,
404 * and objects that are referenced but not internal.
406 if (o
->flags
& EMITTED
)
409 if (!(o
->flags
& REFERENCED
))
410 add_head_def(info
[pack_ix
], o
->sha1
);
411 else if ((o
->flags
& REFERENCED
) && !(o
->flags
& INTERNAL
)) {
414 /* Which pack contains this object? That is what
415 * pack_ix can depend on. We earlier sorted info
416 * array from youngest to oldest, so try newer packs
417 * first to favor them here.
419 for (i
= num_pack
- 1; 0 <= i
; i
--) {
420 struct packed_git
*p
= info
[i
]->p
;
421 struct pack_entry ent
;
422 if (find_pack_entry_one(o
->sha1
, &ent
, p
)) {
423 info
[pack_ix
]->dep
[i
] = 1;
431 static void find_pack_info_one(int pack_ix
)
433 unsigned char sha1
[20];
435 struct object_list
*ref
;
437 struct packed_git
*p
= info
[pack_ix
]->p
;
438 int num
= num_packed_objects(p
);
440 /* Scan objects, clear flags from all the edge ones and
441 * internal ones, possibly marked in the previous round.
443 for (i
= 0; i
< num
; i
++) {
444 if (nth_packed_object_sha1(p
, i
, sha1
))
445 die("corrupt pack file %s?", p
->pack_name
);
446 if ((o
= lookup_object(sha1
)) == NULL
)
447 die("cannot parse %s", sha1_to_hex(sha1
));
448 for (ref
= o
->refs
; ref
; ref
= ref
->next
)
449 ref
->item
->flags
= 0;
453 /* Mark all the internal ones */
454 for (i
= 0; i
< num
; i
++) {
455 if (nth_packed_object_sha1(p
, i
, sha1
))
456 die("corrupt pack file %s?", p
->pack_name
);
457 if ((o
= lookup_object(sha1
)) == NULL
)
458 die("cannot find %s", sha1_to_hex(sha1
));
459 for (ref
= o
->refs
; ref
; ref
= ref
->next
)
460 ref
->item
->flags
|= REFERENCED
;
461 o
->flags
|= INTERNAL
;
464 for (i
= 0; i
< num
; i
++) {
465 if (nth_packed_object_sha1(p
, i
, sha1
))
466 die("corrupt pack file %s?", p
->pack_name
);
467 if ((o
= lookup_object(sha1
)) == NULL
)
468 die("cannot find %s", sha1_to_hex(sha1
));
471 for (ref
= o
->refs
; ref
; ref
= ref
->next
)
472 show(ref
->item
, pack_ix
);
477 static void find_pack_info(void)
480 for (i
= 0; i
< num_pack
; i
++) {
481 /* The packed objects are cast in stone, and a head
482 * in a pack will stay as head, so is the set of missing
483 * objects. If the repo has been reorganized and we
484 * are missing some packs available back then, we have
485 * already discarded the info read from the file, so
486 * we will find (old_num < 0) in that case.
488 if (0 <= info
[i
]->old_num
)
490 find_pack_info_one(i
);
494 static int update_info_packs(int force
)
496 char infofile
[PATH_MAX
];
501 namelen
= sprintf(infofile
, "%s/info/packs", get_object_directory());
502 strcpy(name
, infofile
);
503 strcpy(name
+ namelen
, "+");
505 init_pack_info(infofile
, force
);
508 safe_create_leading_directories(name
);
509 fp
= fopen(name
, "w");
511 return error("cannot open %s", name
);
512 write_pack_info_file(fp
);
514 rename(name
, infofile
);
519 static int record_rev_cache_ref(const char *path
, const unsigned char *sha1
)
521 struct commit
*commit
;
522 if (!(commit
= lookup_commit_reference(sha1
)))
523 return error("not a commit: %s", sha1_to_hex(sha1
));
524 return record_rev_cache(commit
->object
.sha1
, NULL
);
527 static int update_info_revs(int force
)
529 char *path0
= strdup(git_path("info/rev-cache"));
530 int len
= strlen(path0
);
531 char *path1
= xmalloc(len
+ 2);
533 strcpy(path1
, path0
);
534 strcpy(path1
+ len
, "+");
536 /* read existing rev-cache */
538 read_rev_cache(path0
, NULL
, 0);
539 safe_create_leading_directories(path0
);
541 for_each_ref(record_rev_cache_ref
);
543 /* update the rev-cache database */
544 write_rev_cache(path1
, force
? "/dev/null" : path0
);
545 rename(path1
, path0
);
552 int update_server_info(int force
)
554 /* We would add more dumb-server support files later,
555 * including index of available pack files and their
556 * intended audiences.
560 errs
= errs
| update_info_refs(force
);
561 errs
= errs
| update_info_packs(force
);
562 errs
= errs
| update_info_revs(force
);