gitbolt 0.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gitbolt/constants.py ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+
4
+ """
5
+ Constants related to gitbolt.
6
+ """
7
+
8
+ from pathlib import Path
9
+
10
+ GIT_DIR_STR = ".git"
11
+ GIT_DIR = Path(GIT_DIR_STR)
12
+ GIT_DEFAULT_BRANCH_CONFIG_KEY = "init.defaultbranch"
gitbolt/exceptions.py ADDED
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+
4
+ """
5
+ Exceptions specific to git.
6
+ """
7
+
8
+ from vt.utils.errors.error_specs.exceptions import VTException, VTExitingException
9
+
10
+
11
+ class GitException(VTException):
12
+ """
13
+ ``VTException`` specific to git.
14
+
15
+ Examples:
16
+
17
+ * raise exception:
18
+
19
+ >>> raise GitException()
20
+ Traceback (most recent call last):
21
+ gitbolt.exceptions.GitException
22
+
23
+ * raise exception with a message:
24
+
25
+ >>> raise GitException('unexpected.')
26
+ Traceback (most recent call last):
27
+ gitbolt.exceptions.GitException: unexpected.
28
+
29
+ * raise exception from another exception:
30
+
31
+ >>> raise GitException() from ValueError
32
+ Traceback (most recent call last):
33
+ gitbolt.exceptions.GitException: ValueError
34
+
35
+ ... rest examples mimic ``VTException`` examples.
36
+ """
37
+
38
+ pass
39
+
40
+
41
+ class GitExitingException(GitException, VTExitingException):
42
+ """
43
+ ``GitException`` that carries an ``exit_code``.
44
+ """
45
+
46
+ pass
gitbolt/hash_object.py ADDED
@@ -0,0 +1,263 @@
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+
4
+ """
5
+ Helper interfaces specific to ``git hash-object`` subcommand.
6
+ """
7
+
8
+ from abc import abstractmethod
9
+ from pathlib import Path
10
+ from typing import Protocol, override, Literal
11
+
12
+ from gitbolt._internal_init import errmsg_creator
13
+ from gitbolt.exceptions import GitExitingException
14
+ from vt.utils.commons.commons.core_py import has_atleast_one_arg
15
+ from vt.utils.errors.error_specs import ERR_DATA_FORMAT_ERR, ERR_INVALID_USAGE
16
+ from vt.utils.errors.error_specs.utils import require_type, require_iterable
17
+
18
+ _HASH_OBJECT_TYPES = ("commit", "tree", "blob", "tag")
19
+
20
+
21
+ class HashObjectArgsValidator(Protocol):
22
+ """
23
+ The argument validator for ``git hash-object`` subcommand.
24
+ """
25
+
26
+ @abstractmethod
27
+ def validate(
28
+ self,
29
+ file_path: Path | None = None,
30
+ *file_paths: Path,
31
+ t: Literal["commit", "tree", "blob", "tag"] = "blob",
32
+ path: Path | None = None,
33
+ no_filters: bool = False,
34
+ literally: bool = False,
35
+ stdin: bytes | None = None,
36
+ stdin_paths: list[Path] | None = None,
37
+ w: bool = False,
38
+ ) -> None:
39
+ """
40
+ Validate the inputs provided to the ``git hash-object`` command.
41
+
42
+ Either a file path or stdin must be present. ``stdin_paths`` counts as stdin
43
+ because Git reads those paths from standard input.
44
+
45
+ :raises GitExitingException: When validation fails.
46
+ """
47
+ ...
48
+
49
+
50
+ class UtilHashObjectArgsValidator(HashObjectArgsValidator):
51
+ """
52
+ Independent utility function sort of interface to perform hash_object() arguments validation.
53
+ """
54
+
55
+ @override
56
+ def validate(
57
+ self,
58
+ file_path: Path | None = None,
59
+ *file_paths: Path,
60
+ t: Literal["commit", "tree", "blob", "tag"] = "blob",
61
+ path: Path | None = None,
62
+ no_filters: bool = False,
63
+ literally: bool = False,
64
+ stdin: bytes | None = None,
65
+ stdin_paths: list[Path] | None = None,
66
+ w: bool = False,
67
+ ) -> None:
68
+ """
69
+ Validate the inputs provided to the ``git hash-object`` command.
70
+
71
+ This includes:
72
+
73
+ * Either a file path or stdin must be present. ``stdin_paths`` is stdin.
74
+ * ``file_path`` / extra file paths cannot be combined with ``stdin_paths``.
75
+ * ``stdin`` cannot be combined with ``stdin_paths``.
76
+ * ``path`` cannot be combined with ``no_filters``.
77
+ * ``path`` cannot be combined with ``stdin_paths``.
78
+ * Types for ``Path``, ``list[Path]``, object type literal, bools, and ``bytes``.
79
+
80
+ All validations will raise a ``GitExitingException`` with a specific exit code:
81
+
82
+ * ``TypeError`` leads to ``ERR_DATA_FORMAT_ERR``.
83
+ * ``ValueError`` leads to ``ERR_INVALID_USAGE``.
84
+
85
+ See: `git hash-object documentation <https://git-scm.com/docs/git-hash-object>`_.
86
+
87
+ Examples::
88
+
89
+ >>> UtilHashObjectArgsValidator().validate(Path("README.md"))
90
+ >>> UtilHashObjectArgsValidator().validate(Path("a.txt"), Path("b.txt"), no_filters=True)
91
+ >>> UtilHashObjectArgsValidator().validate(stdin=b"blob-bytes")
92
+ >>> UtilHashObjectArgsValidator().validate(stdin_paths=[Path("a.txt")])
93
+ >>> UtilHashObjectArgsValidator().validate(Path("a.txt"), path=Path("filters/a.txt"))
94
+ >>> UtilHashObjectArgsValidator().validate(Path("a.txt"), t="commit", literally=True, w=True)
95
+
96
+ Invalid Examples::
97
+
98
+ >>> UtilHashObjectArgsValidator().validate()
99
+ Traceback (most recent call last):
100
+ gitbolt.exceptions.GitExitingException: ValueError: Either file_path or stdin is required
101
+
102
+ >>> UtilHashObjectArgsValidator().validate(Path("a.txt"), stdin_paths=[Path("b.txt")])
103
+ Traceback (most recent call last):
104
+ gitbolt.exceptions.GitExitingException: ValueError: file_path and stdin_paths are not allowed together
105
+
106
+ >>> UtilHashObjectArgsValidator().validate(stdin=b"x", stdin_paths=[Path("a.txt")])
107
+ Traceback (most recent call last):
108
+ gitbolt.exceptions.GitExitingException: ValueError: stdin and stdin_paths are not allowed together
109
+
110
+ >>> UtilHashObjectArgsValidator().validate(Path("a.txt"), path=Path("p"), no_filters=True)
111
+ Traceback (most recent call last):
112
+ gitbolt.exceptions.GitExitingException: ValueError: path and no_filters are not allowed together
113
+
114
+ >>> UtilHashObjectArgsValidator().validate(path=Path("p"), stdin_paths=[Path("a.txt")])
115
+ Traceback (most recent call last):
116
+ gitbolt.exceptions.GitExitingException: ValueError: path and stdin_paths are not allowed together
117
+
118
+ >>> UtilHashObjectArgsValidator().validate("a.txt") # type: ignore[arg-type]
119
+ Traceback (most recent call last):
120
+ gitbolt.exceptions.GitExitingException: TypeError: 'file_path' must be a pathlib.Path
121
+
122
+ >>> UtilHashObjectArgsValidator().validate(Path("a.txt"), t="blobb") # type: ignore[arg-type]
123
+ Traceback (most recent call last):
124
+ gitbolt.exceptions.GitExitingException: ValueError: Unexpected t value. Choose from 'commit', 'tree', 'blob' and 'tag'.
125
+ """
126
+ self.mandate_required_arguments(
127
+ file_path,
128
+ *file_paths,
129
+ stdin=stdin,
130
+ stdin_paths=stdin_paths,
131
+ )
132
+ self.validate_exclusive_args(
133
+ file_path,
134
+ *file_paths,
135
+ path=path,
136
+ no_filters=no_filters,
137
+ stdin=stdin,
138
+ stdin_paths=stdin_paths,
139
+ )
140
+ self.validate_types(
141
+ file_path,
142
+ *file_paths,
143
+ t=t,
144
+ path=path,
145
+ no_filters=no_filters,
146
+ literally=literally,
147
+ stdin=stdin,
148
+ stdin_paths=stdin_paths,
149
+ w=w,
150
+ )
151
+
152
+ def mandate_required_arguments(
153
+ self,
154
+ file_path: Path | None,
155
+ *file_paths: Path,
156
+ stdin: bytes | None,
157
+ stdin_paths: list[Path] | None,
158
+ ) -> None:
159
+ """
160
+ Either a file path or stdin must be provided. ``stdin_paths`` counts as stdin.
161
+
162
+ >>> UtilHashObjectArgsValidator().mandate_required_arguments(Path("a.txt"), stdin=None, stdin_paths=None)
163
+ >>> UtilHashObjectArgsValidator().mandate_required_arguments(None, stdin=b"x", stdin_paths=None)
164
+ >>> UtilHashObjectArgsValidator().mandate_required_arguments(None, stdin=None, stdin_paths=[Path("a.txt")])
165
+
166
+ >>> UtilHashObjectArgsValidator().mandate_required_arguments(None, stdin=None, stdin_paths=None)
167
+ Traceback (most recent call last):
168
+ gitbolt.exceptions.GitExitingException: ValueError: Either file_path or stdin is required
169
+ """
170
+ has_file_path = has_atleast_one_arg(file_path, *file_paths, enforce_type=False)
171
+ has_stdin = stdin is not None or stdin_paths is not None
172
+ if not has_file_path and not has_stdin:
173
+ errmsg = errmsg_creator.at_least_one_required("file_path", "stdin")
174
+ raise GitExitingException(
175
+ errmsg, exit_code=ERR_INVALID_USAGE
176
+ ) from ValueError(errmsg)
177
+
178
+ def validate_exclusive_args(
179
+ self,
180
+ file_path: Path | None,
181
+ *file_paths: Path,
182
+ path: Path | None,
183
+ no_filters: bool,
184
+ stdin: bytes | None,
185
+ stdin_paths: list[Path] | None,
186
+ ) -> None:
187
+ """
188
+ Check that exclusive args are not provided together.
189
+
190
+ >>> UtilHashObjectArgsValidator().validate_exclusive_args(
191
+ ... Path("a.txt"), path=None, no_filters=False, stdin=None, stdin_paths=None
192
+ ... )
193
+ >>> UtilHashObjectArgsValidator().validate_exclusive_args(
194
+ ... Path("a.txt"), stdin_paths=[Path("b.txt")], path=None, no_filters=False, stdin=None
195
+ ... )
196
+ Traceback (most recent call last):
197
+ gitbolt.exceptions.GitExitingException: ValueError: file_path and stdin_paths are not allowed together
198
+ """
199
+ has_files = has_atleast_one_arg(file_path, *file_paths, enforce_type=False)
200
+ if has_files and stdin_paths is not None:
201
+ errmsg = errmsg_creator.not_allowed_together("file_path", "stdin_paths")
202
+ raise GitExitingException(
203
+ errmsg, exit_code=ERR_INVALID_USAGE
204
+ ) from ValueError(errmsg)
205
+ if stdin is not None and stdin_paths is not None:
206
+ errmsg = errmsg_creator.not_allowed_together("stdin", "stdin_paths")
207
+ raise GitExitingException(
208
+ errmsg, exit_code=ERR_INVALID_USAGE
209
+ ) from ValueError(errmsg)
210
+ if path is not None and no_filters:
211
+ errmsg = errmsg_creator.not_allowed_together("path", "no_filters")
212
+ raise GitExitingException(
213
+ errmsg, exit_code=ERR_INVALID_USAGE
214
+ ) from ValueError(errmsg)
215
+ if path is not None and stdin_paths is not None:
216
+ errmsg = errmsg_creator.not_allowed_together("path", "stdin_paths")
217
+ raise GitExitingException(
218
+ errmsg, exit_code=ERR_INVALID_USAGE
219
+ ) from ValueError(errmsg)
220
+
221
+ def validate_types(
222
+ self,
223
+ file_path: Path | None,
224
+ *file_paths: Path,
225
+ t: Literal["commit", "tree", "blob", "tag"],
226
+ path: Path | None,
227
+ no_filters: bool,
228
+ literally: bool,
229
+ stdin: bytes | None,
230
+ stdin_paths: list[Path] | None,
231
+ w: bool,
232
+ ) -> None:
233
+ if file_path is not None:
234
+ self._require_path(file_path, "file_path")
235
+ for extra_path in file_paths:
236
+ self._require_path(extra_path, "file_path")
237
+ if t not in _HASH_OBJECT_TYPES:
238
+ errmsg = errmsg_creator.errmsg_for_choices(
239
+ emphasis="t", choices=list(_HASH_OBJECT_TYPES)
240
+ )
241
+ raise GitExitingException(
242
+ errmsg, exit_code=ERR_INVALID_USAGE
243
+ ) from ValueError(errmsg)
244
+ if path is not None:
245
+ self._require_path(path, "path")
246
+ require_type(no_filters, "no_filters", bool, GitExitingException)
247
+ require_type(literally, "literally", bool, GitExitingException)
248
+ require_type(w, "w", bool, GitExitingException)
249
+ if stdin is not None:
250
+ if not isinstance(stdin, (bytes, bytearray)):
251
+ errmsg = "'stdin' must be bytes"
252
+ raise GitExitingException(
253
+ errmsg, exit_code=ERR_DATA_FORMAT_ERR
254
+ ) from TypeError(errmsg)
255
+ if stdin_paths is not None:
256
+ require_iterable(stdin_paths, "stdin_paths", Path, list, GitExitingException)
257
+
258
+ def _require_path(self, value: Path, name: str) -> None:
259
+ if not isinstance(value, Path):
260
+ errmsg = f"'{name}' must be a pathlib.Path"
261
+ raise GitExitingException(
262
+ errmsg, exit_code=ERR_DATA_FORMAT_ERR
263
+ ) from TypeError(errmsg)
gitbolt/ls_tree.py ADDED
@@ -0,0 +1,155 @@
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+
4
+ """
5
+ Helper interfaces specific to ``git ls-tree`` subcommand.
6
+ """
7
+
8
+ from abc import abstractmethod
9
+ from typing import Protocol, Unpack, override, Literal
10
+
11
+ from vt.utils.errors.error_specs import ERR_INVALID_USAGE
12
+ from vt.utils.errors.error_specs.utils import require_type, require_iterable
13
+
14
+ from gitbolt.exceptions import GitExitingException
15
+ from gitbolt.models import GitLsTreeOpts
16
+
17
+
18
+ class LsTreeArgsValidator(Protocol):
19
+ """
20
+ The argument validator for ``git ls-tree`` subcommand.
21
+ """
22
+
23
+ @abstractmethod
24
+ def validate(self, tree_ish: str, **ls_tree_opts: Unpack[GitLsTreeOpts]) -> None:
25
+ """
26
+ Validate arguments passed to the ls-tree() method.
27
+
28
+ :param tree_ish: A tree-ish identifier (commit SHA, branch name, etc.).
29
+ :param ls_tree_opts: Keyword arguments mapping to supported options for ``git ls-tree``.
30
+ """
31
+ ...
32
+
33
+
34
+ class UtilLsTreeArgsValidator(LsTreeArgsValidator):
35
+ """
36
+ Independent utility function sort of interface to perform ``ls_tree()`` arguments validation.
37
+ """
38
+
39
+ @override
40
+ def validate(self, tree_ish: str, **ls_tree_opts: Unpack[GitLsTreeOpts]) -> None:
41
+ """
42
+ Validate the inputs provided to the ``git ls-tree`` command.
43
+
44
+ This utility ensures type safety and logical correctness of arguments
45
+ passed to the ``git ls-tree`` subcommand.
46
+
47
+ This includes:
48
+ * Enforcing that ``tree_ish`` is a string.
49
+ * Checking that each supported boolean flag (like ``d``, ``r``, ``z``, etc.)
50
+ is a valid boolean if provided.
51
+ * Validating that ``abbrev`` is an integer between 0 and 40 (inclusive).
52
+ * Ensuring ``format_`` is a string if specified.
53
+ * Checking that ``path`` is a list of strings if specified.
54
+
55
+ All validations will raise a ``GitExitingException`` with a specific
56
+ exit code depending on the nature of the failure:
57
+
58
+ * ``TypeError`` leads to ``ERR_DATA_FORMAT_ERR``.
59
+ * ``ValueError`` leads to ``ERR_INVALID_USAGE``.
60
+
61
+ See: `git ls-tree documentation <https://git-scm.com/docs/git-ls-tree>`_.
62
+
63
+ :param tree_ish: A valid Git tree-ish identifier (e.g., branch name, commit hash).
64
+ :param ls_tree_opts: Keyword arguments that map to valid ls-tree options.
65
+ :raises GitExitingException: When validation fails.
66
+
67
+ Examples::
68
+
69
+ >>> UtilLsTreeArgsValidator().validate("HEAD", d=True, abbrev=10)
70
+ >>> UtilLsTreeArgsValidator().validate("abc123", format_="%(objectname)", path=["src/", "README.md"])
71
+ >>> UtilLsTreeArgsValidator().validate("main", name_only=False, z=True)
72
+
73
+
74
+ Invalid Examples (will raise GitExitingException), printing these just for doctesting::
75
+
76
+ >>> UtilLsTreeArgsValidator().validate(42) # type: ignore[arg-type] # tree_ish expects str and int is provided
77
+ Traceback (most recent call last):
78
+ gitbolt.exceptions.GitExitingException: TypeError: 'tree_ish' must be a string
79
+
80
+ >>> UtilLsTreeArgsValidator().validate("HEAD", abbrev="abc") # type: ignore[arg-type] # abbrev expects int and str is provided
81
+ Traceback (most recent call last):
82
+ gitbolt.exceptions.GitExitingException: TypeError: 'abbrev' must be an int
83
+
84
+ >>> UtilLsTreeArgsValidator().validate("HEAD", abbrev=True) # type: ignore[arg-type] # abbrev expects int and bool is provided
85
+ Traceback (most recent call last):
86
+ gitbolt.exceptions.GitExitingException: TypeError: 'abbrev' must be an int
87
+
88
+ >>> UtilLsTreeArgsValidator().validate("HEAD", abbrev=100)
89
+ Traceback (most recent call last):
90
+ gitbolt.exceptions.GitExitingException: ValueError: abbrev must be between 0 and 40.
91
+
92
+ >>> UtilLsTreeArgsValidator().validate("HEAD",
93
+ ... path="src/") # type: ignore[arg-type] as path expects list[str] and str is provided.
94
+ Traceback (most recent call last):
95
+ gitbolt.exceptions.GitExitingException: TypeError: 'path' must be a non-str iterable
96
+
97
+ >>> UtilLsTreeArgsValidator().validate("HEAD",
98
+ ... path=1) # type: ignore[arg-type] as path expects list[str] and str is provided.
99
+ Traceback (most recent call last):
100
+ gitbolt.exceptions.GitExitingException: TypeError: 'path' must be a non-str iterable
101
+
102
+ >>> UtilLsTreeArgsValidator().validate("HEAD",
103
+ ... z="yes") # type: ignore[arg-type] as z expects bool and str is provided.
104
+ Traceback (most recent call last):
105
+ gitbolt.exceptions.GitExitingException: TypeError: 'z' must be a boolean
106
+ """
107
+ require_type(tree_ish, "tree_ish", str, GitExitingException)
108
+
109
+ bool_keys: list[
110
+ Literal[
111
+ "d",
112
+ "r",
113
+ "t",
114
+ "long",
115
+ "z",
116
+ "name_only",
117
+ "object_only",
118
+ "full_name",
119
+ "full_tree",
120
+ "name_status",
121
+ ]
122
+ ] = [
123
+ "d",
124
+ "r",
125
+ "t",
126
+ "long",
127
+ "z",
128
+ "name_only",
129
+ "object_only",
130
+ "full_name",
131
+ "full_tree",
132
+ "name_status",
133
+ ]
134
+
135
+ for key in bool_keys:
136
+ if key in ls_tree_opts:
137
+ the_key = ls_tree_opts[key]
138
+ require_type(the_key, key, bool, GitExitingException)
139
+
140
+ if "abbrev" in ls_tree_opts:
141
+ abbrev = ls_tree_opts["abbrev"]
142
+ require_type(abbrev, "abbrev", int, GitExitingException)
143
+ if not (0 <= abbrev <= 40):
144
+ errmsg = "abbrev must be between 0 and 40."
145
+ raise GitExitingException(
146
+ errmsg, exit_code=ERR_INVALID_USAGE
147
+ ) from ValueError(errmsg)
148
+
149
+ if "format_" in ls_tree_opts:
150
+ format_ = ls_tree_opts["format_"]
151
+ require_type(format_, "format_", str, GitExitingException)
152
+
153
+ if "path" in ls_tree_opts:
154
+ path = ls_tree_opts["path"]
155
+ require_iterable(path, "path", str, list, GitExitingException)