|
6 | 6 | import pathlib
|
7 | 7 | import re
|
8 | 8 | import shlex
|
| 9 | +import string |
9 | 10 | import typing as t
|
10 | 11 | from collections.abc import Sequence
|
11 | 12 |
|
@@ -1159,26 +1160,71 @@ def init(
|
1159 | 1160 | >>> sha256_repo.mkdir()
|
1160 | 1161 | >>> git = Git(path=sha256_repo)
|
1161 | 1162 | >>> git.init(object_format='sha256') # doctest: +SKIP
|
| 1163 | + 'Initialized empty Git repository in ...' |
1162 | 1164 | """
|
1163 | 1165 | local_flags: list[str] = []
|
1164 | 1166 | required_flags: list[str] = [str(self.path)]
|
1165 | 1167 |
|
1166 | 1168 | if template is not None:
|
| 1169 | + if not isinstance(template, (str, pathlib.Path)): |
| 1170 | + msg = "template must be a string or Path" |
| 1171 | + raise TypeError(msg) |
| 1172 | + template_path = pathlib.Path(template) |
| 1173 | + if not template_path.is_dir(): |
| 1174 | + msg = f"template directory does not exist: {template}" |
| 1175 | + raise ValueError(msg) |
1167 | 1176 | local_flags.append(f"--template={template}")
|
| 1177 | + |
1168 | 1178 | if separate_git_dir is not None:
|
1169 | 1179 | if isinstance(separate_git_dir, pathlib.Path):
|
1170 | 1180 | separate_git_dir = str(separate_git_dir.absolute())
|
1171 | 1181 | local_flags.append(f"--separate-git-dir={separate_git_dir!s}")
|
| 1182 | + |
1172 | 1183 | if object_format is not None:
|
| 1184 | + if object_format not in {"sha1", "sha256"}: |
| 1185 | + msg = "object_format must be either 'sha1' or 'sha256'" |
| 1186 | + raise ValueError(msg) |
1173 | 1187 | local_flags.append(f"--object-format={object_format}")
|
1174 |
| - if branch is not None: |
1175 |
| - local_flags.extend(["--initial-branch", branch]) |
1176 |
| - elif initial_branch is not None: |
1177 |
| - local_flags.extend(["--initial-branch", initial_branch]) |
| 1188 | + |
| 1189 | + if branch is not None and initial_branch is not None: |
| 1190 | + msg = "Cannot specify both branch and initial_branch" |
| 1191 | + raise ValueError(msg) |
| 1192 | + |
| 1193 | + branch_name = branch or initial_branch |
| 1194 | + if branch_name is not None: |
| 1195 | + if any(c.isspace() for c in branch_name): |
| 1196 | + msg = "Branch name cannot contain whitespace" |
| 1197 | + raise ValueError(msg) |
| 1198 | + local_flags.extend(["--initial-branch", branch_name]) |
| 1199 | + |
1178 | 1200 | if shared is not None:
|
| 1201 | + valid_shared_values = { |
| 1202 | + "false", |
| 1203 | + "true", |
| 1204 | + "umask", |
| 1205 | + "group", |
| 1206 | + "all", |
| 1207 | + "world", |
| 1208 | + "everybody", |
| 1209 | + } |
1179 | 1210 | if isinstance(shared, bool):
|
1180 | 1211 | local_flags.append("--shared")
|
1181 | 1212 | else:
|
| 1213 | + shared_str = str(shared).lower() |
| 1214 | + # Check if it's a valid string value or an octal number |
| 1215 | + if not ( |
| 1216 | + shared_str in valid_shared_values |
| 1217 | + or ( |
| 1218 | + shared_str.isdigit() |
| 1219 | + and len(shared_str) <= 4 |
| 1220 | + and all(c in string.octdigits for c in shared_str) |
| 1221 | + ) |
| 1222 | + ): |
| 1223 | + msg = ( |
| 1224 | + f"Invalid shared value. Must be one of {valid_shared_values} " |
| 1225 | + "or an octal number" |
| 1226 | + ) |
| 1227 | + raise ValueError(msg) |
1182 | 1228 | local_flags.append(f"--shared={shared}")
|
1183 | 1229 | if quiet is True:
|
1184 | 1230 | local_flags.append("--quiet")
|
|
0 commit comments