repository: introduce raw object store field

The raw object store field will contain any objects needed for access to objects in a given repository. This patch introduces the raw object store and populates it with the `objectdir`, which used to be part of the repository struct. As the struct gains members, we'll also populate the function to clear the memory for these members. In a later step, we'll introduce a struct object_parser, that will complement the object parsing in a repository struct: The raw object parser is the layer that will provide access to raw object content, while the higher level object parser code will parse raw objects and keeps track of parenthood and other object relationships using 'struct object'. For now only add the lower level to the repository struct. 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 90c62155d65a6bec5c2c293c8ece0b22173f63a3
8 files changed +56 -17
builtin/grep.c
+2 -1
@@ -22,6 +22,7 @@
22 #include "pathspec.h"
23 #include "submodule.h"
24 #include "submodule-config.h"
25 +#include "object-store.h"
26
27 static char const * const grep_usage[] = {
28 N_("git grep [<options>] [-e] <pattern> [<rev>...] [[--] <path>...]"),
@@ -432,7 +433,7 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
433 * object.
434 */
435 grep_read_lock();
435 - add_to_alternates_memory(submodule.objectdir);
436 + add_to_alternates_memory(submodule.objects->objectdir);
437 grep_read_unlock();
438
439 if (oid) {
environment.c
+3 -2
@@ -14,6 +14,7 @@
14 #include "fmt-merge-msg.h"
15 #include "commit.h"
16 #include "argv-array.h"
17 +#include "object-store.h"
18
19 int trust_executable_bit = 1;
20 int trust_ctime = 1;
@@ -270,9 +271,9 @@ const char *get_git_work_tree(void)
271
272 char *get_object_directory(void)
273 {
273 - if (!the_repository->objectdir)
274 + if (!the_repository->objects->objectdir)
275 BUG("git environment hasn't been setup");
275 - return the_repository->objectdir;
276 + return the_repository->objects->objectdir;
277 }
278
279 int odb_mkstemp(struct strbuf *template, const char *pattern)
object-store.h new
+18
@@ -0,0 +1,18 @@
1 +#ifndef OBJECT_STORE_H
2 +#define OBJECT_STORE_H
3 +
4 +struct raw_object_store {
5 + /*
6 + * Path to the repository's object store.
7 + * Cannot be NULL after initialization.
8 + */
9 + char *objectdir;
10 +
11 + /* Path to extra alternate object database if not NULL */
12 + char *alternate_db;
13 +};
14 +
15 +struct raw_object_store *raw_object_store_new(void);
16 +void raw_object_store_clear(struct raw_object_store *o);
17 +
18 +#endif /* OBJECT_STORE_H */
object.c
+14
@@ -4,6 +4,7 @@
4 #include "tree.h"
5 #include "commit.h"
6 #include "tag.h"
7 +#include "object-store.h"
8
9 static struct object **obj_hash;
10 static int nr_objs, obj_hash_size;
@@ -445,3 +446,16 @@ void clear_commit_marks_all(unsigned int flags)
446 obj->flags &= ~flags;
447 }
448 }
449 +
450 +struct raw_object_store *raw_object_store_new(void)
451 +{
452 + struct raw_object_store *o = xmalloc(sizeof(*o));
453 +
454 + memset(o, 0, sizeof(*o));
455 + return o;
456 +}
457 +void raw_object_store_clear(struct raw_object_store *o)
458 +{
459 + FREE_AND_NULL(o->objectdir);
460 + FREE_AND_NULL(o->alternate_db);
461 +}
path.c
+2 -1
@@ -10,6 +10,7 @@
10 #include "submodule-config.h"
11 #include "path.h"
12 #include "packfile.h"
13 +#include "object-store.h"
14
15 static int get_st_mode_bits(const char *path, int *mode)
16 {
@@ -382,7 +383,7 @@ static void adjust_git_path(const struct repository *repo,
383 strbuf_splice(buf, 0, buf->len,
384 repo->index_file, strlen(repo->index_file));
385 else if (dir_prefix(base, "objects"))
385 - replace_dir(buf, git_dir_len + 7, repo->objectdir);
386 + replace_dir(buf, git_dir_len + 7, repo->objects->objectdir);
387 else if (git_hooks_path && dir_prefix(base, "hooks"))
388 replace_dir(buf, git_dir_len + 5, git_hooks_path);
389 else if (repo->different_commondir)
repository.c
+10 -5
@@ -1,5 +1,6 @@
1 #include "cache.h"
2 #include "repository.h"
3 +#include "object-store.h"
4 #include "config.h"
5 #include "submodule-config.h"
6
@@ -12,6 +13,7 @@ void initialize_the_repository(void)
13 the_repository = &the_repo;
14
15 the_repo.index = &the_index;
16 + the_repo.objects = raw_object_store_new();
17 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
18 }
19
@@ -58,10 +60,10 @@ void repo_set_gitdir(struct repository *repo,
60 free(old_gitdir);
61
62 repo_set_commondir(repo, o->commondir);
61 - expand_base_dir(&repo->objectdir, o->object_dir,
63 + expand_base_dir(&repo->objects->objectdir, o->object_dir,
64 repo->commondir, "objects");
63 - free(repo->alternate_db);
64 - repo->alternate_db = xstrdup_or_null(o->alternate_db);
65 + free(repo->objects->alternate_db);
66 + repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
67 expand_base_dir(&repo->graft_file, o->graft_file,
68 repo->commondir, "info/grafts");
69 expand_base_dir(&repo->index_file, o->index_file,
@@ -140,6 +142,8 @@ static int repo_init(struct repository *repo,
142 struct repository_format format;
143 memset(repo, 0, sizeof(*repo));
144
145 + repo->objects = raw_object_store_new();
146 +
147 if (repo_init_gitdir(repo, gitdir))
148 goto error;
149
@@ -214,13 +218,14 @@ void repo_clear(struct repository *repo)
218 {
219 FREE_AND_NULL(repo->gitdir);
220 FREE_AND_NULL(repo->commondir);
217 - FREE_AND_NULL(repo->objectdir);
218 - FREE_AND_NULL(repo->alternate_db);
221 FREE_AND_NULL(repo->graft_file);
222 FREE_AND_NULL(repo->index_file);
223 FREE_AND_NULL(repo->worktree);
224 FREE_AND_NULL(repo->submodule_prefix);
225
226 + raw_object_store_clear(repo->objects);
227 + FREE_AND_NULL(repo->objects);
228 +
229 if (repo->config) {
230 git_configset_clear(repo->config);
231 FREE_AND_NULL(repo->config);
repository.h
+4 -7
@@ -2,9 +2,10 @@
2 #define REPOSITORY_H
3
4 struct config_set;
5 +struct git_hash_algo;
6 struct index_state;
7 +struct raw_object_store;
8 struct submodule_cache;
7 -struct git_hash_algo;
9
10 struct repository {
11 /* Environment */
@@ -21,13 +22,9 @@ struct repository {
22 char *commondir;
23
24 /*
24 - * Path to the repository's object store.
25 - * Cannot be NULL after initialization.
25 + * Holds any information related to accessing the raw object content.
26 */
27 - char *objectdir;
28 -
29 - /* Path to extra alternate object database if not NULL */
30 - char *alternate_db;
27 + struct raw_object_store *objects;
28
29 /*
30 * Path to the repository's graft file.
sha1_file.c
+3 -1
@@ -29,6 +29,7 @@
29 #include "quote.h"
30 #include "packfile.h"
31 #include "fetch-object.h"
32 +#include "object-store.h"
33
34 const unsigned char null_sha1[GIT_MAX_RAWSZ];
35 const struct object_id null_oid;
@@ -671,7 +672,8 @@ void prepare_alt_odb(void)
672 return;
673
674 alt_odb_tail = &alt_odb_list;
674 - link_alt_odb_entries(the_repository->alternate_db, PATH_SEP, NULL, 0);
675 + link_alt_odb_entries(the_repository->objects->alternate_db,
676 + PATH_SEP, NULL, 0);
677
678 read_info_alternates(get_object_directory(), 0);
679 }