Skip to content

Commit 173761e

Browse files
pks-tgitster
authored andcommitted
setup: start tracking ref storage format
In order to discern which ref storage format a repository is supposed to use we need to start setting up and/or discovering the format. This needs to happen in two separate code paths. - The first path is when we create a repository via `init_db()`. When we are re-initializing a preexisting repository we need to retain the previously used ref storage format -- if the user asked for a different format then this indicates an error and we error out. Otherwise we either initialize the repository with the format asked for by the user or the default format, which currently is the "files" backend. - The second path is when discovering repositories, where we need to read the config of that repository. There is not yet any way to configure something other than the "files" backend, so we can just blindly set the ref storage format to this backend. Wire up this logic so that we have the ref storage format always readily available when needed. As there is only a single backend and because it is not configurable we cannot yet verify that this tracking works as expected via tests, but tests will be added in subsequent commits. To countermand this ommission now though, raise a BUG() in case the ref storage format is not set up properly in `ref_store_init()`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0fcc285 commit 173761e

File tree

7 files changed

+48
-9
lines changed

7 files changed

+48
-9
lines changed

builtin/clone.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11071107
* repository, and reference backends may persist that information into
11081108
* their on-disk data structures.
11091109
*/
1110-
init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
1110+
init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN,
1111+
REF_STORAGE_FORMAT_UNKNOWN, NULL,
11111112
do_not_override_repo_unix_permissions, INIT_DB_QUIET | INIT_DB_SKIP_REFDB);
11121113

11131114
if (real_git_dir) {
@@ -1292,7 +1293,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12921293
hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
12931294
initialize_repository_version(hash_algo, 1);
12941295
repo_set_hash_algo(the_repository, hash_algo);
1295-
create_reference_database(NULL, 1);
1296+
create_reference_database(the_repository->ref_storage_format, NULL, 1);
12961297

12971298
/*
12981299
* Before fetching from the remote, download and install bundle

builtin/init-db.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "object-file.h"
1212
#include "parse-options.h"
1313
#include "path.h"
14+
#include "refs.h"
1415
#include "setup.h"
1516
#include "strbuf.h"
1617

@@ -236,5 +237,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
236237

237238
flags |= INIT_DB_EXIST_OK;
238239
return init_db(git_dir, real_git_dir, template_dir, hash_algo,
239-
initial_branch, init_shared_repository, flags);
240+
REF_STORAGE_FORMAT_UNKNOWN, initial_branch,
241+
init_shared_repository, flags);
240242
}

refs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,10 +2045,10 @@ static struct ref_store *ref_store_init(struct repository *repo,
20452045
const char *gitdir,
20462046
unsigned int flags)
20472047
{
2048-
unsigned int format = REF_STORAGE_FORMAT_FILES;
2049-
const struct ref_storage_be *be = find_ref_storage_backend(format);
2048+
const struct ref_storage_be *be;
20502049
struct ref_store *refs;
20512050

2051+
be = find_ref_storage_backend(repo->ref_storage_format);
20522052
if (!be)
20532053
BUG("reference backend is unknown");
20542054

repository.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ void repo_set_hash_algo(struct repository *repo, int hash_algo)
104104
repo->hash_algo = &hash_algos[hash_algo];
105105
}
106106

107+
void repo_set_ref_storage_format(struct repository *repo, unsigned int format)
108+
{
109+
repo->ref_storage_format = format;
110+
}
111+
107112
/*
108113
* Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
109114
* Return 0 upon success and a non-zero value upon failure.
@@ -184,6 +189,7 @@ int repo_init(struct repository *repo,
184189
goto error;
185190

186191
repo_set_hash_algo(repo, format.hash_algo);
192+
repo_set_ref_storage_format(repo, format.ref_storage_format);
187193
repo->repository_format_worktree_config = format.worktree_config;
188194

189195
/* take ownership of format.partial_clone */

repository.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ struct repository {
163163
/* Repository's current hash algorithm, as serialized on disk. */
164164
const struct git_hash_algo *hash_algo;
165165

166+
/* Repository's reference storage format, as serialized on disk. */
167+
unsigned int ref_storage_format;
168+
166169
/* A unique-id for tracing purposes. */
167170
int trace2_repo_id;
168171

@@ -202,6 +205,7 @@ void repo_set_gitdir(struct repository *repo, const char *root,
202205
const struct set_gitdir_args *extra_args);
203206
void repo_set_worktree(struct repository *repo, const char *path);
204207
void repo_set_hash_algo(struct repository *repo, int algo);
208+
void repo_set_ref_storage_format(struct repository *repo, unsigned int format);
205209
void initialize_the_repository(void);
206210
RESULT_MUST_BE_USED
207211
int repo_init(struct repository *r, const char *gitdir, const char *worktree);

setup.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
15661566
}
15671567
if (startup_info->have_repository) {
15681568
repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
1569+
repo_set_ref_storage_format(the_repository,
1570+
repo_fmt.ref_storage_format);
15691571
the_repository->repository_format_worktree_config =
15701572
repo_fmt.worktree_config;
15711573
/* take ownership of repo_fmt.partial_clone */
@@ -1659,6 +1661,8 @@ void check_repository_format(struct repository_format *fmt)
16591661
check_repository_format_gently(get_git_dir(), fmt, NULL);
16601662
startup_info->have_repository = 1;
16611663
repo_set_hash_algo(the_repository, fmt->hash_algo);
1664+
repo_set_ref_storage_format(the_repository,
1665+
fmt->ref_storage_format);
16621666
the_repository->repository_format_worktree_config =
16631667
fmt->worktree_config;
16641668
the_repository->repository_format_partial_clone =
@@ -1899,7 +1903,8 @@ static int is_reinit(void)
18991903
return ret;
19001904
}
19011905

1902-
void create_reference_database(const char *initial_branch, int quiet)
1906+
void create_reference_database(unsigned int ref_storage_format,
1907+
const char *initial_branch, int quiet)
19031908
{
19041909
struct strbuf err = STRBUF_INIT;
19051910
int reinit = is_reinit();
@@ -1919,6 +1924,7 @@ void create_reference_database(const char *initial_branch, int quiet)
19191924
safe_create_dir(git_path("refs"), 1);
19201925
adjust_shared_perm(git_path("refs"));
19211926

1927+
repo_set_ref_storage_format(the_repository, ref_storage_format);
19221928
if (refs_init_db(&err))
19231929
die("failed to set up refs db: %s", err.buf);
19241930

@@ -2137,8 +2143,22 @@ static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash
21372143
}
21382144
}
21392145

