Skip to content

Commit 031dc92

Browse files
stefanbellergitster
authored andcommitted
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>
1 parent 0d4a132 commit 031dc92

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

builtin/fsck.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "builtin.h"
22
#include "cache.h"
3+
#include "repository.h"
34
#include "config.h"
45
#include "commit.h"
56
#include "tree.h"
@@ -714,9 +715,12 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
714715
for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
715716
for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
716717
} else {
718+
struct alternate_object_database *alt_odb_list;
719+
717720
fsck_object_dir(get_object_directory());
718721

719722
prepare_alt_odb();
723+
alt_odb_list = the_repository->objects->alt_odb_list;
720724
for (alt = alt_odb_list; alt; alt = alt->next)
721725
fsck_object_dir(alt->path);
722726

object-store.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef OBJECT_STORE_H
22
#define OBJECT_STORE_H
33

4-
extern struct alternate_object_database {
4+
struct alternate_object_database {
55
struct alternate_object_database *next;
66

77
/* see alt_scratch_buf() */
@@ -19,7 +19,7 @@ extern struct alternate_object_database {
1919
struct oid_array loose_objects_cache;
2020

2121
char path[FLEX_ARRAY];
22-
} *alt_odb_list;
22+
};
2323
void prepare_alt_odb(void);
2424
char *compute_alternate_path(const char *path, struct strbuf *err);
2525
typedef int alt_odb_fn(struct alternate_object_database *, void *);
@@ -61,6 +61,9 @@ struct raw_object_store {
6161

6262
/* Path to extra alternate object database if not NULL */
6363
char *alternate_db;
64+
65+
struct alternate_object_database *alt_odb_list;
66+
struct alternate_object_database **alt_odb_tail;
6467
};
6568

6669
struct raw_object_store *raw_object_store_new(void);

packfile.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cache.h"
22
#include "list.h"
33
#include "pack.h"
4+
#include "repository.h"
45
#include "dir.h"
56
#include "mergesort.h"
67
#include "packfile.h"
@@ -892,7 +893,7 @@ void prepare_packed_git(void)
892893
return;
893894
prepare_packed_git_one(get_object_directory(), 1);
894895
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)
896897
prepare_packed_git_one(alt->path, 0);
897898
rearrange_packed_git();
898899
prepare_packed_git_mru();

sha1_file.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "pack-revindex.h"
2323
#include "sha1-lookup.h"
2424
#include "bulk-checkin.h"
25+
#include "repository.h"
2526
#include "streaming.h"
2627
#include "dir.h"
2728
#include "list.h"
@@ -343,9 +344,6 @@ static const char *alt_sha1_path(struct alternate_object_database *alt,
343344
return buf->buf;
344345
}
345346

346-
struct alternate_object_database *alt_odb_list;
347-
static struct alternate_object_database **alt_odb_tail;
348-
349347
/*
350348
* Return non-zero iff the path is usable as an alternate object database.
351349
*/
@@ -365,7 +363,7 @@ static int alt_odb_usable(struct strbuf *path, const char *normalized_objdir)
365363
* Prevent the common mistake of listing the same
366364
* thing twice, or object directory itself.
367365
*/
368-
for (alt = alt_odb_list; alt; alt = alt->next) {
366+
for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
369367
if (!fspathcmp(path->buf, alt->path))
370368
return 0;
371369
}
@@ -425,8 +423,8 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
425423
ent = alloc_alt_odb(pathbuf.buf);
426424

427425
/* 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);
430428
ent->next = NULL;
431429

432430
/* recursively add alternates */
@@ -560,7 +558,7 @@ void add_to_alternates_file(const char *reference)
560558
fprintf_or_die(out, "%s\n", reference);
561559
if (commit_lock_file(&lock))
562560
die_errno("unable to move new alternates file into place");
563-
if (alt_odb_tail)
561+
if (the_repository->objects->alt_odb_tail)
564562
link_alt_odb_entries(reference, '\n', NULL, 0);
565563
}
566564
free(alts);
@@ -658,7 +656,7 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
658656
int r = 0;
659657

660658
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) {
662660
r = fn(ent, cb);
663661
if (r)
664662
break;
@@ -668,10 +666,11 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
668666

669667
void prepare_alt_odb(void)
670668
{
671-
if (alt_odb_tail)
669+
if (the_repository->objects->alt_odb_tail)
672670
return;
673671

674-
alt_odb_tail = &alt_odb_list;
672+
the_repository->objects->alt_odb_tail =
673+
&the_repository->objects->alt_odb_list;
675674
link_alt_odb_entries(the_repository->objects->alternate_db,
676675
PATH_SEP, NULL, 0);
677676

@@ -716,7 +715,7 @@ static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen)
716715
{
717716
struct alternate_object_database *alt;
718717
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) {
720719
const char *path = alt_sha1_path(alt, sha1);
721720
if (check_and_freshen_file(path, freshen))
722721
return 1;
@@ -876,7 +875,7 @@ static int stat_sha1_file(const unsigned char *sha1, struct stat *st,
876875

877876
prepare_alt_odb();
878877
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) {
880879
*path = alt_sha1_path(alt, sha1);
881880
if (!lstat(*path, st))
882881
return 0;
@@ -906,7 +905,7 @@ static int open_sha1_file(const unsigned char *sha1, const char **path)
906905
most_interesting_errno = errno;
907906

908907
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) {
910909
*path = alt_sha1_path(alt, sha1);
911910
fd = git_open(*path);
912911
if (fd >= 0)

sha1_name.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "sha1-array.h"
1212
#include "packfile.h"
1313
#include "object-store.h"
14+
#include "repository.h"
1415

1516
static int get_oid_oneline(const char *, struct object_id *, struct commit_list *);
1617

@@ -105,7 +106,7 @@ static void find_short_object_filename(struct disambiguate_state *ds)
105106
*/
106107
fakeent = alloc_alt_odb(get_object_directory());
107108
}
108-
fakeent->next = alt_odb_list;
109+
fakeent->next = the_repository->objects->alt_odb_list;
109110

110111
for (alt = fakeent; alt && !ds->ambiguous; alt = alt->next) {
111112
int pos;

0 commit comments

Comments
 (0)