Skip to content

Commit

Permalink
repository: initialize the_repository in main()
Browse files Browse the repository at this point in the history
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 <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
pclouds authored and gitster committed Mar 5, 2018
1 parent b2e45c6 commit b2f0ece
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions common-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ int main(int argc, const char **argv)

git_setup_gettext();

initialize_the_repository();

attr_start();

git_extract_argv0_path(argv[0]);
Expand Down
18 changes: 13 additions & 5 deletions repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
#include "submodule-config.h"

/* The main repository */
static struct repository the_repo = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &the_index, &hash_algos[GIT_HASH_SHA1], 0, 0
};
struct repository *the_repository = &the_repo;
static struct repository the_repo;
struct repository *the_repository;

void initialize_the_repository(void)
{
the_repository = &the_repo;

the_repo.index = &the_index;
repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
}

static char *git_path_from_env(const char *envvar, const char *git_dir,
const char *path, int fromenv)
Expand Down Expand Up @@ -128,7 +134,9 @@ static int read_and_verify_repository_format(struct repository_format *format,
* Initialize 'repo' based on the provided 'gitdir'.
* Return 0 upon success and a non-zero value upon failure.
*/
int repo_init(struct repository *repo, const char *gitdir, const char *worktree)
static int repo_init(struct repository *repo,
const char *gitdir,
const char *worktree)
{
struct repository_format format;
memset(repo, 0, sizeof(*repo));
Expand Down
2 changes: 1 addition & 1 deletion repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ extern struct repository *the_repository;
extern void repo_set_gitdir(struct repository *repo, const char *path);
extern void repo_set_worktree(struct repository *repo, const char *path);
extern void repo_set_hash_algo(struct repository *repo, int algo);
extern int repo_init(struct repository *repo, const char *gitdir, const char *worktree);
extern void initialize_the_repository(void);
extern int repo_submodule_init(struct repository *submodule,
struct repository *superproject,
const char *path);
Expand Down

0 comments on commit b2f0ece

Please sign in to comment.