Skip to content

Commit

Permalink
Merge branch 'jc/move-is-bare-repository-cfg-variable-to-repo' into seen
Browse files Browse the repository at this point in the history
* jc/move-is-bare-repository-cfg-variable-to-repo:
  repository: BUG when is_bare_cfg is not initialized
  setup: initialize is_bare_cfg
  git: remove is_bare_repository_cfg global variable
  • Loading branch information
gitster committed Nov 7, 2024
2 parents f4f24fe + 8771214 commit 4013ec9
Show file tree
Hide file tree
Showing 29 changed files with 81 additions and 56 deletions.
4 changes: 2 additions & 2 deletions attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ static enum git_attr_direction direction;

void git_attr_set_direction(enum git_attr_direction new_direction)
{
if (is_bare_repository() && new_direction != GIT_ATTR_INDEX)
if (repo_is_bare(the_repository) && new_direction != GIT_ATTR_INDEX)
BUG("non-INDEX attr direction in a bare repo");

if (new_direction != direction)
Expand Down Expand Up @@ -847,7 +847,7 @@ static struct attr_stack *read_attr(struct index_state *istate,
res = read_attr_from_index(istate, path, flags);
} else if (tree_oid) {
res = read_attr_from_blob(istate, tree_oid, path, flags);
} else if (!is_bare_repository()) {
} else if (!repo_is_bare(the_repository)) {
if (direction == GIT_ATTR_CHECKOUT) {
res = read_attr_from_index(istate, path, flags);
if (!res)
Expand Down
2 changes: 1 addition & 1 deletion builtin/bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ static enum bisect_error bisect_start(struct bisect_terms *terms, int argc,
struct object_id oid;
const char *head;

if (is_bare_repository())
if (repo_is_bare(the_repository))
no_checkout = 1;

/*
Expand Down
2 changes: 1 addition & 1 deletion builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ int cmd_blame(int argc,

revs.disable_stdin = 1;
setup_revisions(argc, argv, &revs, NULL);
if (!revs.pending.nr && is_bare_repository()) {
if (!revs.pending.nr && repo_is_bare(the_repository)) {
struct commit *head_commit;
struct object_id head_oid;

Expand Down
2 changes: 1 addition & 1 deletion builtin/check-attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int cmd_check_attr(int argc,
struct object_id initialized_oid;
int cnt, i, doubledash, filei;

if (!is_bare_repository())
if (!repo_is_bare(the_repository))
setup_work_tree();

git_config(git_default_config, NULL);
Expand Down
4 changes: 2 additions & 2 deletions builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ int cmd_clone(int argc,
repo_clear(the_repository);

/* At this point, we need the_repository to match the cloned repo. */
if (repo_init(the_repository, git_dir, work_tree))
if (repo_init(the_repository, git_dir, work_tree, -1))
warning(_("failed to initialize the repo, skipping bundle URI"));
else if (fetch_bundle_uri(the_repository, bundle_uri, &has_heuristic))
warning(_("failed to fetch objects from bundle URI '%s'"),
Expand Down Expand Up @@ -1446,7 +1446,7 @@ int cmd_clone(int argc,
repo_clear(the_repository);

/* At this point, we need the_repository to match the cloned repo. */
if (repo_init(the_repository, git_dir, work_tree))
if (repo_init(the_repository, git_dir, work_tree, -1))
warning(_("failed to initialize the repo, skipping bundle URI"));
else if (fetch_bundle_list(the_repository,
transport->bundles))
Expand Down
3 changes: 2 additions & 1 deletion builtin/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,8 @@ static int set_head(const struct ref *remote_refs)
else
head_name = xstrdup(heads.items[0].string);
if (head_name) {
int is_bare = is_bare_repository();
int is_bare = repo_is_bare(the_repository);

if (is_bare) {
strbuf_addstr(&b_head, "HEAD");
strbuf_addf(&b_remote_head, "refs/heads/%s", head_name);
Expand Down
2 changes: 1 addition & 1 deletion builtin/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ struct repository *repo UNUSED)
die(_("failed to parse gc.logExpiry value %s"), cfg.gc_log_expire);

if (cfg.pack_refs < 0)
cfg.pack_refs = !is_bare_repository();
cfg.pack_refs = !repo_is_bare(the_repository);

argc = parse_options(argc, argv, prefix, builtin_gc_options,
builtin_gc_usage, 0);
Expand Down
14 changes: 7 additions & 7 deletions builtin/init-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ int cmd_init_db(int argc,
const struct option init_db_options[] = {
OPT_STRING(0, "template", &template_dir, N_("template-directory"),
N_("directory from which templates will be used")),
OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
OPT_SET_INT(0, "bare", &the_repository->is_bare_cfg,
N_("create a bare repository"), 1),
{ OPTION_CALLBACK, 0, "shared", &init_shared_repository,
N_("permissions"),
Expand All @@ -112,7 +112,7 @@ int cmd_init_db(int argc,

argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);

if (real_git_dir && is_bare_repository_cfg == 1)
if (real_git_dir && the_repository->is_bare_cfg == 1)
die(_("options '%s' and '%s' cannot be used together"), "--separate-git-dir", "--bare");

if (real_git_dir && !is_absolute_path(real_git_dir))
Expand Down Expand Up @@ -156,7 +156,7 @@ int cmd_init_db(int argc,
} else if (0 < argc) {
usage(init_db_usage[0]);
}
if (is_bare_repository_cfg == 1) {
if (the_repository->is_bare_cfg == 1) {
char *cwd = xgetcwd();
setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
free(cwd);
Expand All @@ -183,7 +183,7 @@ int cmd_init_db(int argc,
*/
git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT));
work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT));
if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
if ((!git_dir || the_repository->is_bare_cfg == 1) && work_tree)
die(_("%s (or --work-tree=<directory>) not allowed without "
"specifying %s (or --git-dir=<directory>)"),
GIT_WORK_TREE_ENVIRONMENT,
Expand Down Expand Up @@ -220,10 +220,10 @@ int cmd_init_db(int argc,
strbuf_release(&sb);
}

if (is_bare_repository_cfg < 0)
is_bare_repository_cfg = guess_repository_type(git_dir);
if (the_repository->is_bare_cfg < 0)
the_repository->is_bare_cfg = guess_repository_type(git_dir);

if (!is_bare_repository_cfg) {
if (!the_repository->is_bare_cfg) {
const char *git_dir_parent = strrchr(git_dir, '/');
if (git_dir_parent) {
char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
Expand Down
2 changes: 1 addition & 1 deletion builtin/repack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ int cmd_repack(int argc,

if (write_bitmaps < 0) {
if (!write_midx &&
(!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
(!(pack_everything & ALL_INTO_ONE) || !repo_is_bare(the_repository)))
write_bitmaps = 0;
}
if (pack_kept_objects < 0)
Expand Down
2 changes: 1 addition & 1 deletion builtin/reset.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ int cmd_reset(int argc,
if (reset_type != SOFT && (reset_type != MIXED || repo_get_work_tree(the_repository)))
setup_work_tree();

if (reset_type == MIXED && is_bare_repository())
if (reset_type == MIXED && repo_is_bare(the_repository))
die(_("%s reset is not allowed in a bare repository"),
_(reset_type_names[reset_type]));

Expand Down
2 changes: 1 addition & 1 deletion builtin/rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ int cmd_rev_parse(int argc,
continue;
}
if (!strcmp(arg, "--is-bare-repository")) {
printf("%s\n", is_bare_repository() ? "true"
printf("%s\n", repo_is_bare(the_repository) ? "true"
: "false");
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/submodule--helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ static int add_possible_reference_from_superproject(
struct strbuf err = STRBUF_INIT;
strbuf_add(&sb, odb->path, len);

if (repo_init(&alternate, sb.buf, NULL) < 0)
if (repo_init(&alternate, sb.buf, NULL, the_repository->is_bare_cfg) < 0)
die(_("could not get a repository handle for gitdir '%s'"),
sb.buf);

Expand Down
2 changes: 1 addition & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ static int git_default_core_config(const char *var, const char *value,
}

if (!strcmp(var, "core.bare")) {
is_bare_repository_cfg = git_config_bool(var, value);
the_repository->is_bare_cfg = git_config_bool(var, value);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -4016,7 +4016,7 @@ static void connect_wt_gitdir_in_nested(const char *sub_worktree,
const struct submodule *sub;

/* If the submodule has no working tree, we can ignore it. */
if (repo_init(&subrepo, sub_gitdir, sub_worktree))
if (repo_init(&subrepo, sub_gitdir, sub_worktree, the_repository->is_bare_cfg))
return;

if (repo_read_index(&subrepo) < 0)
Expand Down
7 changes: 0 additions & 7 deletions environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ int has_symlinks = 1;
int minimum_abbrev = 4, default_abbrev = -1;
int ignore_case;
int assume_unchanged;
int is_bare_repository_cfg = -1; /* unspecified */
int warn_on_object_refname_ambiguity = 1;
int repository_format_precious_objects;
char *git_commit_encoding;
Expand Down Expand Up @@ -143,12 +142,6 @@ const char *getenv_safe(struct strvec *argv, const char *name)
return argv->v[argv->nr - 1];
}

int is_bare_repository(void)
{
/* if core.bare is not 'false', let's see if there is a work tree */
return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
}

int have_git_dir(void)
{
return startup_info->have_repository
Expand Down
3 changes: 1 addition & 2 deletions environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ void set_shared_repository(int value);
int get_shared_repository(void);
void reset_shared_repository(void);

extern int is_bare_repository_cfg;
int is_bare_repository(void);
int is_bare_repository(struct repository *repo);
extern char *git_work_tree_cfg;

/* Environment bits from configuration mechanism */
Expand Down
2 changes: 1 addition & 1 deletion git.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
*envchanged = 1;
} else if (!strcmp(cmd, "--bare")) {
char *cwd = xgetcwd();
is_bare_repository_cfg = 1;
the_repository->is_bare_cfg = 1;
setenv(GIT_DIR_ENVIRONMENT, cwd, 0);
free(cwd);
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
Expand Down
4 changes: 2 additions & 2 deletions mailmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ int read_mailmap(struct string_list *map)
map->strdup_strings = 1;
map->cmp = namemap_cmp;

if (!git_mailmap_blob && is_bare_repository())
if (!git_mailmap_blob && repo_is_bare(the_repository))
git_mailmap_blob = xstrdup("HEAD:.mailmap");

if (!startup_info->have_repository || !is_bare_repository())
if (!startup_info->have_repository || !repo_is_bare(the_repository))
err |= read_mailmap_file(map, ".mailmap",
startup_info->have_repository ?
MAILMAP_NOFOLLOW : 0);
Expand Down
2 changes: 1 addition & 1 deletion refs/files-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ static int log_ref_setup(struct files_ref_store *refs,
char *logfile;

if (log_refs_cfg == LOG_REFS_UNSET)
log_refs_cfg = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
log_refs_cfg = repo_is_bare(refs->base.repo) ? LOG_REFS_NONE : LOG_REFS_NORMAL;

files_reflog_path(refs, &logfile_sb, refname);
logfile = strbuf_detach(&logfile_sb, NULL);
Expand Down
2 changes: 1 addition & 1 deletion refs/reftable-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ static int should_write_log(struct reftable_ref_store *refs, const char *refname
{
enum log_refs_config log_refs_cfg = refs->log_all_ref_updates;
if (log_refs_cfg == LOG_REFS_UNSET)
log_refs_cfg = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
log_refs_cfg = repo_is_bare(refs->base.repo) ? LOG_REFS_NONE : LOG_REFS_NORMAL;

switch (log_refs_cfg) {
case LOG_REFS_NONE:
Expand Down
23 changes: 19 additions & 4 deletions repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
extern struct repository *the_repository;

/* The main repository */
static struct repository the_repo;
static struct repository the_repo = {
.is_bare_cfg = -1,
};
struct repository *the_repository = &the_repo;

/*
Expand Down Expand Up @@ -263,10 +265,13 @@ 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.
* is_bare can be passed to indicate whether or not the repository should be
* treated as bare when repo_init() is used to initiate a secondary repository.
*/
int repo_init(struct repository *repo,
const char *gitdir,
const char *worktree)
const char *worktree,
int is_bare)
{
struct repository_format format = REPOSITORY_FORMAT_INIT;
memset(repo, 0, sizeof(*repo));
Expand All @@ -284,6 +289,8 @@ int repo_init(struct repository *repo,
repo_set_ref_storage_format(repo, format.ref_storage_format);
repo->repository_format_worktree_config = format.worktree_config;
repo->repository_format_relative_worktrees = format.relative_worktrees;
if (is_bare > 0)
repo->is_bare_cfg = is_bare;

/* take ownership of format.partial_clone */
repo->repository_format_partial_clone = format.partial_clone;
Expand Down Expand Up @@ -315,7 +322,7 @@ int repo_submodule_init(struct repository *subrepo,
strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
strbuf_repo_worktree_path(&worktree, superproject, "%s", path);

if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
if (repo_init(subrepo, gitdir.buf, worktree.buf, superproject->is_bare_cfg)) {
/*
* If initialization fails then it may be due to the submodule
* not being populated in the superproject's worktree. Instead
Expand All @@ -333,7 +340,7 @@ int repo_submodule_init(struct repository *subrepo,
strbuf_reset(&gitdir);
submodule_name_to_gitdir(&gitdir, superproject, sub->name);

if (repo_init(subrepo, gitdir.buf, NULL)) {
if (repo_init(subrepo, gitdir.buf, NULL, superproject->is_bare_cfg)) {
ret = -1;
goto out;
}
Expand Down Expand Up @@ -454,3 +461,11 @@ int repo_hold_locked_index(struct repository *repo,
BUG("the repo hasn't been setup");
return hold_lock_file_for_update(lf, repo->index_file, flags);
}

int repo_is_bare(struct repository *repo)
{
/* if core.bare is not 'false', let's see if there is a work tree */
if (repo->is_bare_cfg < 0 )
BUG("is_bare_cfg unspecified");
return repo->is_bare_cfg && !repo_get_work_tree(repo);
}
12 changes: 11 additions & 1 deletion repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ struct repository {

/* Indicate if a repository has a different 'commondir' from 'gitdir' */
unsigned different_commondir:1;

/*
* Indicates if the repository is set to be treated as a bare repository,
* through a command line argument, configuration, or environment
* variable.
* -1 means unspecified, 0 indicates non-bare, and 1 indicates bare.
*/
int is_bare_cfg;
};

#ifdef USE_THE_REPOSITORY_VARIABLE
Expand Down Expand Up @@ -189,7 +197,7 @@ void repo_set_ref_storage_format(struct repository *repo,
enum ref_storage_format format);
void initialize_repository(struct repository *repo);
RESULT_MUST_BE_USED
int repo_init(struct repository *r, const char *gitdir, const char *worktree);
int repo_init(struct repository *r, const char *gitdir, const char *worktree, int is_bare);

/*
* Initialize the repository 'subrepo' as the submodule at the given path. If
Expand Down Expand Up @@ -233,4 +241,6 @@ void repo_update_index_if_able(struct repository *, struct lock_file *);
*/
int upgrade_repository_format(int target_version);

int repo_is_bare(struct repository *repo);

#endif /* REPOSITORY_H */
2 changes: 1 addition & 1 deletion scalar.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ static int cmd_reconfigure(int argc, const char **argv)

git_config_clear();

if (repo_init(&r, gitdir.buf, commondir.buf))
if (repo_init(&r, gitdir.buf, commondir.buf, the_repository->is_bare_cfg))
goto loop_end;

old_repo = the_repository;
Expand Down
Loading

0 comments on commit 4013ec9

Please sign in to comment.