object-store: move alt_odb_list and alt_odb_tail to object store

In a process with multiple repositories open, alternates should be associated to a single repository and not shared globally. Move alt_odb_list and alt_odb_tail into the_repository and adjust callers to reflect this. Now that the alternative object data base is per repository, we're leaking its memory upon freeing a repository. The next patch plugs this hole. No functional change intended. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.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:20 UTC 031dc927f443fa5274e794c12ac34f19a0e0d318
5 files changed +25 -17
builtin/fsck.c
+4
@@ -1,5 +1,6 @@
1 #include "builtin.h"
2 #include "cache.h"
3 +#include "repository.h"
4 #include "config.h"
5 #include "commit.h"
6 #include "tree.h"
@@ -714,9 +715,12 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
715 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
716 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
717 } else {
718 + struct alternate_object_database *alt_odb_list;
719 +
720 fsck_object_dir(get_object_directory());
721
722 prepare_alt_odb();
723 + alt_odb_list = the_repository->objects->alt_odb_list;
724 for (alt = alt_odb_list; alt; alt = alt->next)
725 fsck_object_dir(alt->path);
726
object-store.h
+5 -2
@@ -1,7 +1,7 @@
1 #ifndef OBJECT_STORE_H
2 #define OBJECT_STORE_H
3
4 -extern struct alternate_object_database {
4 +struct alternate_object_database {
5 struct alternate_object_database *next;
6
7 /* see alt_scratch_buf() */
@@ -19,7 +19,7 @@ extern struct alternate_object_database {
19 struct oid_array loose_objects_cache;
20
21 char path[FLEX_ARRAY];
22 -} *alt_odb_list;
22 +};
23 void prepare_alt_odb(void);
24 char *compute_alternate_path(const char *path, struct strbuf *err);
25 typedef int alt_odb_fn(struct alternate_object_database *, void *);
@@ -61,6 +61,9 @@ struct raw_object_store {
61
62 /* Path to extra alternate object database if not NULL */
63 char *alternate_db;
64 +
65 + struct alternate_object_database *alt_odb_list;
66 + struct alternate_object_database **alt_odb_tail;
67 };
68
69 struct raw_object_store *raw_object_store_new(void);
packfile.c
+2 -1
@@ -1,6 +1,7 @@
1 #include "cache.h"
2 #include "list.h"
3 #include "pack.h"
4 +#include "repository.h"
5 #include "dir.h"
6 #include "mergesort.h"
7 #include "packfile.h"
@@ -892,7 +893,7 @@ void prepare_packed_git(void)
893 return;
894 prepare_packed_git_one(get_object_directory(), 1);
895 prepare_alt_odb();
895 - for (alt = alt_odb_list; alt; alt = alt->next)
896 + for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next)
897 prepare_packed_git_one(alt->path, 0);
898 rearrange_packed_git();
899 prepare_packed_git_mru();
sha1_file.c
+12 -13
@@ -22,6 +22,7 @@
22 #include "pack-revindex.h"
23 #include "sha1-lookup.h"
24 #include "bulk-checkin.h"
25 +#include "repository.h"
26 #include "streaming.h"
27 #include "dir.h"
28 #include "list.h"
@@ -343,9 +344,6 @@ static const char *alt_sha1_path(struct alternate_object_database *alt,
344 return buf->buf;
345 }
346
346 -struct alternate_object_database *alt_odb_list;
347 -static struct alternate_object_database **alt_odb_tail;
348 -
347 /*
348 * Return non-zero iff the path is usable as an alternate object database.
349 */
@@ -365,7 +363,7 @@ static int alt_odb_usable(struct strbuf *path, const char *normalized_objdir)
363 * Prevent the common mistake of listing the same
364 * thing twice, or object directory itself.
365 */
368 - for (alt = alt_odb_list; alt; alt = alt->next) {
366 + for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
367 if (!fspathcmp(path->buf, alt->path))
368 return 0;
369 }
@@ -425,8 +423,8 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
423 ent = alloc_alt_odb(pathbuf.buf);
424
425 /* add the alternate entry */
428 - *alt_odb_tail = ent;
429 - alt_odb_tail = &(ent->next);
426 + *the_repository->objects->alt_odb_tail = ent;
427 + the_repository->objects->alt_odb_tail = &(ent->next);
428 ent->next = NULL;
429
430 /* recursively add alternates */
@@ -560,7 +558,7 @@ void add_to_alternates_file(const char *reference)
558 fprintf_or_die(out, "%s\n", reference);
559 if (commit_lock_file(&lock))
560 die_errno("unable to move new alternates file into place");
563 - if (alt_odb_tail)
561 + if (the_repository->objects->alt_odb_tail)
562 link_alt_odb_entries(reference, '\n', NULL, 0);
563 }
564 free(alts);
@@ -658,7 +656,7 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
656 int r = 0;
657
658 prepare_alt_odb();
661 - for (ent = alt_odb_list; ent; ent = ent->next) {
659 + for (ent = the_repository->objects->alt_odb_list; ent; ent = ent->next) {
660 r = fn(ent, cb);
661 if (r)
662 break;
@@ -668,10 +666,11 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
666
667 void prepare_alt_odb(void)
668 {
671 - if (alt_odb_tail)
669 + if (the_repository->objects->alt_odb_tail)
670 return;
671
674 - alt_odb_tail = &alt_odb_list;
672 + the_repository->objects->alt_odb_tail =
673 + &the_repository->objects->alt_odb_list;
674 link_alt_odb_entries(the_repository->objects->alternate_db,
675 PATH_SEP, NULL, 0);
676
@@ -716,7 +715,7 @@ static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen)
715 {
716 struct alternate_object_database *alt;
717 prepare_alt_odb();
719 - for (alt = alt_odb_list; alt; alt = alt->next) {
718 + for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
719 const char *path = alt_sha1_path(alt, sha1);
720 if (check_and_freshen_file(path, freshen))
721 return 1;
@@ -876,7 +875,7 @@ static int stat_sha1_file(const unsigned char *sha1, struct stat *st,
875
876 prepare_alt_odb();
877 errno = ENOENT;
879 - for (alt = alt_odb_list; alt; alt = alt->next) {
878 + for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
879 *path = alt_sha1_path(alt, sha1);
880 if (!lstat(*path, st))
881 return 0;
@@ -906,7 +905,7 @@ static int open_sha1_file(const unsigned char *sha1, const char **path)
905 most_interesting_errno = errno;
906
907 prepare_alt_odb();
909 - for (alt = alt_odb_list; alt; alt = alt->next) {
908 + for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
909 *path = alt_sha1_path(alt, sha1);
910 fd = git_open(*path);
911 if (fd >= 0)
sha1_name.c
+2 -1
@@ -11,6 +11,7 @@
11 #include "sha1-array.h"
12 #include "packfile.h"
13 #include "object-store.h"
14 +#include "repository.h"
15
16 static int get_oid_oneline(const char *, struct object_id *, struct commit_list *);
17
@@ -105,7 +106,7 @@ static void find_short_object_filename(struct disambiguate_state *ds)
106 */
107 fakeent = alloc_alt_odb(get_object_directory());
108 }
108 - fakeent->next = alt_odb_list;
109 + fakeent->next = the_repository->objects->alt_odb_list;
110
111 for (alt = fakeent; alt && !ds->ambiguous; alt = alt->next) {
112 int pos;