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