sha1_file: allow link_alt_odb_entries to handle arbitrary repositories

Actually this also allows read_info_alternates and link_alt_odb_entry to handle arbitrary repositories, but link_alt_odb_entries is the most interesting function in this set of functions, hence the commit subject. These functions span a strongly connected component in the function graph, i.e. the recursive call chain might look like -> link_alt_odb_entries -> link_alt_odb_entry -> read_info_alternates -> link_alt_odb_entries That is why we need to convert all these functions at the same time. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Mar 23, 2018 at 18:21 UTC 77f012e876d5845856cb939c6435e839d5f9d027
2 files changed +20 -20
object-store.h
+4
@@ -18,6 +18,10 @@ struct alternate_object_database {
18 char loose_objects_subdir_seen[256];
19 struct oid_array loose_objects_cache;
20
21 + /*
22 + * Path to the alternative object store. If this is a relative path,
23 + * it is relative to the current working directory.
24 + */
25 char path[FLEX_ARRAY];
26 };
27 #define prepare_alt_odb(r) prepare_alt_odb_##r()
sha1_file.c
+16 -20
@@ -390,11 +390,10 @@ static int alt_odb_usable(struct raw_object_store *o,
390 * SHA1, an extra slash for the first level indirection, and the
391 * terminating NUL.
392 */
393 -#define read_info_alternates(r, rb, d) read_info_alternates_##r(rb, d)
394 -static void read_info_alternates_the_repository(const char *relative_base,
395 - int depth);
396 -#define link_alt_odb_entry(r, e, rb, d, n) link_alt_odb_entry_##r(e, rb, d, n)
397 -static int link_alt_odb_entry_the_repository(const char *entry,
393 +static void read_info_alternates(struct repository *r,
394 + const char *relative_base,
395 + int depth);
396 +static int link_alt_odb_entry(struct repository *r, const char *entry,
397 const char *relative_base, int depth, const char *normalized_objdir)
398 {
399 struct alternate_object_database *ent;
@@ -420,7 +419,7 @@ static int link_alt_odb_entry_the_repository(const char *entry,
419 while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
420 strbuf_setlen(&pathbuf, pathbuf.len - 1);
421
423 - if (!alt_odb_usable(the_repository->objects, &pathbuf, normalized_objdir)) {
422 + if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir)) {
423 strbuf_release(&pathbuf);
424 return -1;
425 }
@@ -428,12 +427,12 @@ static int link_alt_odb_entry_the_repository(const char *entry,
427 ent = alloc_alt_odb(pathbuf.buf);
428
429 /* add the alternate entry */
431 - *the_repository->objects->alt_odb_tail = ent;
432 - the_repository->objects->alt_odb_tail = &(ent->next);
430 + *r->objects->alt_odb_tail = ent;
431 + r->objects->alt_odb_tail = &(ent->next);
432 ent->next = NULL;
433
434 /* recursively add alternates */
436 - read_info_alternates(the_repository, pathbuf.buf, depth + 1);
435 + read_info_alternates(r, pathbuf.buf, depth + 1);
436
437 strbuf_release(&pathbuf);
438 return 0;
@@ -468,12 +467,8 @@ static const char *parse_alt_odb_entry(const char *string,
467 return end;
468 }
469
471 -#define link_alt_odb_entries(r, a, s, rb, d) \
472 - link_alt_odb_entries_##r(a, s, rb, d)
473 -static void link_alt_odb_entries_the_repository(const char *alt,
474 - int sep,
475 - const char *relative_base,
476 - int depth)
470 +static void link_alt_odb_entries(struct repository *r, const char *alt,
471 + int sep, const char *relative_base, int depth)
472 {
473 struct strbuf objdirbuf = STRBUF_INIT;
474 struct strbuf entry = STRBUF_INIT;
@@ -487,7 +482,7 @@ static void link_alt_odb_entries_the_repository(const char *alt,
482 return;
483 }
484
490 - strbuf_add_absolute_path(&objdirbuf, get_object_directory());
485 + strbuf_add_absolute_path(&objdirbuf, r->objects->objectdir);
486 if (strbuf_normalize_path(&objdirbuf) < 0)
487 die("unable to normalize object directory: %s",
488 objdirbuf.buf);
@@ -496,15 +491,16 @@ static void link_alt_odb_entries_the_repository(const char *alt,
491 alt = parse_alt_odb_entry(alt, sep, &entry);
492 if (!entry.len)
493 continue;
499 - link_alt_odb_entry(the_repository, entry.buf,
494 + link_alt_odb_entry(r, entry.buf,
495 relative_base, depth, objdirbuf.buf);
496 }
497 strbuf_release(&entry);
498 strbuf_release(&objdirbuf);
499 }
500
506 -static void read_info_alternates_the_repository(const char *relative_base,
507 - int depth)
501 +static void read_info_alternates(struct repository *r,
502 + const char *relative_base,
503 + int depth)
504 {
505 char *path;
506 struct strbuf buf = STRBUF_INIT;
@@ -516,7 +512,7 @@ static void read_info_alternates_the_repository(const char *relative_base,
512 return;
513 }
514
519 - link_alt_odb_entries(the_repository, buf.buf, '\n', relative_base, depth);
515 + link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth);
516 strbuf_release(&buf);
517 free(path);
518 }