Skip to content

Commit ba1c21d

Browse files
pks-tgitster
authored andcommitted
odb: split struct odb_source into separate header
Subsequent commits will expand the `struct odb_source` to become a generic interface for accessing an object database source. As part of these refactorings we'll add a set of function pointers that will significantly expand the structure overall. Prepare for this by splitting out the `struct odb_source` into a separate header. This keeps the high-level object database interface detached from the low-level object database sources. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b1af291 commit ba1c21d

6 files changed

Lines changed: 91 additions & 69 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,7 @@ LIB_OBJS += object-file.o
12141214
LIB_OBJS += object-name.o
12151215
LIB_OBJS += object.o
12161216
LIB_OBJS += odb.o
1217+
LIB_OBJS += odb/source.o
12171218
LIB_OBJS += odb/streaming.o
12181219
LIB_OBJS += oid-array.o
12191220
LIB_OBJS += oidmap.o

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ libgit_sources = [
397397
'object-name.c',
398398
'object.c',
399399
'odb.c',
400+
'odb/source.c',
400401
'odb/streaming.c',
401402
'oid-array.c',
402403
'oidmap.c',

odb.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -217,23 +217,6 @@ static void odb_source_read_alternates(struct odb_source *source,
217217
free(path);
218218
}
219219

220-
221-
static struct odb_source *odb_source_new(struct object_database *odb,
222-
const char *path,
223-
bool local)
224-
{
225-
struct odb_source *source;
226-
227-
CALLOC_ARRAY(source, 1);
228-
source->odb = odb;
229-
source->local = local;
230-
source->path = xstrdup(path);
231-
source->loose = odb_source_loose_new(source);
232-
source->packfiles = packfile_store_new(source);
233-
234-
return source;
235-
}
236-
237220
static struct odb_source *odb_add_alternate_recursively(struct object_database *odb,
238221
const char *source,
239222
int depth)
@@ -373,14 +356,6 @@ struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
373356
return source->next;
374357
}
375358

376-
static void odb_source_free(struct odb_source *source)
377-
{
378-
free(source->path);
379-
odb_source_loose_free(source->loose);
380-
packfile_store_free(source->packfiles);
381-
free(source);
382-
}
383-
384359
void odb_restore_primary_source(struct object_database *odb,
385360
struct odb_source *restore_source,
386361
const char *old_path)

odb.h

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "hashmap.h"
55
#include "object.h"
6+
#include "odb/source.h"
67
#include "oidset.h"
78
#include "oidmap.h"
89
#include "string-list.h"
@@ -30,50 +31,6 @@ extern int fetch_if_missing;
3031
*/
3132
char *compute_alternate_path(const char *path, struct strbuf *err);
3233

33-
/*
34-
* The source is the part of the object database that stores the actual
35-
* objects. It thus encapsulates the logic to read and write the specific
36-
* on-disk format. An object database can have multiple sources:
37-
*
38-
* - The primary source, which is typically located in "$GIT_DIR/objects".
39-
* This is where new objects are usually written to.
40-
*
41-
* - Alternate sources, which are configured via "objects/info/alternates" or
42-
* via the GIT_ALTERNATE_OBJECT_DIRECTORIES environment variable. These
43-
* alternate sources are only used to read objects.
44-
*/
45-
struct odb_source {
46-
struct odb_source *next;
47-
48-
/* Object database that owns this object source. */
49-
struct object_database *odb;
50-
51-
/* Private state for loose objects. */
52-
struct odb_source_loose *loose;
53-
54-
/* Should only be accessed directly by packfile.c and midx.c. */
55-
struct packfile_store *packfiles;
56-
57-
/*
58-
* Figure out whether this is the local source of the owning
59-
* repository, which would typically be its ".git/objects" directory.
60-
* This local object directory is usually where objects would be
61-
* written to.
62-
*/
63-
bool local;
64-
65-
/*
66-
* This object store is ephemeral, so there is no need to fsync.
67-
*/
68-
int will_destroy;
69-
70-
/*
71-
* Path to the source. If this is a relative path, it is relative to
72-
* the current working directory.
73-
*/
74-
char *path;
75-
};
76-
7734
struct packed_git;
7835
struct packfile_store;
7936
struct cached_object_entry;

odb/source.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "git-compat-util.h"
2+
#include "object-file.h"
3+
#include "odb/source.h"
4+
#include "packfile.h"
5+
6+
struct odb_source *odb_source_new(struct object_database *odb,
7+
const char *path,
8+
bool local)
9+
{
10+
struct odb_source *source;
11+
12+
CALLOC_ARRAY(source, 1);
13+
source->odb = odb;
14+
source->local = local;
15+
source->path = xstrdup(path);
16+
source->loose = odb_source_loose_new(source);
17+
source->packfiles = packfile_store_new(source);
18+
19+
return source;
20+
}
21+
22+
void odb_source_free(struct odb_source *source)
23+
{
24+
free(source->path);
25+
odb_source_loose_free(source->loose);
26+
packfile_store_free(source->packfiles);
27+
free(source);
28+
}

odb/source.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef ODB_SOURCE_H
2+
#define ODB_SOURCE_H
3+
4+
/*
5+
* The source is the part of the object database that stores the actual
6+
* objects. It thus encapsulates the logic to read and write the specific
7+
* on-disk format. An object database can have multiple sources:
8+
*
9+
* - The primary source, which is typically located in "$GIT_DIR/objects".
10+
* This is where new objects are usually written to.
11+
*
12+
* - Alternate sources, which are configured via "objects/info/alternates" or
13+
* via the GIT_ALTERNATE_OBJECT_DIRECTORIES environment variable. These
14+
* alternate sources are only used to read objects.
15+
*/
16+
struct odb_source {
17+
struct odb_source *next;
18+
19+
/* Object database that owns this object source. */
20+
struct object_database *odb;
21+
22+
/* Private state for loose objects. */
23+
struct odb_source_loose *loose;
24+
25+
/* Should only be accessed directly by packfile.c and midx.c. */
26+
struct packfile_store *packfiles;
27+
28+
/*
29+
* Figure out whether this is the local source of the owning
30+
* repository, which would typically be its ".git/objects" directory.
31+
* This local object directory is usually where objects would be
32+
* written to.
33+
*/
34+
bool local;
35+
36+
/*
37+
* This object store is ephemeral, so there is no need to fsync.
38+
*/
39+
int will_destroy;
40+
41+
/*
42+
* Path to the source. If this is a relative path, it is relative to
43+
* the current working directory.
44+
*/
45+
char *path;
46+
};
47+
48+
/*
49+
* Allocate and initialize a new source for the given object database located
50+
* at `path`. `local` indicates whether or not the source is the local and thus
51+
* primary object source of the object database.
52+
*/
53+
struct odb_source *odb_source_new(struct object_database *odb,
54+
const char *path,
55+
bool local);
56+
57+
/* Free the object database source, releasing all associated resources. */
58+
void odb_source_free(struct odb_source *source);
59+
60+
#endif

0 commit comments

Comments
 (0)