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/__init__.py +97 -0
- gitbolt/_internal_init.py +18 -0
- gitbolt/add.py +652 -0
- gitbolt/base.py +983 -0
- gitbolt/constants.py +12 -0
- gitbolt/exceptions.py +46 -0
- gitbolt/hash_object.py +263 -0
- gitbolt/ls_tree.py +155 -0
- gitbolt/models.py +698 -0
- gitbolt/py.typed +0 -0
- gitbolt/pytest_plugin.py +41 -0
- gitbolt/subprocess/__init__.py +18 -0
- gitbolt/subprocess/_internal_init.py +9 -0
- gitbolt/subprocess/add.py +484 -0
- gitbolt/subprocess/base.py +1835 -0
- gitbolt/subprocess/constants.py +23 -0
- gitbolt/subprocess/exceptions.py +110 -0
- gitbolt/subprocess/hash_object.py +227 -0
- gitbolt/subprocess/hash_object_util.py +90 -0
- gitbolt/subprocess/impl/__init__.py +6 -0
- gitbolt/subprocess/impl/simple.py +613 -0
- gitbolt/subprocess/ls_tree.py +406 -0
- gitbolt/subprocess/runner/__init__.py +8 -0
- gitbolt/subprocess/runner/base.py +106 -0
- gitbolt/subprocess/runner/simple.py +150 -0
- gitbolt/subprocess/utils/__init__.py +6 -0
- gitbolt/subprocess/utils/main_cmd.py +216 -0
- gitbolt/subprocess/utils/models.py +97 -0
- gitbolt/subprocess/utils/session.py +610 -0
- gitbolt/subprocess/worktree.py +266 -0
- gitbolt/utils.py +215 -0
- gitbolt-0.0.0.dist-info/METADATA +464 -0
- gitbolt-0.0.0.dist-info/RECORD +37 -0
- gitbolt-0.0.0.dist-info/WHEEL +5 -0
- gitbolt-0.0.0.dist-info/entry_points.txt +2 -0
- gitbolt-0.0.0.dist-info/licenses/LICENSE +201 -0
- gitbolt-0.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# coding=utf-8
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Helper interfaces for ``git worktree`` subcommand with default implementation for subprocess calls.
|
|
6
|
+
"""
|
|
7
|
+
import abc
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Literal, Iterable
|
|
11
|
+
|
|
12
|
+
from vt.utils.commons.commons.core_py import Unset, UNSET
|
|
13
|
+
|
|
14
|
+
from gitbolt.subprocess.constants import WORKTREE_CMD, WORKTREE_LIST_CMD, WORKTREE_ADD_CMD, \
|
|
15
|
+
WORKTREE_MOVE_CMD, WORKTREE_REMOVE_CMD, WORKTREE_LOCK_CMD
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def build_non_none_list(*vals: str | Unset | Iterable[str] | None) -> list[str]:
|
|
19
|
+
"""
|
|
20
|
+
Build a list of strings from supplied params ``vals``.
|
|
21
|
+
|
|
22
|
+
Examples:
|
|
23
|
+
|
|
24
|
+
>>> assert len(build_non_none_list()) == 0
|
|
25
|
+
|
|
26
|
+
>>> assert len(build_non_none_list(UNSET)) == 0
|
|
27
|
+
|
|
28
|
+
>>> assert len(build_non_none_list(UNSET, UNSET)) == 0
|
|
29
|
+
|
|
30
|
+
>>> assert build_non_none_list(UNSET, ["-b", "-c"]) == ["-b", "-c"]
|
|
31
|
+
|
|
32
|
+
>>> assert build_non_none_list(UNSET, ["-b", "-c"], UNSET) == ["-b", "-c"]
|
|
33
|
+
|
|
34
|
+
>>> assert build_non_none_list(UNSET, ["-b", "-c"], UNSET, "--detach", "--my-opt") == ["-b", "-c", "--detach", "--my-opt"]
|
|
35
|
+
|
|
36
|
+
>>> assert build_non_none_list([]) == []
|
|
37
|
+
|
|
38
|
+
:param vals: values to include in the options building list.
|
|
39
|
+
:returns: list of strings representing cli options.
|
|
40
|
+
"""
|
|
41
|
+
ret_list: list[str] = []
|
|
42
|
+
for val in vals:
|
|
43
|
+
if val is not None:
|
|
44
|
+
if not isinstance(val, Unset):
|
|
45
|
+
if not isinstance(val, str) and isinstance(val, Iterable):
|
|
46
|
+
ret_list.extend(val)
|
|
47
|
+
else:
|
|
48
|
+
ret_list.append(val)
|
|
49
|
+
return ret_list
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def handle_bool_opt(arg: bool | Unset, opt_str: str) -> str | Unset:
|
|
53
|
+
"""
|
|
54
|
+
Examples:
|
|
55
|
+
|
|
56
|
+
>>> assert handle_bool_opt(UNSET, "") == UNSET
|
|
57
|
+
|
|
58
|
+
>>> assert handle_bool_opt(True, "my-opt") == "--my-opt"
|
|
59
|
+
|
|
60
|
+
>>> assert handle_bool_opt(False, "my-opt") == "--no-my-opt"
|
|
61
|
+
|
|
62
|
+
>>> handle_bool_opt("garbage", "my-opt") # type: ignore[arg-type]
|
|
63
|
+
Traceback (most recent call last):
|
|
64
|
+
ValueError: my-opt must either be a bool or remain Unset.
|
|
65
|
+
|
|
66
|
+
"""
|
|
67
|
+
if arg is True:
|
|
68
|
+
return f"--{opt_str}"
|
|
69
|
+
elif arg is False:
|
|
70
|
+
return f"--no-{opt_str}"
|
|
71
|
+
elif arg == UNSET:
|
|
72
|
+
return arg
|
|
73
|
+
else:
|
|
74
|
+
raise ValueError(f"{opt_str} must either be a bool or remain Unset.")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def handle_true_opt(arg: Literal[True] | Unset, opt_str: str, single_dash: bool = False) -> str | Unset:
|
|
78
|
+
"""
|
|
79
|
+
Examples:
|
|
80
|
+
|
|
81
|
+
>>> assert handle_true_opt(UNSET, "") == UNSET
|
|
82
|
+
|
|
83
|
+
>>> assert handle_true_opt(UNSET, "my-opt") == UNSET
|
|
84
|
+
|
|
85
|
+
>>> assert handle_true_opt(True, "my-opt") == "--my-opt"
|
|
86
|
+
|
|
87
|
+
>>> assert handle_true_opt(True, "a", True) == "-a"
|
|
88
|
+
|
|
89
|
+
>>> assert handle_true_opt(UNSET, "a", True) == UNSET
|
|
90
|
+
|
|
91
|
+
>>> handle_true_opt(False, "my-opt") # type: ignore[arg-type]
|
|
92
|
+
Traceback (most recent call last):
|
|
93
|
+
ValueError: my-opt must either be true or remain Unset.
|
|
94
|
+
|
|
95
|
+
>>> handle_true_opt("garbage", "my-opt") # type: ignore[arg-type]
|
|
96
|
+
Traceback (most recent call last):
|
|
97
|
+
ValueError: my-opt must either be true or remain Unset.
|
|
98
|
+
|
|
99
|
+
"""
|
|
100
|
+
if arg is True:
|
|
101
|
+
if single_dash:
|
|
102
|
+
return f"-{opt_str}"
|
|
103
|
+
return f"--{opt_str}"
|
|
104
|
+
elif isinstance(arg, Unset):
|
|
105
|
+
return arg
|
|
106
|
+
else:
|
|
107
|
+
raise ValueError(f"{opt_str} must either be true or remain Unset.")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def handle_str_bool_opt(arg: bool | Unset | str, opt_str: str) -> list[str] | Unset:
|
|
111
|
+
if arg is False:
|
|
112
|
+
return [f"--no-{opt_str}"]
|
|
113
|
+
elif arg == UNSET:
|
|
114
|
+
return arg
|
|
115
|
+
elif isinstance(arg, str):
|
|
116
|
+
return [f"--{opt_str}", arg]
|
|
117
|
+
else:
|
|
118
|
+
raise ValueError(f"{opt_str} must either have a value, be False or remain Unset.")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class WorktreeCLIArgsBuilder(abc.ABC):
|
|
122
|
+
def build_list_cli_args(self, *, verbose: bool | Unset, porcelain: Literal[True, False] | Unset,
|
|
123
|
+
z: Literal[True] | Unset) -> list[str]:
|
|
124
|
+
return [WORKTREE_CMD, WORKTREE_LIST_CMD,
|
|
125
|
+
*build_non_none_list(self._handle_verbose_option(verbose),
|
|
126
|
+
self._handle_porcelain_option(porcelain),
|
|
127
|
+
self._handle_z_option(z))]
|
|
128
|
+
|
|
129
|
+
def build_lock_cli_args(self, worktree: Path, reason: str | Literal[False] | Unset) -> list[str]:
|
|
130
|
+
return [WORKTREE_CMD, WORKTREE_LOCK_CMD, str(worktree),
|
|
131
|
+
*build_non_none_list(self._handle_reason_option(reason))]
|
|
132
|
+
|
|
133
|
+
def build_unlock_cli_args(self, worktree) -> list[str]:
|
|
134
|
+
return [WORKTREE_CMD, WORKTREE_LOCK_CMD, str(worktree)]
|
|
135
|
+
|
|
136
|
+
def build_move_cli_args(self, worktree: Path, new_path: Path, *, force: bool | Unset,
|
|
137
|
+
reforce: bool | Unset, relative_paths: bool | Unset) -> list[str]:
|
|
138
|
+
return [WORKTREE_CMD, WORKTREE_MOVE_CMD, str(worktree), str(new_path),
|
|
139
|
+
*build_non_none_list(
|
|
140
|
+
self._handle_force_option(force),
|
|
141
|
+
self._handle_reforce_option(reforce),
|
|
142
|
+
self._handle_relative_paths_option(relative_paths),
|
|
143
|
+
)]
|
|
144
|
+
|
|
145
|
+
def build_prune_cli_args(self, dry_run: bool | Unset, verbose: bool | Unset,
|
|
146
|
+
expire: Literal[False] | int | datetime | Unset) -> list[str]:
|
|
147
|
+
return [WORKTREE_CMD, WORKTREE_MOVE_CMD,
|
|
148
|
+
*build_non_none_list(
|
|
149
|
+
self._handle_dry_run(dry_run),
|
|
150
|
+
self._handle_verbose_option(verbose),
|
|
151
|
+
self._handle_expire_option(expire))]
|
|
152
|
+
|
|
153
|
+
def build_remove_cli_args(self, worktree: Path, *, force: bool | Unset, reforce: bool | Unset) -> list[str]:
|
|
154
|
+
return [WORKTREE_CMD, WORKTREE_REMOVE_CMD, str(worktree),
|
|
155
|
+
*build_non_none_list(
|
|
156
|
+
self._handle_force_option(force),
|
|
157
|
+
self._handle_reforce_option(reforce)
|
|
158
|
+
)]
|
|
159
|
+
|
|
160
|
+
def build_repair_cli_args(self, *worktrees: Path, relative_paths: bool | Unset) -> list[str]:
|
|
161
|
+
return [WORKTREE_CMD, WORKTREE_REMOVE_CMD, *[str(worktree) for worktree in worktrees],
|
|
162
|
+
*build_non_none_list(
|
|
163
|
+
self._handle_relative_paths_option(relative_paths)
|
|
164
|
+
)]
|
|
165
|
+
|
|
166
|
+
def build_add_cli_args(self, worktree: Path, commit_ish: str | None, *, force: bool | Unset,
|
|
167
|
+
reforce: bool | Unset, new_branch: str | Unset, new_branch_force: str | Unset,
|
|
168
|
+
orphan: Literal[True] | Unset, detach: Literal[True] | Unset,
|
|
169
|
+
checkout: bool | Unset, lock: bool | Unset,
|
|
170
|
+
reason: str | Literal[False] | Unset, quiet: bool | Unset,
|
|
171
|
+
track: bool | Unset, guess_remote: bool | Unset,
|
|
172
|
+
relative_paths: bool | Unset) -> list[str]:
|
|
173
|
+
cmd_list = [WORKTREE_CMD, WORKTREE_ADD_CMD, str(worktree), ]
|
|
174
|
+
if commit_ish is not None:
|
|
175
|
+
cmd_list.append(commit_ish)
|
|
176
|
+
cmd_list.extend(
|
|
177
|
+
build_non_none_list(
|
|
178
|
+
self._handle_force_option(force),
|
|
179
|
+
self._handle_reforce_option(reforce),
|
|
180
|
+
self._handle_new_branch_option(new_branch, commit_ish),
|
|
181
|
+
self._handle_new_branch_force_option(new_branch_force, commit_ish),
|
|
182
|
+
self._handle_orphan_option(orphan),
|
|
183
|
+
self._handle_detach_option(detach),
|
|
184
|
+
self._handle_checkout_option(checkout),
|
|
185
|
+
self._handle_lock_option(lock),
|
|
186
|
+
self._handle_reason_option(reason),
|
|
187
|
+
self._handle_quiet_option(quiet),
|
|
188
|
+
self._handle_track_option(track),
|
|
189
|
+
self._handle_guess_remote_option(guess_remote),
|
|
190
|
+
self._handle_relative_paths_option(relative_paths),
|
|
191
|
+
))
|
|
192
|
+
return cmd_list
|
|
193
|
+
|
|
194
|
+
def _handle_verbose_option(self, verbose: bool | Unset) -> str | Unset:
|
|
195
|
+
return handle_bool_opt(verbose, "verbose")
|
|
196
|
+
|
|
197
|
+
def _handle_porcelain_option(self, porcelain: Literal[True, False] | Unset) -> str | Unset:
|
|
198
|
+
return handle_bool_opt(porcelain, "porcelain")
|
|
199
|
+
|
|
200
|
+
def _handle_z_option(self, z: Literal[True] | Unset) -> str | Unset:
|
|
201
|
+
return handle_true_opt(z, "z", True)
|
|
202
|
+
|
|
203
|
+
def _handle_reason_option(self, reason: str | Literal[False] | Unset) -> list[str] | Unset:
|
|
204
|
+
return handle_str_bool_opt(reason, "reason")
|
|
205
|
+
|
|
206
|
+
def _handle_force_option(self, force: bool | Unset) -> str | Unset:
|
|
207
|
+
return handle_bool_opt(force, "force")
|
|
208
|
+
|
|
209
|
+
def _handle_reforce_option(self, reforce: bool | Unset) -> str | Unset:
|
|
210
|
+
return self._handle_force_option(reforce)
|
|
211
|
+
|
|
212
|
+
def _handle_relative_paths_option(self, relative_paths: bool | Unset) -> str | Unset:
|
|
213
|
+
return handle_bool_opt(relative_paths, "relative-paths")
|
|
214
|
+
|
|
215
|
+
def _handle_dry_run(self, dry_run: bool | Unset) -> str | Unset:
|
|
216
|
+
return handle_bool_opt(dry_run, "dry-run")
|
|
217
|
+
|
|
218
|
+
def _handle_expire_option(self, expire: Literal[False] | int | datetime | Unset) -> list[str]:
|
|
219
|
+
expire_str = "expire"
|
|
220
|
+
if expire is False:
|
|
221
|
+
return [f"--no-{expire_str}"]
|
|
222
|
+
elif isinstance(expire, datetime):
|
|
223
|
+
return [f"--{expire_str}", expire.strftime("%d/%m/%Y, %H:%M:%S")]
|
|
224
|
+
elif isinstance(expire, int):
|
|
225
|
+
return [f"--{expire_str}", str(expire)]
|
|
226
|
+
else:
|
|
227
|
+
raise ValueError(f"{expire} must either have a datetime value, int value, be False or remain Unset.")
|
|
228
|
+
|
|
229
|
+
def _handle_new_branch_option(self, new_branch: str | Unset, commit_ish: str | None) -> list[str] | Unset:
|
|
230
|
+
return self._new_branch_option_helper(new_branch, commit_ish, "-b")
|
|
231
|
+
|
|
232
|
+
def _handle_new_branch_force_option(self, new_branch_force: str | Unset,
|
|
233
|
+
commit_ish: str | None) -> list[str] | Unset:
|
|
234
|
+
return self._new_branch_option_helper(new_branch_force, commit_ish, "-B")
|
|
235
|
+
|
|
236
|
+
def _handle_orphan_option(self, orphan: Literal[True] | Unset) -> str | Unset:
|
|
237
|
+
return handle_true_opt(orphan, "orphan")
|
|
238
|
+
|
|
239
|
+
def _handle_detach_option(self, detach: Literal[True] | Unset) -> str | Unset:
|
|
240
|
+
return handle_true_opt(detach, "detach")
|
|
241
|
+
|
|
242
|
+
def _handle_checkout_option(self, checkout: bool | Unset) -> str | Unset:
|
|
243
|
+
return handle_bool_opt(checkout, "checkout")
|
|
244
|
+
|
|
245
|
+
def _handle_lock_option(self, lock: bool | Unset) -> str | Unset:
|
|
246
|
+
return handle_bool_opt(lock, "lock")
|
|
247
|
+
|
|
248
|
+
def _handle_quiet_option(self, quiet: bool | Unset) -> str | Unset:
|
|
249
|
+
return handle_bool_opt(quiet, "quiet")
|
|
250
|
+
|
|
251
|
+
def _handle_track_option(self, track: bool | Unset) -> str | Unset:
|
|
252
|
+
return handle_bool_opt(track, "track")
|
|
253
|
+
|
|
254
|
+
def _handle_guess_remote_option(self, guess_remote: bool | Unset) -> str | Unset:
|
|
255
|
+
return handle_bool_opt(guess_remote, "guess-remote")
|
|
256
|
+
|
|
257
|
+
def _new_branch_option_helper(self, new_branch: str | Unset, commit_ish: str | None,
|
|
258
|
+
opt_str: str) -> list[str] | Unset:
|
|
259
|
+
if new_branch==UNSET:
|
|
260
|
+
return new_branch
|
|
261
|
+
elif isinstance(new_branch, str):
|
|
262
|
+
if commit_ish is not None:
|
|
263
|
+
return [opt_str, commit_ish]
|
|
264
|
+
return [opt_str]
|
|
265
|
+
else:
|
|
266
|
+
raise ValueError(f"{opt_str} must either have a branch-name value or remain Unset.")
|
gitbolt/utils.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# coding=utf-8
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Utility functions related to processors specific to git commands.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from vt.utils.commons.commons.core_py import is_unset
|
|
11
|
+
|
|
12
|
+
from gitbolt.models import GitOpts, GitEnvVars
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def merge_git_opts(primary: GitOpts, fallback: GitOpts) -> GitOpts:
|
|
16
|
+
"""
|
|
17
|
+
Merge the ``primary`` and ``fallback`` ``GitOpts`` object and return a new ``GitOpts`` object.
|
|
18
|
+
|
|
19
|
+
Construction of the new ``GitOpts`` object is done such that:
|
|
20
|
+
|
|
21
|
+
* first prioritise picking up a property from the ``primary``.
|
|
22
|
+
* if a property is ``None`` in the ``primary`` then that corresponding property is picked from the ``fallback``.
|
|
23
|
+
|
|
24
|
+
Example usage:
|
|
25
|
+
|
|
26
|
+
>>> from pathlib import Path
|
|
27
|
+
>>> from typing import cast
|
|
28
|
+
>>> _primary = cast(GitOpts, {
|
|
29
|
+
... "C": [Path("/repo")],
|
|
30
|
+
... "c": {"user.name": "Alice", "color.ui": None},
|
|
31
|
+
... "paginate": True,
|
|
32
|
+
... "no_pager": None,
|
|
33
|
+
... "exec_path": None,
|
|
34
|
+
... "git_dir": Path("/repo/.git"),
|
|
35
|
+
... "work_tree": None,
|
|
36
|
+
... "bare": None,
|
|
37
|
+
... "namespace": None
|
|
38
|
+
... })
|
|
39
|
+
>>> _fallback = cast(GitOpts, {
|
|
40
|
+
... "C": [Path("/fallback")],
|
|
41
|
+
... "c": {"user.name": "Bob", "core.editor": "vim"},
|
|
42
|
+
... "paginate": False,
|
|
43
|
+
... "no_pager": True,
|
|
44
|
+
... "exec_path": Path("/usr/lib/git-core"),
|
|
45
|
+
... "git_dir": None,
|
|
46
|
+
... "work_tree": Path("/fallback"),
|
|
47
|
+
... "bare": True,
|
|
48
|
+
... "namespace": "fallback-ns"
|
|
49
|
+
... })
|
|
50
|
+
>>> _merged = merge_git_opts(_primary, _fallback)
|
|
51
|
+
>>> _merged["C"] == [Path("/repo")]
|
|
52
|
+
True
|
|
53
|
+
>>> _merged["c"]
|
|
54
|
+
{'user.name': 'Alice', 'color.ui': None}
|
|
55
|
+
>>> _merged["paginate"]
|
|
56
|
+
True
|
|
57
|
+
>>> _merged["no_pager"]
|
|
58
|
+
True
|
|
59
|
+
>>> _merged["exec_path"] == Path("/usr/lib/git-core")
|
|
60
|
+
True
|
|
61
|
+
>>> _merged["git_dir"] == Path("/repo/.git")
|
|
62
|
+
True
|
|
63
|
+
>>> _merged["work_tree"] == Path("/fallback")
|
|
64
|
+
True
|
|
65
|
+
>>> _merged["bare"]
|
|
66
|
+
True
|
|
67
|
+
>>> _merged["namespace"]
|
|
68
|
+
'fallback-ns'
|
|
69
|
+
|
|
70
|
+
Empty example:
|
|
71
|
+
|
|
72
|
+
>>> merge_git_opts({}, {})
|
|
73
|
+
{}
|
|
74
|
+
|
|
75
|
+
Partial fallback behavior:
|
|
76
|
+
|
|
77
|
+
>>> merge_git_opts({"paginate": None}, {"paginate": True})["paginate"]
|
|
78
|
+
True
|
|
79
|
+
>>> 'paginate' in merge_git_opts({"paginate": None}, {"paginate": None})
|
|
80
|
+
False
|
|
81
|
+
>>> merge_git_opts({"paginate": False}, {"paginate": True})["paginate"]
|
|
82
|
+
False
|
|
83
|
+
|
|
84
|
+
:param primary: The first priority ``GitOpts`` object.
|
|
85
|
+
:param fallback: the second priority or fallback ``GitOpts`` object.
|
|
86
|
+
:return: A new ``GitOpts`` object that contains all the properties from the ``primary`` ``GitOpts`` object and
|
|
87
|
+
fallbacks on the corresponding property from the ``fallback`` ``GitOpts`` object if that corresponding property
|
|
88
|
+
is ``None`` in the ``primary`` ``GitOpts`` object.
|
|
89
|
+
"""
|
|
90
|
+
return merge_typed_dicts(primary, fallback, GitOpts, omit_unset_keys=True)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def merge_git_envs(primary: GitEnvVars, fallback: GitEnvVars) -> GitEnvVars:
|
|
94
|
+
"""
|
|
95
|
+
Merge the ``primary`` and ``fallback`` ``GitEnvVars`` objects and return a new ``GitEnvVars`` object.
|
|
96
|
+
|
|
97
|
+
Construction of the new ``GitEnvVars`` object is done such that:
|
|
98
|
+
|
|
99
|
+
* values from ``primary`` are prioritized.
|
|
100
|
+
* if a key exists in both, and the value in ``primary`` is explicitly ``None``, then the value from ``fallback`` is used.
|
|
101
|
+
* if a value in ``primary`` is ``Unset``, it is retained and does not fall back.
|
|
102
|
+
* any keys missing from ``primary`` but present in ``fallback`` are included.
|
|
103
|
+
|
|
104
|
+
Example usage:
|
|
105
|
+
|
|
106
|
+
>>> from pathlib import Path
|
|
107
|
+
>>> from datetime import datetime
|
|
108
|
+
>>> from typing import cast
|
|
109
|
+
>>> _primary = cast(GitEnvVars, {
|
|
110
|
+
... "GIT_AUTHOR_NAME": "Alice",
|
|
111
|
+
... "GIT_AUTHOR_EMAIL": "alice@example.com",
|
|
112
|
+
... "GIT_COMMITTER_DATE": None,
|
|
113
|
+
... "GIT_EDITOR": None,
|
|
114
|
+
... "GIT_TRACE": 2,
|
|
115
|
+
... "GIT_SSH": Path("/usr/bin/ssh"),
|
|
116
|
+
... })
|
|
117
|
+
>>> _fallback = cast(GitEnvVars, {
|
|
118
|
+
... "GIT_AUTHOR_NAME": "Bob",
|
|
119
|
+
... "GIT_AUTHOR_EMAIL": "bob@example.com",
|
|
120
|
+
... "GIT_COMMITTER_DATE": datetime(2020, 1, 1),
|
|
121
|
+
... "GIT_EDITOR": "vim",
|
|
122
|
+
... "GIT_TRACE": Path("/tmp/trace.log"),
|
|
123
|
+
... "GIT_SSH": None,
|
|
124
|
+
... "GIT_CONFIG_GLOBAL": Path("/home/bob/.gitconfig"),
|
|
125
|
+
... "GIT_REDACT_COOKIES": "sessionid"
|
|
126
|
+
... })
|
|
127
|
+
>>> _merged = merge_git_envs(_primary, _fallback)
|
|
128
|
+
>>> _merged["GIT_AUTHOR_NAME"]
|
|
129
|
+
'Alice'
|
|
130
|
+
>>> _merged["GIT_AUTHOR_EMAIL"]
|
|
131
|
+
'alice@example.com'
|
|
132
|
+
>>> _merged["GIT_COMMITTER_DATE"]
|
|
133
|
+
datetime.datetime(2020, 1, 1, 0, 0)
|
|
134
|
+
>>> _merged["GIT_EDITOR"]
|
|
135
|
+
'vim'
|
|
136
|
+
>>> _merged["GIT_TRACE"]
|
|
137
|
+
2
|
|
138
|
+
>>> _merged["GIT_SSH"]==Path('/usr/bin/ssh')
|
|
139
|
+
True
|
|
140
|
+
>>> _merged["GIT_CONFIG_GLOBAL"]==Path('/home/bob/.gitconfig')
|
|
141
|
+
True
|
|
142
|
+
|
|
143
|
+
Empty example:
|
|
144
|
+
|
|
145
|
+
>>> empty = merge_git_envs({}, {})
|
|
146
|
+
>>> isinstance(empty, dict)
|
|
147
|
+
True
|
|
148
|
+
|
|
149
|
+
Partial fallback behavior:
|
|
150
|
+
|
|
151
|
+
>>> merge_git_envs({"GIT_EDITOR": None}, {"GIT_EDITOR": "nano"})["GIT_EDITOR"]
|
|
152
|
+
'nano'
|
|
153
|
+
>>> 'GIT_EDITOR' in merge_git_envs({"GIT_EDITOR": None}, {"GIT_EDITOR": None})
|
|
154
|
+
False
|
|
155
|
+
>>> merge_git_envs({"GIT_EDITOR": "code"}, {"GIT_EDITOR": "nano"})["GIT_EDITOR"]
|
|
156
|
+
'code'
|
|
157
|
+
|
|
158
|
+
>>> from vt.utils.commons.commons.core_py import Unset, UNSET
|
|
159
|
+
>>> merge_git_envs({"GIT_EDITOR": UNSET}, {"GIT_EDITOR": "nano"})["GIT_EDITOR"] is UNSET
|
|
160
|
+
True
|
|
161
|
+
|
|
162
|
+
:param primary: The first priority ``GitEnvVars`` object.
|
|
163
|
+
:param fallback: The second priority or fallback ``GitEnvVars`` object.
|
|
164
|
+
:return: A new ``GitEnvVars`` object that contains all the properties from the ``primary`` ``GitEnvVars`` object and
|
|
165
|
+
fallbacks on the corresponding property from the ``fallback`` ``GitEnvVars`` object if that corresponding property
|
|
166
|
+
is explicitly ``None`` in the ``primary`` ``GitEnvVars`` object.
|
|
167
|
+
"""
|
|
168
|
+
return merge_typed_dicts(primary, fallback, GitEnvVars, omit_unset_keys=False)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
# TODO: check for typing this function
|
|
172
|
+
def merge_typed_dicts(
|
|
173
|
+
primary, fallback, the_typed_dict, *, omit_unset_keys: bool = False
|
|
174
|
+
):
|
|
175
|
+
"""
|
|
176
|
+
Merge typed dicts preserving insertion order of ``fallback`` then new keys from ``primary``.
|
|
177
|
+
|
|
178
|
+
Value resolution matches the previous annotation-order merge: primary wins, ``None`` in primary
|
|
179
|
+
falls back, ``Unset`` is kept for env merges and omitted from opts when ``omit_unset_keys`` is True.
|
|
180
|
+
"""
|
|
181
|
+
valid_keys = frozenset(the_typed_dict.__annotations__.keys())
|
|
182
|
+
|
|
183
|
+
keys_order: list[str] = []
|
|
184
|
+
seen: set[str] = set()
|
|
185
|
+
for k in fallback:
|
|
186
|
+
if k in valid_keys and k not in seen:
|
|
187
|
+
keys_order.append(k)
|
|
188
|
+
seen.add(k)
|
|
189
|
+
for k in primary:
|
|
190
|
+
if k in valid_keys and k not in seen:
|
|
191
|
+
keys_order.append(k)
|
|
192
|
+
seen.add(k)
|
|
193
|
+
|
|
194
|
+
merged: dict = {}
|
|
195
|
+
for k in keys_order:
|
|
196
|
+
if k in primary and is_unset(primary[k]):
|
|
197
|
+
if omit_unset_keys:
|
|
198
|
+
continue
|
|
199
|
+
merged[k] = primary[k]
|
|
200
|
+
continue
|
|
201
|
+
|
|
202
|
+
if k in primary:
|
|
203
|
+
val = primary[k]
|
|
204
|
+
if val is None:
|
|
205
|
+
val = fallback.get(k)
|
|
206
|
+
else:
|
|
207
|
+
val = fallback.get(k)
|
|
208
|
+
|
|
209
|
+
if val is None:
|
|
210
|
+
continue
|
|
211
|
+
if omit_unset_keys and is_unset(val):
|
|
212
|
+
continue
|
|
213
|
+
merged[k] = val
|
|
214
|
+
|
|
215
|
+
return merged
|