|
4 | 4 |
|
5 | 5 | import functools
|
6 | 6 | import getpass
|
| 7 | +import os |
7 | 8 | import pathlib
|
8 | 9 | import random
|
9 | 10 | import shutil
|
@@ -300,20 +301,55 @@ def __call__(
|
300 | 301 |
|
301 | 302 |
|
302 | 303 | DEFAULT_GIT_REMOTE_REPO_CMD_ARGS = ["--bare"]
|
| 304 | +DEFAULT_GIT_INITIAL_BRANCH = os.environ.get("LIBVCS_GIT_DEFAULT_INITIAL_BRANCH", "main") |
303 | 305 |
|
304 | 306 |
|
305 | 307 | def _create_git_remote_repo(
|
306 | 308 | remote_repo_path: pathlib.Path,
|
307 | 309 | remote_repo_post_init: CreateRepoPostInitFn | None = None,
|
308 | 310 | init_cmd_args: InitCmdArgs = DEFAULT_GIT_REMOTE_REPO_CMD_ARGS,
|
309 | 311 | env: _ENV | None = None,
|
| 312 | + initial_branch: str | None = None, |
310 | 313 | ) -> 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 | + |
311 | 335 | if init_cmd_args is None:
|
312 | 336 | 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) |
317 | 353 |
|
318 | 354 | if remote_repo_post_init is not None and callable(remote_repo_post_init):
|
319 | 355 | remote_repo_post_init(remote_repo_path=remote_repo_path, env=env)
|
|
0 commit comments