Skip to content

repository: ignore GIT_COMMON_DIR when checking for an existing repository on init - #7491

Open
ds-env wants to merge 1 commit into
libgit2:mainfrom
ds-env:init-common-dir
Open

ds-env wants to merge 1 commit into
libgit2:mainfrom
ds-env:init-common-dir

Conversation

@ds-env

@ds-env ds-env commented Nov 20, 2026

Copy link
Copy Markdown

git_repository_init_ext passes opts->flags, which are GIT_REPOSITORY_INIT_* flags, to is_valid_repository_path, which expects GIT_REPOSITORY_OPEN_* flags. The two enums share bit values: GIT_REPOSITORY_INIT_MKPATH and GIT_REPOSITORY_OPEN_FROM_ENV are both 1 << 4. git_repository_init always sets MKPATH, and git_repository_init_ext passes it on whenever the caller sets it, so init takes the common directory from GIT_COMMON_DIR. With GIT_COMMON_DIR pointing elsewhere, an existing repository is not recognized, and reinitializing it resets its configuration: core.repositoryformatversion goes back to 0, an unsupported version is not rejected, and core.ignorecase is removed. is_valid_repository_path reads no other open flag, and no init flag asks for the environment, so the commit passes 0, as git_repository_open_bare does. This restores the behaviour before #6544, which added the flags argument.

A question on the same mix-up, which I left alone: repo_init_config also tests the init flags for GIT_REPOSITORY_OPEN_FROM_ENV to get use_env (also from #6544). So git_repository_init always runs with use_env set, and git_repository_init_ext does whenever the caller sets MKPATH. As far as I can see, use_env matters there only on Windows: are_symlinks_supported then reads core.symlinks from the system and global config, and use_env decides whether GIT_CONFIG_NOSYSTEM, GIT_CONFIG_SYSTEM and GIT_CONFIG_GLOBAL choose those files. Should init honor these variables always, never, or through an init flag of its own? I'm happy to change it either way.

To reproduce (on main, 42a89d7)

GIT_COMMON_DIR=/tmp/x build/libgit2_tests -srepo::init   # reinit_nondefault_version, reinit_unsupported_version, reinit_doesnot_overwrite_ignorecase

Testing

The commit adds repo::init::reinit_ignores_common_dir_env: it sets GIT_COMMON_DIR to a directory that does not exist, reinitializes a repository with a non-default format version, checks that the version is kept, and restores GIT_COMMON_DIR in the suite's cleanup. Without the fix it fails (1 != 0), whatever the environment.

With the commit, repo::init passes with GIT_COMMON_DIR unset and set, and the command above passes. The offline suite (-xonline -xstress, Debug build, Linux) passes except core::structinit::compare, which fails on my machine without the commit too.

git_repository_init_ext passed its GIT_REPOSITORY_INIT_* flags to
is_valid_repository_path, which expects GIT_REPOSITORY_OPEN_* flags.
GIT_REPOSITORY_INIT_MKPATH, which git_repository_init always sets, has
the value of GIT_REPOSITORY_OPEN_FROM_ENV, so initializing a repository
took the common directory from GIT_COMMON_DIR. With GIT_COMMON_DIR set,
reinitializing an existing repository looked for it in the wrong place,
and repo::init::reinit_nondefault_version and two more tests fail.

Add a test that reinitializes a repository with GIT_COMMON_DIR pointing
elsewhere.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant