Skip to content

Commit b2f0ece

Browse files
pcloudsgitster
authored andcommitted
repository: initialize the_repository in main()
This simplifies initialization of struct repository and anything inside. Easier to read. Easier to add/remove fields. Everything will go through main() common-main.c so this should cover all programs, including t/helper. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b2e45c6 commit b2f0ece

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

common-main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ int main(int argc, const char **argv)
3434

3535
git_setup_gettext();
3636

37+
initialize_the_repository();
38+
3739
attr_start();
3840

3941
git_extract_argv0_path(argv[0]);

repository.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
#include "submodule-config.h"
55

66
/* The main repository */
7-
static struct repository the_repo = {
8-
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &the_index, &hash_algos[GIT_HASH_SHA1], 0, 0
9-
};
10-
struct repository *the_repository = &the_repo;
7+
static struct repository the_repo;
8+
struct repository *the_repository;
9+
10+
void initialize_the_repository(void)
11+
{
12+
the_repository = &the_repo;
13+
14+
the_repo.index = &the_index;
15+
repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
16+
}
1117

1218
static char *git_path_from_env(const char *envvar, const char *git_dir,
1319
const char *path, int fromenv)
@@ -128,7 +134,9 @@ static int read_and_verify_repository_format(struct repository_format *format,
128134
* Initialize 'repo' based on the provided 'gitdir'.
129135
* Return 0 upon success and a non-zero value upon failure.
130136
*/
131-
int repo_init(struct repository *repo, const char *gitdir, const char *worktree)
137+
static int repo_init(struct repository *repo,
138+
const char *gitdir,
139+
const char *worktree)
132140
{
133141
struct repository_format format;
134142
memset(repo, 0, sizeof(*repo));

repository.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ extern struct repository *the_repository;
9191
extern void repo_set_gitdir(struct repository *repo, const char *path);
9292
extern void repo_set_worktree(struct repository *repo, const char *path);
9393
extern void repo_set_hash_algo(struct repository *repo, int algo);
94-
extern int repo_init(struct repository *repo, const char *gitdir, const char *worktree);
94+
extern void initialize_the_repository(void);
9595
extern int repo_submodule_init(struct repository *submodule,
9696
struct repository *superproject,
9797
const char *path);

0 commit comments

Comments
 (0)