517
*/
518
static int alt_odb_usable(struct raw_object_store *o,
519
struct strbuf *path,
520
- const char *normalized_objdir)
520
+ const char *normalized_objdir, khiter_t *pos)
521
{
522
- struct object_directory *odb;
522
+ int r;
523
524
/* Detect cases where alternate disappeared */
525
if (!is_directory(path->buf)) {
533
* Prevent the common mistake of listing the same
534
* thing twice, or object directory itself.
535
*/
536
- for (odb = o->odb; odb; odb = odb->next) {
537
- if (!fspathcmp(path->buf, odb->path))
538
- return 0;
536
+ if (!o->odb_by_path) {
537
+ khiter_t p;
538
+
539
+ o->odb_by_path = kh_init_odb_path_map();
540
+ assert(!o->odb->next);
541
+ p = kh_put_odb_path_map(o->odb_by_path, o->odb->path, &r);
542
+ assert(r == 1); /* never used */
543
+ kh_value(o->odb_by_path, p) = o->odb;
544
}
540
- if (!fspathcmp(path->buf, normalized_objdir))
545
+ if (fspatheq(path->buf, normalized_objdir))
546
return 0;
542
-
543
- return 1;
547
+ *pos = kh_put_odb_path_map(o->odb_by_path, path->buf, &r);
548
+ /* r: 0 = exists, 1 = never used, 2 = deleted */
549
+ return r == 0 ? 0 : 1;
550
}
551
552
/*
572
{
573
struct object_directory *ent;
574
struct strbuf pathbuf = STRBUF_INIT;
575
+ khiter_t pos;
576
577
if (!is_absolute_path(entry) && relative_base) {
578
strbuf_realpath(&pathbuf, relative_base, 1);
594
while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
595
strbuf_setlen(&pathbuf, pathbuf.len - 1);
596
590
- if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir)) {
597
+ if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir, &pos)) {
598
strbuf_release(&pathbuf);
599
return -1;
600
}
601
602
CALLOC_ARRAY(ent, 1);
596
- ent->path = xstrdup(pathbuf.buf);
603
+ /* pathbuf.buf is already in r->objects->odb_by_path */
604
+ ent->path = strbuf_detach(&pathbuf, NULL);
605
606
/* add the alternate entry */
607
*r->objects->odb_tail = ent;
608
r->objects->odb_tail = &(ent->next);
609
ent->next = NULL;
610
+ assert(r->objects->odb_by_path);
611
+ kh_value(r->objects->odb_by_path, pos) = ent;
612
613
/* recursively add alternates */
604
- read_info_alternates(r, pathbuf.buf, depth + 1);
614
+ read_info_alternates(r, ent->path, depth + 1);
615
606
- strbuf_release(&pathbuf);
616
return 0;
617
}
618