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
);
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);
23 strcpy(path1
+ len
, "+");
25 safe_create_leading_directories(path0
);
26 info_ref_fp
= fopen(path1
, "w");
28 return error("unable to update %s", path0
);
29 for_each_ref(add_info_ref
);
38 static struct pack_info
{
45 unsigned char (*head
)[20];
46 char dep
[0]; /* more */
49 static const char *objdir
;
52 static struct object
*parse_object_cheap(const unsigned char *sha1
)
56 if ((o
= parse_object(sha1
)) == NULL
)
58 if (o
->type
== commit_type
) {
59 struct commit
*commit
= (struct commit
*)o
;
61 commit
->buffer
= NULL
;
66 static struct pack_info
*find_pack_by_name(const char *name
)
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
))
78 static struct pack_info
*find_pack_by_old_num(int old_num
)
81 for (i
= 0; i
< num_pack
; i
++)
82 if (info
[i
]->old_num
== old_num
)
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);
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);
104 i
->old_num
= old_cnt
;
108 /* The file describes a pack that is no longer here;
109 * dependencies between packs needs to be recalculated.
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
)
122 struct pack_info
*this, *that
;
125 num
= strtoul(cp
, &ep
, 10);
127 return error("invalid input %s", line
);
128 this = find_pack_by_old_num(num
);
131 while (ep
&& *(cp
= ep
)) {
132 num
= strtoul(cp
, &ep
, 10);
135 that
= find_pack_by_old_num(num
);
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.
144 this->dep
[that
->new_num
] = 1;
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];
157 struct pack_info
*this;
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
);
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
)
183 fp
= fopen(infofile
, "r");
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')
193 case 'P': /* P name */
194 if (parse_pack_def(line
, old_cnt
++))
197 case 'D': /* D ix dep-ix1 dep-ix2... */
198 if (parse_depend_def(line
))
201 case 'T': /* T ix sha1 type */
202 if (parse_head_def(line
))
206 error("unrecognized: %s", line
);
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];
225 int num
= num_packed_objects(p
);
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
;
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 */
254 else if (0 <= (*b
)->old_num
)
255 /* The other way around. */
258 if ((*a
)->latest
< (*b
)->latest
)
260 else if ((*a
)->latest
== (*b
)->latest
)
266 static void init_pack_info(const char *infofile
, int force
)
268 struct packed_git
*p
;
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))
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
] != '/')
292 info
[i
] = xcalloc(1, sizeof(struct pack_info
) + num_pack
);
294 info
[i
]->old_num
= -1;
298 if (infofile
&& !force
)
299 stale
= read_pack_info_file(infofile
);
303 for (i
= 0; i
< num_pack
; i
++) {
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
321 for (i
= 0; i
< num_pack
; i
++) {
324 if (info
[i
]->old_num
< 0)
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
])
333 base
= find_pack_by_old_num(old
);
335 die("internal error renumbering");
336 dep_temp
[base
->new_num
] = 1;
338 memcpy(info
[i
]->dep
, dep_temp
, num_pack
);
343 static void write_pack_info_file(FILE *fp
)
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
]))
354 fprintf(fp
, " %1d", j
);
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
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
)
383 if (!(o
->flags
& REFERENCED
))
384 add_head_def(info
[pack_ix
], o
->sha1
);
385 else if ((o
->flags
& REFERENCED
) && !(o
->flags
& INTERNAL
)) {
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;
405 static void find_pack_info_one(int pack_ix
)
407 unsigned char sha1
[20];
409 struct object_list
*ref
;
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;
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
));
445 for (ref
= o
->refs
; ref
; ref
= ref
->next
)
446 show(ref
->item
, pack_ix
);
451 static void find_pack_info(void)
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
)
464 find_pack_info_one(i
);
468 static int update_info_packs(int force
)
470 char infofile
[PATH_MAX
];
475 namelen
= sprintf(infofile
, "%s/info/packs", get_object_directory());
476 strcpy(name
, infofile
);
477 strcpy(name
+ namelen
, "+");
479 init_pack_info(infofile
, force
);
482 safe_create_leading_directories(name
);
483 fp
= fopen(name
, "w");
485 return error("cannot open %s", name
);
486 write_pack_info_file(fp
);
488 rename(name
, infofile
);
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.
501 errs
= errs
| update_info_refs(force
);
502 errs
= errs
| update_info_packs(force
);