Skip to content

Commit 2339972

Browse files
committed
pytest_plugin(fix[_create_git_remote_repo]): Use Git.init() for version compatibility
why: Fix compatibility with Git 2.43.0+ which has stricter init behavior what: - Replace raw run() command with Git.init() method - Add initial_branch parameter with env var and default fallback - Try --initial-branch flag first, fall back for older Git versions - Add DEFAULT_GIT_INITIAL_BRANCH constant configurable via env var This ensures Git repository creation works across all Git versions by leveraging libvcs's own Git command abstraction layer.
1 parent 172b3bc commit 2339972

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

src/libvcs/pytest_plugin.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import functools
66
import getpass
7+
import os
78
import pathlib
89
import random
910
import shutil
@@ -300,20 +301,55 @@ def __call__(
300301

301302

302303
DEFAULT_GIT_REMOTE_REPO_CMD_ARGS = ["--bare"]
304+
DEFAULT_GIT_INITIAL_BRANCH = os.environ.get("LIBVCS_GIT_DEFAULT_INITIAL_BRANCH", "main")
303305

304306

305307
def _create_git_remote_repo(
306308
remote_repo_path: pathlib.Path,
307309
remote_repo_post_init: CreateRepoPostInitFn | None = None,
308310
init_cmd_args: InitCmdArgs = DEFAULT_GIT_REMOTE_REPO_CMD_ARGS,
309311
env: _ENV | None = None,
312+
initial_branch: str | None = None,
310313
) -> pathlib.Path:
314+
"""Create a git repository with version-aware initialization.
315+
316+
Parameters
317+
----------
318+
remote_repo_path : pathlib.Path
319+
Path where the repository will be created
320+
remote_repo_post_init : CreateRepoPostInitFn | None
321+
Optional callback to run after repository creation
322+
init_cmd_args : InitCmdArgs
323+
Additional arguments for git init (e.g., ["--bare"])
324+
env : _ENV | None
325+
Environment variables to use
326+
initial_branch : str | None
327+
Name of the initial branch. If None, uses LIBVCS_GIT_DEFAULT_INITIAL_BRANCH
328+
environment variable or "main" as default.
329+
"""
330+
from libvcs.cmd.git import Git
331+
332+
if initial_branch is None:
333+
initial_branch = DEFAULT_GIT_INITIAL_BRANCH
334+
311335
if init_cmd_args is None:
312336
init_cmd_args = []
313-
run(
314-
["git", "init", remote_repo_path.stem, *init_cmd_args],
315-
cwd=remote_repo_path.parent,
316-
)
337+
338+
# Parse init_cmd_args to determine if --bare is requested
339+
bare = "--bare" in init_cmd_args
340+
341+
# Create the directory
342+
remote_repo_path.mkdir(parents=True, exist_ok=True)
343+
344+
# Create Git instance for the new repository
345+
git = Git(path=remote_repo_path)
346+
347+
try:
348+
# Try with --initial-branch (Git 2.30.0+)
349+
git.init(initial_branch=initial_branch, bare=bare, check_returncode=True)
350+
except exc.CommandError:
351+
# Fall back to plain init for older Git versions
352+
git.init(bare=bare, check_returncode=True)
317353

318354
if remote_repo_post_init is not None and callable(remote_repo_post_init):
319355
remote_repo_post_init(remote_repo_path=remote_repo_path, env=env)

0 commit comments

Comments
 (0)