Skip to content

Commit 5467031

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 2616a02 commit 5467031

1 file changed

Lines changed: 42 additions & 4 deletions

File tree

src/libvcs/pytest_plugin.py

Lines changed: 42 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
@@ -298,20 +299,57 @@ def __call__(
298299

299300

300301
DEFAULT_GIT_REMOTE_REPO_CMD_ARGS = ["--bare"]
302+
DEFAULT_GIT_INITIAL_BRANCH = os.environ.get(
303+
"LIBVCS_GIT_DEFAULT_INITIAL_BRANCH", "master"
304+
)
301305

302306

303307
def _create_git_remote_repo(
304308
remote_repo_path: pathlib.Path,
305309
remote_repo_post_init: CreateRepoPostInitFn | None = None,
306310
init_cmd_args: InitCmdArgs = DEFAULT_GIT_REMOTE_REPO_CMD_ARGS,
307311
env: Env | None = None,
312+
initial_branch: str | None = None,
308313
) -> 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+
309335
if init_cmd_args is None:
310336
init_cmd_args = []
311-
run(
312-
["git", "init", remote_repo_path.stem, *init_cmd_args],
313-
cwd=remote_repo_path.parent,
314-
)
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)
315353

316354
if remote_repo_post_init is not None and callable(remote_repo_post_init):
317355
remote_repo_post_init(remote_repo_path=remote_repo_path, env=env)

0 commit comments

Comments
 (0)