2146+
static void validate_ref_storage_format(struct repository_format *repo_fmt,
2147+
unsigned int format)
2148+
{
2149+
if (repo_fmt->version >= 0 &&
2150+
format != REF_STORAGE_FORMAT_UNKNOWN &&
2151+
format != repo_fmt->ref_storage_format) {
2152+
die(_("attempt to reinitialize repository with different reference storage format"));
2153+
} else if (format != REF_STORAGE_FORMAT_UNKNOWN) {
2154+
repo_fmt->ref_storage_format = format;
2155+
}
2156+
}
2157+
21402158
int init_db(const char *git_dir, const char *real_git_dir,
2141-
const char *template_dir, int hash, const char *initial_branch,
2159+
const char *template_dir, int hash,
2160+
unsigned int ref_storage_format,
2161+
const char *initial_branch,
21422162
int init_shared_repository, unsigned int flags)
21432163
{
21442164
int reinit;
@@ -2181,13 +2201,15 @@ int init_db(const char *git_dir, const char *real_git_dir,
21812201
check_repository_format(&repo_fmt);
21822202

21832203
validate_hash_algorithm(&repo_fmt, hash);
2204+
validate_ref_storage_format(&repo_fmt, ref_storage_format);
21842205

21852206
reinit = create_default_files(template_dir, original_git_dir,
21862207
&repo_fmt, prev_bare_repository,
21872208
init_shared_repository);
21882209

21892210
if (!(flags & INIT_DB_SKIP_REFDB))
2190-
create_reference_database(initial_branch, flags & INIT_DB_QUIET);
2211+
create_reference_database(repo_fmt.ref_storage_format,
2212+
initial_branch, flags & INIT_DB_QUIET);
21912213
create_object_directory();
21922214

21932215
if (get_shared_repository()) {

setup.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ struct repository_format {
115115
int worktree_config;
116116
int is_bare;
117117
int hash_algo;
118+
unsigned int ref_storage_format;
118119
int sparse_index;
119120
char *work_tree;
120121
struct string_list unknown_extensions;
@@ -131,6 +132,7 @@ struct repository_format {
131132
.version = -1, \
132133
.is_bare = -1, \
133134
.hash_algo = GIT_HASH_SHA1, \
135+
.ref_storage_format = REF_STORAGE_FORMAT_FILES, \
134136
.unknown_extensions = STRING_LIST_INIT_DUP, \
135137
.v1_only_extensions = STRING_LIST_INIT_DUP, \
136138
}
@@ -175,10 +177,12 @@ void check_repository_format(struct repository_format *fmt);
175177

176178
int init_db(const char *git_dir, const char *real_git_dir,
177179
const char *template_dir, int hash_algo,
180+
unsigned int ref_storage_format,
178181
const char *initial_branch, int init_shared_repository,
179182
unsigned int flags);
180183
void initialize_repository_version(int hash_algo, int reinit);
181-
void create_reference_database(const char *initial_branch, int quiet);
184+
void create_reference_database(unsigned int ref_storage_format,
185+
const char *initial_branch, int quiet);
182186

183187
/*
184188
* NOTE NOTE NOTE!!

0 commit comments

Comments
 (0)