Skip to content

Commit 842c196

Browse files
committed
docs: improve Git.init documentation and validation tests
- Enhance Git.init docstrings with detailed parameter descriptions - Add comprehensive examples including SHA-256 object format - Add return value and exception documentation - Improve type hints for shared parameter with Literal types - Add extensive validation tests for all parameters
1 parent c8e63da commit 842c196

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

src/libvcs/cmd/git.py

+49-9
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,10 @@ def init(
10311031
object_format: t.Literal["sha1", "sha256"] | None = None,
10321032
branch: str | None = None,
10331033
initial_branch: str | None = None,
1034-
shared: bool | str | None = None,
1034+
shared: bool
1035+
| Literal[false, true, umask, group, all, world, everybody]
1036+
| str
1037+
| None = None,
10351038
quiet: bool | None = None,
10361039
bare: bool | None = None,
10371040
# libvcs special behavior
@@ -1045,28 +1048,58 @@ def init(
10451048
template : str, optional
10461049
Directory from which templates will be used. The template directory
10471050
contains files and directories that will be copied to the $GIT_DIR
1048-
after it is created.
1051+
after it is created. The template directory will be one of the
1052+
following (in order):
1053+
- The argument given with the --template option
1054+
- The contents of the $GIT_TEMPLATE_DIR environment variable
1055+
- The init.templateDir configuration variable
1056+
- The default template directory: /usr/share/git-core/templates
10491057
separate_git_dir : :attr:`libvcs._internal.types.StrOrBytesPath`, optional
10501058
Instead of placing the git repository in <directory>/.git/, place it in
1051-
the specified path.
1059+
the specified path. The .git file at <directory>/.git will contain a
1060+
gitfile that points to the separate git dir. This is useful when you
1061+
want to store the git directory on a different disk or filesystem.
10521062
object_format : "sha1" | "sha256", optional
10531063
Specify the hash algorithm to use. The default is sha1. Note that
1054-
sha256 is still experimental in git.
1064+
sha256 is still experimental in git and requires git version >= 2.29.0.
1065+
Once the repository is created with a specific hash algorithm, it cannot
1066+
be changed.
10551067
branch : str, optional
10561068
Use the specified name for the initial branch. If not specified, fall
1057-
back to the default name (currently "master").
1069+
back to the default name (currently "master", but may change based on
1070+
init.defaultBranch configuration).
10581071
initial_branch : str, optional
10591072
Alias for branch parameter. Specify the name for the initial branch.
1073+
This is provided for compatibility with newer git versions.
10601074
shared : bool | str, optional
10611075
Specify that the git repository is to be shared amongst several users.
1062-
Can be 'false', 'true', 'umask', 'group', 'all', 'world',
1063-
'everybody', or an octal number.
1076+
Valid values are:
1077+
- false: Turn off sharing (default)
1078+
- true: Same as group
1079+
- umask: Use permissions specified by umask
1080+
- group: Make the repository group-writable
1081+
- all, world, everybody: Same as world, make repo readable by all users
1082+
- An octal number: Explicit mode specification (e.g., "0660")
10641083
quiet : bool, optional
10651084
Only print error and warning messages; all other output will be
1066-
suppressed.
1085+
suppressed. Useful for scripting.
10671086
bare : bool, optional
10681087
Create a bare repository. If GIT_DIR environment is not set, it is set
1069-
to the current working directory.
1088+
to the current working directory. Bare repositories have no working
1089+
tree and are typically used as central repositories.
1090+
check_returncode : bool, optional
1091+
If True, check the return code of the git command and raise a
1092+
CalledProcessError if it is non-zero.
1093+
1094+
Returns
1095+
-------
1096+
str
1097+
The output of the git init command.
1098+
1099+
Raises
1100+
------
1101+
CalledProcessError
1102+
If the git command fails and check_returncode is True.
10701103
10711104
Examples
10721105
--------
@@ -1115,6 +1148,13 @@ def init(
11151148
>>> git = Git(path=template_repo)
11161149
>>> git.init(template=str(tmp_path))
11171150
'Initialized empty Git repository in ...'
1151+
1152+
Create with SHA-256 object format (requires git >= 2.29.0):
1153+
1154+
>>> sha256_repo = tmp_path / 'sha256_example'
1155+
>>> sha256_repo.mkdir()
1156+
>>> git = Git(path=sha256_repo)
1157+
>>> git.init(object_format='sha256') # doctest: +SKIP
11181158
"""
11191159
local_flags: list[str] = []
11201160
required_flags: list[str] = [str(self.path)]

tests/cmd/test_git.py

+39
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,42 @@ def test_git_reinit(tmp_path: pathlib.Path) -> None:
132132
# Reinit
133133
second_result = repo.init()
134134
assert "Reinitialized existing Git repository" in second_result
135+
136+
137+
def test_git_init_validation_errors(tmp_path: pathlib.Path) -> None:
138+
"""Test validation errors in git init."""
139+
repo = git.Git(path=tmp_path)
140+
141+
# Test invalid template type
142+
with pytest.raises(TypeError, match="template must be a string or Path"):
143+
repo.init(template=123) # type: ignore
144+
145+
# Test non-existent template directory
146+
with pytest.raises(ValueError, match="template directory does not exist"):
147+
repo.init(template=str(tmp_path / "nonexistent"))
148+
149+
# Test invalid object format
150+
with pytest.raises(
151+
ValueError,
152+
match="object_format must be either 'sha1' or 'sha256'",
153+
):
154+
repo.init(object_format="invalid") # type: ignore
155+
156+
# Test specifying both branch and initial_branch
157+
with pytest.raises(
158+
ValueError,
159+
match="Cannot specify both branch and initial_branch",
160+
):
161+
repo.init(branch="main", initial_branch="master")
162+
163+
# Test branch name with whitespace
164+
with pytest.raises(ValueError, match="Branch name cannot contain whitespace"):
165+
repo.init(branch="main branch")
166+
167+
# Test invalid shared value
168+
with pytest.raises(ValueError, match="Invalid shared value"):
169+
repo.init(shared="invalid")
170+
171+
# Test invalid octal number for shared
172+
with pytest.raises(ValueError, match="Invalid shared value"):
173+
repo.init(shared="8888") # Invalid octal number

0 commit comments

Comments
 (0)