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.
@@ -0,0 +1,216 @@
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+
4
+ """
5
+ Utility functions related to processors specific to git commands using subprocess.
6
+ """
7
+
8
+ from collections.abc import Sequence
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+ from vt.utils.commons.commons.core_py import is_unset, not_none_not_unset, Unset
13
+
14
+
15
+ def git_main_cmd_repeating_flag_args(
16
+ val: Sequence[Path] | Unset | None, cmd_flag: str
17
+ ) -> list[str]:
18
+ """
19
+ Returns a flattened list of repeating flags and values.
20
+
21
+ Used for flags like `-C` which may be repeated with multiple values.
22
+
23
+ Args:
24
+ val: A list of values to apply with the flag.
25
+ cmd_flag: The flag to apply to each value.
26
+
27
+ Returns:
28
+ A flattened list like ['-C', 'repo1', '-C', 'repo2']
29
+
30
+ Examples:
31
+ >>> git_main_cmd_repeating_flag_args([Path("repo1"), Path("repo2")], "-C")
32
+ ['-C', 'repo1', '-C', 'repo2']
33
+
34
+ >>> git_main_cmd_repeating_flag_args([], "-C")
35
+ []
36
+
37
+ >>> from vt.utils.commons.commons.core_py import UNSET
38
+ >>> git_main_cmd_repeating_flag_args([Path("repo1"), UNSET, Path("repo2")], "-C")
39
+ ['-C', 'repo1', '-C', 'repo2']
40
+
41
+ >>> git_main_cmd_repeating_flag_args(None, "-C")
42
+ []
43
+
44
+ >>> git_main_cmd_repeating_flag_args(UNSET, "-C")
45
+ []
46
+ """
47
+ if not_none_not_unset(val):
48
+ return [
49
+ item
50
+ for entry in val
51
+ if not is_unset(entry)
52
+ for item in [cmd_flag, str(entry)]
53
+ ]
54
+ return []
55
+
56
+
57
+ def git_main_cmd_dict_flag_args(
58
+ val: dict[str, str | bool | None | Unset] | None | Unset, cmd_flag: str
59
+ ) -> list[str]:
60
+ """
61
+ Converts a dictionary into flag pairs used by commands like `-c key=value`.
62
+
63
+ - `True` or `None` => `-c key`
64
+ - `False` => `-c key=`
65
+ - `UNSET` keys are skipped
66
+
67
+ Args:
68
+ val: Dictionary of key-value pairs.
69
+ cmd_flag: The flag to apply.
70
+
71
+ Returns:
72
+ A list of CLI arguments.
73
+
74
+ Examples:
75
+ >>> git_main_cmd_dict_flag_args({"foo.bar": "baz"}, "-c")
76
+ ['-c', 'foo.bar=baz']
77
+
78
+ >>> git_main_cmd_dict_flag_args({"foo.bar": ""}, "-c")
79
+ ['-c', 'foo.bar=']
80
+
81
+ >>> git_main_cmd_dict_flag_args({"foo.bar": True}, "-c")
82
+ ['-c', 'foo.bar']
83
+
84
+ >>> git_main_cmd_dict_flag_args({"foo.bar": False}, "-c")
85
+ ['-c', 'foo.bar=']
86
+
87
+ >>> git_main_cmd_dict_flag_args({"foo.bar": None}, "-c")
88
+ ['-c', 'foo.bar']
89
+
90
+ >>> from vt.utils.commons.commons.core_py import UNSET
91
+ >>> git_main_cmd_dict_flag_args({"foo.bar": UNSET}, "-c")
92
+ []
93
+
94
+ >>> git_main_cmd_dict_flag_args({
95
+ ... "a.b": "x", "c.d": "", "e.f": True, "g.h": False, "i.j": None
96
+ ... }, "-c")
97
+ ['-c', 'a.b=x', '-c', 'c.d=', '-c', 'e.f', '-c', 'g.h=', '-c', 'i.j']
98
+
99
+ >>> git_main_cmd_dict_flag_args(None, "-c")
100
+ []
101
+
102
+ >>> git_main_cmd_dict_flag_args(UNSET, "-c")
103
+ []
104
+
105
+ >>> git_main_cmd_dict_flag_args({}, "-c")
106
+ []
107
+ """
108
+ if not_none_not_unset(val):
109
+ args = []
110
+ for k, v in val.items():
111
+ if is_unset(v):
112
+ continue
113
+ if v is True or v is None:
114
+ args += [cmd_flag, k]
115
+ elif v is False:
116
+ args += [cmd_flag, f"{k}="]
117
+ else:
118
+ args += [cmd_flag, f"{k}={v}"]
119
+ return args
120
+ return []
121
+
122
+
123
+ def git_main_cmd_simple_flag_args(val: bool | Unset | None, cmd_flag: str) -> list[str]:
124
+ """
125
+ Returns a single flag if the value is set and truthy.
126
+
127
+ Args:
128
+ val: A boolean-like value.
129
+ cmd_flag: The flag to apply.
130
+
131
+ Returns:
132
+ A list with just the flag, or empty.
133
+
134
+ Examples:
135
+ >>> git_main_cmd_simple_flag_args(True, "--no-pager")
136
+ ['--no-pager']
137
+
138
+ >>> git_main_cmd_simple_flag_args(False, "--no-pager")
139
+ []
140
+
141
+ >>> git_main_cmd_simple_flag_args(None, "--no-pager")
142
+ []
143
+
144
+ >>> from vt.utils.commons.commons.core_py import UNSET
145
+ >>> git_main_cmd_simple_flag_args(UNSET, "--no-pager")
146
+ []
147
+ """
148
+ if not_none_not_unset(val):
149
+ return [cmd_flag] if val else []
150
+ else:
151
+ return []
152
+
153
+
154
+ def git_main_cmd_pair_flag_args(val: Any, cmd_flag: str) -> list[str]:
155
+ """
156
+ Returns a flag and value pair, or nothing if not set.
157
+
158
+ Args:
159
+ val: The value to attach to the flag.
160
+ cmd_flag: The flag name.
161
+
162
+ Returns:
163
+ A list like ['--exec-path', 'tmp'] or []
164
+
165
+ Examples:
166
+ >>> git_main_cmd_pair_flag_args("foo", "--namespace")
167
+ ['--namespace', 'foo']
168
+
169
+ >>> git_main_cmd_pair_flag_args(Path("repo"), "--namespace")
170
+ ['--namespace', 'repo']
171
+
172
+ >>> git_main_cmd_pair_flag_args(None, "--namespace")
173
+ []
174
+
175
+ >>> from vt.utils.commons.commons.core_py import UNSET
176
+ >>> git_main_cmd_pair_flag_args(UNSET, "--namespace")
177
+ []
178
+ """
179
+ return [cmd_flag, str(val)] if not_none_not_unset(val) else []
180
+
181
+
182
+ def git_main_opts_clean_cap_c(main_opts: list[str]) -> list[str]:
183
+ """
184
+ Clean the cap C of -C CLI options from the CLI command list. This function removes the ``-C`` CLI option and its
185
+ values from the supplied param list.
186
+
187
+ >>> assert git_main_opts_clean_cap_c([]) == []
188
+
189
+ >>> assert git_main_opts_clean_cap_c(["--no-pager"]) == ["--no-pager"]
190
+
191
+ >>> assert git_main_opts_clean_cap_c(["--no-pager", "-C", "/root", "-C", "dir", "-C", "file"]) == ["--no-pager"]
192
+
193
+ >>> assert git_main_opts_clean_cap_c(["-C", "/root", "-C", "dir", "-C", "file", "--no-pager"]) == ["--no-pager"]
194
+
195
+ >>> assert git_main_opts_clean_cap_c(["-C", "/root", "-C", "dir", "--no-pager", "-C", "file"]) == ["--no-pager"]
196
+
197
+ >>> assert git_main_opts_clean_cap_c(["-C", "/root", "-C", "dir", "-C", "file"]) == []
198
+
199
+ :param main_opts: git CLI command options.
200
+ :returns: command line options after removing -C options and its values.
201
+ """
202
+ cap_c_indices: list[int] = []
203
+ main_opts_copy = main_opts.copy()
204
+ try:
205
+ i=0
206
+ while i<len(main_opts):
207
+ found_index = main_opts_copy.index("-C", i)
208
+ cap_c_indices.append(found_index)
209
+ i=found_index+1
210
+ except ValueError:
211
+ # didn't find -C or done finding -C
212
+ pass
213
+ for cap_c_index in reversed(cap_c_indices):
214
+ del main_opts_copy[cap_c_index+1]
215
+ del main_opts_copy[cap_c_index]
216
+ return main_opts_copy
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+
4
+ """
5
+ Models useful for session utilities.
6
+ """
7
+ import dataclasses
8
+ import datetime
9
+
10
+
11
+ @dataclasses.dataclass
12
+ class GitRawActor:
13
+ name: bytes
14
+ email: bytes
15
+ time: datetime.datetime
16
+
17
+ @classmethod
18
+ def from_commit_bytes(cls, commit_bytes: bytes, email_start_pattern: bytes = b" <",
19
+ email_end_pattern: bytes = b"> ") -> "GitRawActor":
20
+ """
21
+ Obtain ``Actor`` object from the author/committer bytes.
22
+
23
+ Examples:
24
+
25
+ Simple timestamp without timezone:
26
+
27
+ >>> _ss_actor1 = GitRawActor.from_commit_bytes(b"Suhas <sss@ss.ss> 1780211249")
28
+ >>> assert _ss_actor1.name == b"Suhas"
29
+ >>> assert _ss_actor1.email == b"sss@ss.ss"
30
+ >>> assert _ss_actor1.time.timestamp() == 1780211249.0
31
+
32
+ Simple timestamp with timezone:
33
+
34
+ >>> _ss_actor2 = GitRawActor.from_commit_bytes(b"Suhas Srivastava <sss@vaastav.tech> 1780211249 +0530")
35
+ >>> assert _ss_actor2.name == b"Suhas Srivastava"
36
+ >>> assert _ss_actor2.email == b"sss@vaastav.tech"
37
+
38
+ # failing timezone test >>> assert _ss_actor2.time.timestamp() == 1780211249.0
39
+
40
+ :param commit_bytes: author/committer information in bytes form.
41
+ :param email_start_pattern: email spearates name and timestamp. Thus is required as a separator.
42
+ Marks email beginning.
43
+ :param email_end_pattern: email spearates name and timestamp. Thus is required as a separator.
44
+ Marks email ending.
45
+ :returns: ``Actor`` object with all the parsed and set values.
46
+ """
47
+ email_pattern_start_index = commit_bytes.find(email_start_pattern)
48
+ email_pattern_end_index = commit_bytes.rfind(email_end_pattern)
49
+ email_start_index = email_pattern_start_index+len(email_start_pattern)
50
+ email_end_index = email_pattern_end_index
51
+ name = commit_bytes[:email_pattern_start_index]
52
+ email = commit_bytes[email_start_index:email_end_index]
53
+ # TODO: handle timezone correctly
54
+ time_bytes = commit_bytes[email_end_index+len(email_end_pattern):]
55
+ time_main, time_zone = time_bytes.split() if b" " in time_bytes else (time_bytes, b"")
56
+ date_time_for_iso_str = datetime.datetime.fromtimestamp(int(time_main))
57
+ date_time = datetime.datetime.fromisoformat(f"{date_time_for_iso_str.date()}T{date_time_for_iso_str.time()}{time_zone.decode()}")
58
+ return GitRawActor(name, email, date_time)
59
+
60
+
61
+ @dataclasses.dataclass
62
+ class GitRawSignature:
63
+ signature: bytes
64
+
65
+
66
+ @dataclasses.dataclass
67
+ class GPGGitRawSignature(GitRawSignature):
68
+ pass
69
+
70
+
71
+ @dataclasses.dataclass
72
+ class SSHGitRawSignature(GitRawSignature):
73
+ pass
74
+
75
+
76
+ @dataclasses.dataclass
77
+ class RawCommitBytesObj:
78
+ """
79
+ Commit info all in bytes as read from ``git cat-file --batch``.
80
+ """
81
+ commit_hash: bytes
82
+ tree_hash: bytes
83
+ parents: list[bytes]
84
+ author: GitRawActor
85
+ committer: GitRawActor
86
+ signature: GitRawSignature | None
87
+ message: bytes
88
+
89
+
90
+ @dataclasses.dataclass
91
+ class RawBytesValsOfCommit:
92
+ tree_val: bytes
93
+ commit_parents_vals: list[bytes]
94
+ author_val: bytes
95
+ committer_val: bytes
96
+ signature_val: bytes | None
97
+ commit_message: bytes