Skip to content

Commit ddd31f3

Browse files
committed
feat(cmd): Add git.init
1 parent d450985 commit ddd31f3

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

libvcs/cmd/git.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,3 +932,82 @@ def pull(
932932
return self.run(
933933
["pull", *local_flags, "--", *required_flags], check_returncode=False
934934
)
935+
936+
def init(
937+
self,
938+
template: Optional[str] = None,
939+
separate_git_dir: Optional[StrOrBytesPath] = None,
940+
object_format: Optional[Literal["sha1", "sha256"]] = None,
941+
branch: Optional[str] = None,
942+
initial_branch: Optional[str] = None,
943+
shared: Optional[bool] = None,
944+
quiet: Optional[bool] = None,
945+
bare: Optional[bool] = None,
946+
**kwargs,
947+
):
948+
"""Create empty repo. Wraps `git init <https://git-scm.com/docs/git-init>`_.
949+
950+
Parameters
951+
----------
952+
quiet : bool
953+
``--quiet``
954+
bare : bool
955+
``--bare``
956+
object_format :
957+
Hash algorithm used for objects. SHA-256 is still experimental as of git
958+
2.36.0.
959+
960+
Examples
961+
--------
962+
>>> new_repo = tmp_path / 'example'
963+
>>> new_repo.mkdir()
964+
>>> git = Git(dir=new_repo)
965+
>>> git.init()
966+
'Initialized empty Git repository in ...'
967+
>>> pathlib.Path(new_repo / 'test').write_text('foo', 'utf-8')
968+
3
969+
>>> git.run(['add', '.'])
970+
''
971+
972+
Bare:
973+
974+
>>> new_repo = tmp_path / 'example1'
975+
>>> new_repo.mkdir()
976+
>>> git = Git(dir=new_repo)
977+
>>> git.init(bare=True)
978+
'Initialized empty Git repository in ...'
979+
>>> pathlib.Path(new_repo / 'HEAD').exists()
980+
True
981+
982+
Existing repo:
983+
984+
>>> git = Git(dir=new_repo)
985+
>>> git = Git(dir=git_local_clone.dir)
986+
>>> git_remote_repo = create_git_remote_repo()
987+
>>> git.init()
988+
'Reinitialized existing Git repository in ...'
989+
990+
"""
991+
required_flags: list[str] = [str(self.dir)]
992+
local_flags: list[str] = []
993+
994+
if template is not None:
995+
local_flags.append(f"--template={template}")
996+
if separate_git_dir is not None:
997+
local_flags.append(f"--separate-git-dir={separate_git_dir}")
998+
if object_format is not None:
999+
local_flags.append(f"--object-format={object_format}")
1000+
if branch is not None:
1001+
local_flags.append(f"--branch {branch}")
1002+
if initial_branch is not None:
1003+
local_flags.append(f"--initial-branch {initial_branch}")
1004+
if shared is True:
1005+
local_flags.append("--shared")
1006+
if quiet is True:
1007+
local_flags.append("--quiet")
1008+
if bare is True:
1009+
local_flags.append("--bare")
1010+
1011+
return self.run(
1012+
["init", *local_flags, "--", *required_flags], check_returncode=False
1013+
)

0 commit comments

Comments
 (0)