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,613 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# coding=utf-8
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Simple and direct implementations of git commands using subprocess calls.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import warnings
|
|
11
|
+
from abc import ABC
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from subprocess import Popen
|
|
14
|
+
from typing import override, Literal, overload, Callable
|
|
15
|
+
|
|
16
|
+
from vt.utils.commons.commons.op import RootDirOp
|
|
17
|
+
|
|
18
|
+
from gitbolt.base import Version
|
|
19
|
+
from gitbolt.add import AddArgsValidator
|
|
20
|
+
from gitbolt.hash_object import HashObjectArgsValidator
|
|
21
|
+
from gitbolt.subprocess import (
|
|
22
|
+
GitCommand,
|
|
23
|
+
VersionCommand,
|
|
24
|
+
LsTreeCommand,
|
|
25
|
+
GitSubcmdCommand,
|
|
26
|
+
AddCommand,
|
|
27
|
+
UncheckedSubcmd,
|
|
28
|
+
)
|
|
29
|
+
from gitbolt.subprocess.add import AddCLIArgsBuilder
|
|
30
|
+
from gitbolt.subprocess.base import GitSession, WorktreeCommand, HashObjectCommand, WritingHashObjectCommand
|
|
31
|
+
from gitbolt.subprocess.constants import VERSION_CMD
|
|
32
|
+
from gitbolt.subprocess.hash_object import HashObjectCLIArgsBuilder
|
|
33
|
+
from gitbolt.subprocess.ls_tree import LsTreeCLIArgsBuilder
|
|
34
|
+
from gitbolt.subprocess.runner import GitCommandRunner
|
|
35
|
+
from gitbolt.subprocess.runner.simple import SimpleGitCR
|
|
36
|
+
from gitbolt.ls_tree import LsTreeArgsValidator
|
|
37
|
+
from gitbolt.subprocess.utils.main_cmd import git_main_opts_clean_cap_c
|
|
38
|
+
from gitbolt.subprocess.worktree import WorktreeCLIArgsBuilder
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class GitSubcmdCommandImpl(GitSubcmdCommand, ABC):
|
|
42
|
+
def __init__(self, git: GitCommand):
|
|
43
|
+
self._underlying_git = git
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def git(self) -> GitCommand:
|
|
47
|
+
return self._underlying_git
|
|
48
|
+
|
|
49
|
+
def _set_underlying_git(self, git: "GitCommand") -> None:
|
|
50
|
+
self._underlying_git = git
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class VersionCommandImpl(VersionCommand, GitSubcmdCommandImpl):
|
|
54
|
+
@overload
|
|
55
|
+
def version(self) -> Version.VersionInfo: ...
|
|
56
|
+
|
|
57
|
+
@overload
|
|
58
|
+
def version(self, build_options: Literal[True]) -> Version.VersionWithBuildInfo: ...
|
|
59
|
+
|
|
60
|
+
@override
|
|
61
|
+
def version(
|
|
62
|
+
self, build_options: Literal[True, False] = False
|
|
63
|
+
) -> Version.VersionInfo | Version.VersionWithBuildInfo:
|
|
64
|
+
self._require_valid_args(build_options)
|
|
65
|
+
main_cmd_args = self.git.build_main_cmd_args()
|
|
66
|
+
sub_cmd_args = [VERSION_CMD]
|
|
67
|
+
env_vars = self.git.build_git_envs()
|
|
68
|
+
if build_options:
|
|
69
|
+
sub_cmd_args.append("--build-options")
|
|
70
|
+
|
|
71
|
+
def rosetta_supplier():
|
|
72
|
+
return self.git.runner.run_git_command(
|
|
73
|
+
main_cmd_args,
|
|
74
|
+
sub_cmd_args,
|
|
75
|
+
check=True,
|
|
76
|
+
text=True,
|
|
77
|
+
capture_output=True,
|
|
78
|
+
env=env_vars,
|
|
79
|
+
).stdout.strip()
|
|
80
|
+
|
|
81
|
+
if build_options:
|
|
82
|
+
return VersionCommand.VersionWithBuildInfoForCmd(rosetta_supplier)
|
|
83
|
+
return VersionCommand.VersionInfoForCmd(rosetta_supplier)
|
|
84
|
+
|
|
85
|
+
def clone(self) -> "VersionCommandImpl":
|
|
86
|
+
return VersionCommandImpl(self.git)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class LsTreeCommandImpl(LsTreeCommand, GitSubcmdCommandImpl):
|
|
90
|
+
def __init__(
|
|
91
|
+
self,
|
|
92
|
+
git_root_dir: Path,
|
|
93
|
+
git: GitCommand,
|
|
94
|
+
*,
|
|
95
|
+
args_validator: LsTreeArgsValidator | None = None,
|
|
96
|
+
cli_args_builder: LsTreeCLIArgsBuilder | None = None,
|
|
97
|
+
):
|
|
98
|
+
"""
|
|
99
|
+
``ls-tree`` cli command implementation using subprocess.
|
|
100
|
+
|
|
101
|
+
:param git_root_dir: Path to the Git repository root.
|
|
102
|
+
:param git: Underlying Git command interface.
|
|
103
|
+
:param args_validator: Optional custom argument validator. If None, uses the default from superclass.
|
|
104
|
+
:param cli_args_builder: Optional CLI args builder. If None, uses the default from superclass.
|
|
105
|
+
"""
|
|
106
|
+
super().__init__(git)
|
|
107
|
+
self._git_root_dir = git_root_dir
|
|
108
|
+
self._args_validator = args_validator or super().args_validator
|
|
109
|
+
self._cli_args_builder = cli_args_builder or super().cli_args_builder
|
|
110
|
+
|
|
111
|
+
@override
|
|
112
|
+
@property
|
|
113
|
+
def root_dir(self) -> Path:
|
|
114
|
+
return self._git_root_dir
|
|
115
|
+
|
|
116
|
+
@override
|
|
117
|
+
@property
|
|
118
|
+
def args_validator(self) -> LsTreeArgsValidator:
|
|
119
|
+
return self._args_validator
|
|
120
|
+
|
|
121
|
+
@override
|
|
122
|
+
@property
|
|
123
|
+
def cli_args_builder(self) -> LsTreeCLIArgsBuilder:
|
|
124
|
+
return self._cli_args_builder
|
|
125
|
+
|
|
126
|
+
def clone(self) -> "LsTreeCommandImpl":
|
|
127
|
+
return LsTreeCommandImpl(self.root_dir, self.git)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class AddCommandImpl(AddCommand, GitSubcmdCommandImpl):
|
|
131
|
+
def __init__(
|
|
132
|
+
self,
|
|
133
|
+
root_dir: Path,
|
|
134
|
+
git: GitCommand,
|
|
135
|
+
*,
|
|
136
|
+
args_validator: AddArgsValidator | None = None,
|
|
137
|
+
cli_args_builder: AddCLIArgsBuilder | None = None,
|
|
138
|
+
):
|
|
139
|
+
super().__init__(git)
|
|
140
|
+
self._root_dir = root_dir
|
|
141
|
+
self._args_validator = args_validator or super().args_validator
|
|
142
|
+
self._cli_args_builder = cli_args_builder or super().cli_args_builder
|
|
143
|
+
|
|
144
|
+
@override
|
|
145
|
+
@property
|
|
146
|
+
def root_dir(self) -> Path:
|
|
147
|
+
return self._root_dir
|
|
148
|
+
|
|
149
|
+
@override
|
|
150
|
+
@property
|
|
151
|
+
def args_validator(self) -> AddArgsValidator:
|
|
152
|
+
return self._args_validator
|
|
153
|
+
|
|
154
|
+
@override
|
|
155
|
+
@property
|
|
156
|
+
def cli_args_builder(self) -> AddCLIArgsBuilder:
|
|
157
|
+
return self._cli_args_builder
|
|
158
|
+
|
|
159
|
+
def clone(self) -> "AddCommandImpl":
|
|
160
|
+
return AddCommandImpl(self.root_dir, self.git, args_validator=self.args_validator,
|
|
161
|
+
cli_args_builder=self.cli_args_builder)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class HashObjectCommandImpl(HashObjectCommand, GitSubcmdCommandImpl):
|
|
165
|
+
def __init__(
|
|
166
|
+
self,
|
|
167
|
+
git: GitCommand,
|
|
168
|
+
*,
|
|
169
|
+
args_validator: HashObjectArgsValidator | None = None,
|
|
170
|
+
cli_args_builder: HashObjectCLIArgsBuilder | None = None,
|
|
171
|
+
):
|
|
172
|
+
"""
|
|
173
|
+
``hash-object`` cli command implementation using subprocess.
|
|
174
|
+
|
|
175
|
+
:param git: Underlying Git command interface.
|
|
176
|
+
:param args_validator: Optional custom argument validator. If None, uses the default from superclass.
|
|
177
|
+
:param cli_args_builder: Optional CLI args builder. If None, uses the default from superclass.
|
|
178
|
+
"""
|
|
179
|
+
super().__init__(git)
|
|
180
|
+
self._args_validator = args_validator or super().args_validator()
|
|
181
|
+
self._cli_args_builder = cli_args_builder or super().cli_args_builder
|
|
182
|
+
|
|
183
|
+
@override
|
|
184
|
+
def args_validator(self) -> HashObjectArgsValidator:
|
|
185
|
+
return self._args_validator
|
|
186
|
+
|
|
187
|
+
@override
|
|
188
|
+
@property
|
|
189
|
+
def cli_args_builder(self) -> HashObjectCLIArgsBuilder:
|
|
190
|
+
return self._cli_args_builder
|
|
191
|
+
|
|
192
|
+
@override
|
|
193
|
+
def clone(self) -> "HashObjectCommandImpl":
|
|
194
|
+
return HashObjectCommandImpl(self.git, args_validator=self.args_validator(),
|
|
195
|
+
cli_args_builder=self.cli_args_builder)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
class WritingHashObjectCommandImpl(WritingHashObjectCommand, HashObjectCommandImpl):
|
|
199
|
+
def __init__(
|
|
200
|
+
self,
|
|
201
|
+
root_dir: Path,
|
|
202
|
+
git: GitCommand,
|
|
203
|
+
*,
|
|
204
|
+
args_validator: HashObjectArgsValidator | None = None,
|
|
205
|
+
cli_args_builder: HashObjectCLIArgsBuilder | None = None,
|
|
206
|
+
):
|
|
207
|
+
super().__init__(
|
|
208
|
+
git,
|
|
209
|
+
args_validator=args_validator,
|
|
210
|
+
cli_args_builder=cli_args_builder,
|
|
211
|
+
)
|
|
212
|
+
self._root_dir = root_dir
|
|
213
|
+
|
|
214
|
+
@override
|
|
215
|
+
@property
|
|
216
|
+
def root_dir(self) -> Path:
|
|
217
|
+
return self._root_dir
|
|
218
|
+
|
|
219
|
+
@override
|
|
220
|
+
def _hash_object_cwd(self) -> Path | None:
|
|
221
|
+
return self.root_dir
|
|
222
|
+
|
|
223
|
+
@override
|
|
224
|
+
def clone(self) -> "WritingHashObjectCommandImpl":
|
|
225
|
+
return WritingHashObjectCommandImpl(self.root_dir, self.git, args_validator=self.args_validator(),
|
|
226
|
+
cli_args_builder=self.cli_args_builder)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
class WorktreeSubcmdCommandImpl(WorktreeCommand.WorktreeSubcmdCommand, ABC):
|
|
230
|
+
def __init__(self, worktree: WorktreeCommand,):
|
|
231
|
+
self._underlying_worktree = worktree
|
|
232
|
+
self._root_dir = self.underlying_worktree.root_dir
|
|
233
|
+
self._underlying_git = self.underlying_worktree.git
|
|
234
|
+
self._cli_args_builder = self.underlying_worktree.cli_args_builder
|
|
235
|
+
|
|
236
|
+
@override
|
|
237
|
+
@property
|
|
238
|
+
def git(self) -> GitCommand:
|
|
239
|
+
return self._underlying_git
|
|
240
|
+
|
|
241
|
+
@override
|
|
242
|
+
@property
|
|
243
|
+
def underlying_worktree(self) -> WorktreeCommand:
|
|
244
|
+
return self._underlying_worktree
|
|
245
|
+
|
|
246
|
+
@override
|
|
247
|
+
def _set_underlying_worktree(self, worktree: "WorktreeCommand") -> None:
|
|
248
|
+
self._underlying_worktree = worktree
|
|
249
|
+
self._underlying_git = self.underlying_worktree.git
|
|
250
|
+
|
|
251
|
+
@property
|
|
252
|
+
def root_dir(self) -> Path:
|
|
253
|
+
return self._root_dir
|
|
254
|
+
|
|
255
|
+
@property
|
|
256
|
+
def cli_args_builder(self) -> WorktreeCLIArgsBuilder:
|
|
257
|
+
return self._cli_args_builder
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
class WorktreeCommandImpl(WorktreeCommand, GitSubcmdCommandImpl):
|
|
261
|
+
|
|
262
|
+
def __init__(
|
|
263
|
+
self,
|
|
264
|
+
root_dir: Path,
|
|
265
|
+
git: GitCommand,
|
|
266
|
+
*,
|
|
267
|
+
list_subcmd: WorktreeCommand.ListCommand | None = None,
|
|
268
|
+
lock_subcmd: WorktreeCommand.LockCommand | None = None,
|
|
269
|
+
unlock_subcmd: WorktreeCommand.UnLockCommand | None = None,
|
|
270
|
+
add_subcmd: WorktreeCommand.AddCommand | None = None,
|
|
271
|
+
remove_subcmd: WorktreeCommand.RemoveCommand | None = None,
|
|
272
|
+
move_subcmd: WorktreeCommand.MoveCommand | None = None,
|
|
273
|
+
prune_subcmd: WorktreeCommand.PruneCommand | None = None,
|
|
274
|
+
repair_subcmd: WorktreeCommand.RepairCommand | None = None,
|
|
275
|
+
cli_args_builder: WorktreeCLIArgsBuilder | None = None,
|
|
276
|
+
):
|
|
277
|
+
super().__init__(git)
|
|
278
|
+
self._root_dir = root_dir
|
|
279
|
+
self._cli_args_builder = cli_args_builder or super().cli_args_builder
|
|
280
|
+
self._list_subcmd: WorktreeCommand.ListCommand = list_subcmd or WorktreeCommandImpl.ListCommandImpl(self)
|
|
281
|
+
self._lock_subcmd: WorktreeCommand.LockCommand = lock_subcmd or WorktreeCommandImpl.LockCommandImpl(self)
|
|
282
|
+
self._unlock_subcmd: WorktreeCommand.UnLockCommand = unlock_subcmd or WorktreeCommandImpl.UnLockCommandImpl(self)
|
|
283
|
+
self._add_subcmd: WorktreeCommand.AddCommand = add_subcmd or WorktreeCommandImpl.AddCommandImpl(self)
|
|
284
|
+
self._remove_subcmd: WorktreeCommand.RemoveCommand = remove_subcmd or WorktreeCommandImpl.RemoveCommandImpl(self)
|
|
285
|
+
self._move_subcmd: WorktreeCommand.MoveCommand = move_subcmd or WorktreeCommandImpl.MoveCommandImpl(self)
|
|
286
|
+
self._prune_subcmd: WorktreeCommand.PruneCommand = prune_subcmd or WorktreeCommandImpl.PruneCommandImpl(self)
|
|
287
|
+
self._repair_subcmd: WorktreeCommand.RepairCommand = repair_subcmd or WorktreeCommandImpl.RepairCommandImpl(self)
|
|
288
|
+
|
|
289
|
+
class ListCommandImpl(WorktreeCommand.ListCommand, WorktreeSubcmdCommandImpl):
|
|
290
|
+
@override
|
|
291
|
+
def clone(self) -> WorktreeCommandImpl.ListCommandImpl:
|
|
292
|
+
return WorktreeCommandImpl.ListCommandImpl(self.underlying_worktree)
|
|
293
|
+
|
|
294
|
+
class LockCommandImpl(WorktreeCommand.LockCommand, WorktreeSubcmdCommandImpl):
|
|
295
|
+
@override
|
|
296
|
+
def clone(self) -> WorktreeCommandImpl.LockCommandImpl:
|
|
297
|
+
return WorktreeCommandImpl.LockCommandImpl(self.underlying_worktree)
|
|
298
|
+
|
|
299
|
+
class UnLockCommandImpl(WorktreeCommand.UnLockCommand, WorktreeSubcmdCommandImpl):
|
|
300
|
+
@override
|
|
301
|
+
def clone(self) -> WorktreeCommandImpl.UnLockCommandImpl:
|
|
302
|
+
return WorktreeCommandImpl.UnLockCommandImpl(self.underlying_worktree)
|
|
303
|
+
|
|
304
|
+
class AddCommandImpl(WorktreeCommand.AddCommand, WorktreeSubcmdCommandImpl):
|
|
305
|
+
@override
|
|
306
|
+
def clone(self) -> WorktreeCommandImpl.AddCommandImpl:
|
|
307
|
+
return WorktreeCommandImpl.AddCommandImpl(self.underlying_worktree)
|
|
308
|
+
|
|
309
|
+
class RemoveCommandImpl(WorktreeCommand.RemoveCommand, WorktreeSubcmdCommandImpl):
|
|
310
|
+
@override
|
|
311
|
+
def clone(self) -> WorktreeCommandImpl.RemoveCommandImpl:
|
|
312
|
+
return WorktreeCommandImpl.RemoveCommandImpl(self.underlying_worktree)
|
|
313
|
+
|
|
314
|
+
class MoveCommandImpl(WorktreeCommand.MoveCommand, WorktreeSubcmdCommandImpl):
|
|
315
|
+
@override
|
|
316
|
+
def clone(self) -> WorktreeCommandImpl.MoveCommandImpl:
|
|
317
|
+
return WorktreeCommandImpl.MoveCommandImpl(self.underlying_worktree)
|
|
318
|
+
|
|
319
|
+
class PruneCommandImpl(WorktreeCommand.PruneCommand, WorktreeSubcmdCommandImpl):
|
|
320
|
+
@override
|
|
321
|
+
def clone(self) -> WorktreeCommandImpl.PruneCommandImpl:
|
|
322
|
+
return WorktreeCommandImpl.PruneCommandImpl(self.underlying_worktree)
|
|
323
|
+
|
|
324
|
+
class RepairCommandImpl(WorktreeCommand.RepairCommand, WorktreeSubcmdCommandImpl):
|
|
325
|
+
@override
|
|
326
|
+
def clone(self) -> WorktreeCommandImpl.RepairCommandImpl:
|
|
327
|
+
return WorktreeCommandImpl.RepairCommandImpl(self.underlying_worktree)
|
|
328
|
+
|
|
329
|
+
@override
|
|
330
|
+
def list_subcmd(self) -> WorktreeCommand.ListCommand:
|
|
331
|
+
_list_subcmd = self._list_subcmd.clone()
|
|
332
|
+
_list_subcmd._set_underlying_worktree(self)
|
|
333
|
+
return self._list_subcmd
|
|
334
|
+
|
|
335
|
+
@override
|
|
336
|
+
def lock_subcmd(self) -> WorktreeCommand.LockCommand:
|
|
337
|
+
_lock_subcmd = self._lock_subcmd.clone()
|
|
338
|
+
_lock_subcmd._set_underlying_worktree(self)
|
|
339
|
+
return self._lock_subcmd
|
|
340
|
+
|
|
341
|
+
@override
|
|
342
|
+
def unlock_subcmd(self) -> WorktreeCommand.UnLockCommand:
|
|
343
|
+
_unlock_subcmd = self._unlock_subcmd.clone()
|
|
344
|
+
_unlock_subcmd._set_underlying_worktree(self)
|
|
345
|
+
return self._unlock_subcmd
|
|
346
|
+
|
|
347
|
+
@override
|
|
348
|
+
def move_subcmd(self) -> WorktreeCommand.MoveCommand:
|
|
349
|
+
_move_subcmd = self._move_subcmd.clone()
|
|
350
|
+
_move_subcmd._set_underlying_worktree(self)
|
|
351
|
+
return self._move_subcmd
|
|
352
|
+
|
|
353
|
+
@override
|
|
354
|
+
def prune_subcmd(self) -> WorktreeCommand.PruneCommand:
|
|
355
|
+
_prune_subcmd = self._prune_subcmd.clone()
|
|
356
|
+
_prune_subcmd._set_underlying_worktree(self)
|
|
357
|
+
return self._prune_subcmd
|
|
358
|
+
|
|
359
|
+
@override
|
|
360
|
+
def remove_subcmd(self) -> WorktreeCommand.RemoveCommand:
|
|
361
|
+
_remove_subcmd = self._remove_subcmd.clone()
|
|
362
|
+
_remove_subcmd._set_underlying_worktree(self)
|
|
363
|
+
return self._remove_subcmd
|
|
364
|
+
|
|
365
|
+
@override
|
|
366
|
+
def repair_subcmd(self) -> WorktreeCommand.RepairCommand:
|
|
367
|
+
_repair_subcmd = self._repair_subcmd.clone()
|
|
368
|
+
_repair_subcmd._set_underlying_worktree(self)
|
|
369
|
+
return self._repair_subcmd
|
|
370
|
+
|
|
371
|
+
@override
|
|
372
|
+
def add_subcmd(self) -> WorktreeCommand.AddCommand:
|
|
373
|
+
_add_subcmd = self._add_subcmd.clone()
|
|
374
|
+
_add_subcmd._set_underlying_worktree(self)
|
|
375
|
+
return self._add_subcmd
|
|
376
|
+
|
|
377
|
+
@override
|
|
378
|
+
@property
|
|
379
|
+
def root_dir(self) -> Path:
|
|
380
|
+
return self._root_dir
|
|
381
|
+
|
|
382
|
+
@override
|
|
383
|
+
@property
|
|
384
|
+
def cli_args_builder(self) -> WorktreeCLIArgsBuilder:
|
|
385
|
+
return self._cli_args_builder
|
|
386
|
+
|
|
387
|
+
def clone(self) -> "WorktreeCommandImpl":
|
|
388
|
+
return WorktreeCommandImpl(self.root_dir, self.git,
|
|
389
|
+
list_subcmd=self.list_subcmd(),
|
|
390
|
+
lock_subcmd=self.lock_subcmd(),
|
|
391
|
+
unlock_subcmd=self.unlock_subcmd(),
|
|
392
|
+
add_subcmd=self.add_subcmd(),
|
|
393
|
+
remove_subcmd=self.remove_subcmd(),
|
|
394
|
+
move_subcmd=self.move_subcmd(),
|
|
395
|
+
prune_subcmd=self.prune_subcmd(),
|
|
396
|
+
repair_subcmd=self.repair_subcmd(),
|
|
397
|
+
cli_args_builder=self.cli_args_builder)
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
class UncheckedSubcmdImpl(UncheckedSubcmd, GitSubcmdCommandImpl):
|
|
401
|
+
def __init__(self, root_dir: Path, git: GitCommand):
|
|
402
|
+
super().__init__(git)
|
|
403
|
+
self._root_dir = root_dir
|
|
404
|
+
|
|
405
|
+
@override
|
|
406
|
+
@property
|
|
407
|
+
def root_dir(self) -> Path:
|
|
408
|
+
return self._root_dir
|
|
409
|
+
|
|
410
|
+
def clone(self) -> "UncheckedSubcmdImpl":
|
|
411
|
+
return UncheckedSubcmdImpl(self.root_dir, self.git)
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
class SimpleGitCommand(GitCommand, RootDirOp):
|
|
415
|
+
|
|
416
|
+
def __init__(
|
|
417
|
+
self,
|
|
418
|
+
git_root_dir: Path = Path.cwd(),
|
|
419
|
+
runner: GitCommandRunner = SimpleGitCR(),
|
|
420
|
+
*,
|
|
421
|
+
version_subcmd: VersionCommand | None = None,
|
|
422
|
+
ls_tree_subcmd: LsTreeCommand | None = None,
|
|
423
|
+
add_subcmd: AddCommand | None = None,
|
|
424
|
+
hash_object_subcmd: HashObjectCommand | None = None,
|
|
425
|
+
writing_hash_object_subcmd: WritingHashObjectCommand | None = None,
|
|
426
|
+
worktree_subcmd: WorktreeCommand | None = None,
|
|
427
|
+
subcmd_unchecked: UncheckedSubcmd | None = None,
|
|
428
|
+
):
|
|
429
|
+
super().__init__(runner)
|
|
430
|
+
self.git_root_dir = git_root_dir
|
|
431
|
+
self._version_subcmd = version_subcmd or VersionCommandImpl(self)
|
|
432
|
+
self._ls_tree = ls_tree_subcmd or LsTreeCommandImpl(self.root_dir, self)
|
|
433
|
+
self._add_subcmd = add_subcmd or AddCommandImpl(self.root_dir, self)
|
|
434
|
+
self._hash_object_subcmd = hash_object_subcmd or HashObjectCommandImpl(self)
|
|
435
|
+
self._writing_hash_object_subcmd = (
|
|
436
|
+
writing_hash_object_subcmd
|
|
437
|
+
or WritingHashObjectCommandImpl(self.root_dir, self)
|
|
438
|
+
)
|
|
439
|
+
self._worktree_subcmd = worktree_subcmd or WorktreeCommandImpl(self.root_dir, self)
|
|
440
|
+
self._subcmd_unchecked = subcmd_unchecked or UncheckedSubcmdImpl(self.root_dir, self)
|
|
441
|
+
|
|
442
|
+
@override
|
|
443
|
+
def version_subcmd(self) -> VersionCommand:
|
|
444
|
+
# TODO: in all subcommand methods, find a better way to retain envs and opts rather than cloning each time
|
|
445
|
+
# and setting the underlying git.
|
|
446
|
+
version_subcmd = self._version_subcmd.clone()
|
|
447
|
+
version_subcmd._set_underlying_git(self)
|
|
448
|
+
return version_subcmd
|
|
449
|
+
|
|
450
|
+
@override
|
|
451
|
+
def ls_tree_subcmd(self) -> LsTreeCommand:
|
|
452
|
+
ls_tree_subcmd = self._ls_tree.clone()
|
|
453
|
+
ls_tree_subcmd._set_underlying_git(self)
|
|
454
|
+
return ls_tree_subcmd
|
|
455
|
+
|
|
456
|
+
@override
|
|
457
|
+
def add_subcmd(self) -> AddCommand:
|
|
458
|
+
add_subcmd = self._add_subcmd.clone()
|
|
459
|
+
add_subcmd._set_underlying_git(self)
|
|
460
|
+
return add_subcmd
|
|
461
|
+
|
|
462
|
+
@override
|
|
463
|
+
def hash_object_subcmd(self) -> HashObjectCommand:
|
|
464
|
+
hash_object_subcmd = self._hash_object_subcmd.clone()
|
|
465
|
+
hash_object_subcmd._set_underlying_git(self)
|
|
466
|
+
return hash_object_subcmd
|
|
467
|
+
|
|
468
|
+
@override
|
|
469
|
+
def writing_hash_object_subcmd(self) -> WritingHashObjectCommand:
|
|
470
|
+
writing_hash_object_subcmd = self._writing_hash_object_subcmd.clone()
|
|
471
|
+
writing_hash_object_subcmd._set_underlying_git(self)
|
|
472
|
+
return writing_hash_object_subcmd
|
|
473
|
+
|
|
474
|
+
@override
|
|
475
|
+
def worktree_subcmd(self) -> WorktreeCommand:
|
|
476
|
+
warnings.warn("Worktree implementations are not stable. Use subcmd_unchecked instead.")
|
|
477
|
+
return self._worktree_subcmd_clone()
|
|
478
|
+
|
|
479
|
+
def _worktree_subcmd_clone(self) -> WorktreeCommand:
|
|
480
|
+
"""
|
|
481
|
+
Non public-facing worktree clone extract.
|
|
482
|
+
"""
|
|
483
|
+
worktree_subcmd = self._worktree_subcmd.clone()
|
|
484
|
+
worktree_subcmd._set_underlying_git(self)
|
|
485
|
+
return worktree_subcmd
|
|
486
|
+
|
|
487
|
+
@override
|
|
488
|
+
def _subclass_clone(self) -> SimpleGitCommand:
|
|
489
|
+
return SimpleGitCommand(
|
|
490
|
+
self.root_dir,
|
|
491
|
+
self.runner,
|
|
492
|
+
version_subcmd=self.version_subcmd(),
|
|
493
|
+
ls_tree_subcmd=self.ls_tree_subcmd(),
|
|
494
|
+
add_subcmd=self.add_subcmd(),
|
|
495
|
+
hash_object_subcmd=self.hash_object_subcmd(),
|
|
496
|
+
writing_hash_object_subcmd=self.writing_hash_object_subcmd(),
|
|
497
|
+
worktree_subcmd=self._worktree_subcmd_clone(),
|
|
498
|
+
subcmd_unchecked=self.subcmd_unchecked(),
|
|
499
|
+
)
|
|
500
|
+
|
|
501
|
+
@override
|
|
502
|
+
@property
|
|
503
|
+
def root_dir(self) -> Path:
|
|
504
|
+
return self.git_root_dir
|
|
505
|
+
|
|
506
|
+
@override
|
|
507
|
+
def subcmd_unchecked(self) -> UncheckedSubcmd:
|
|
508
|
+
subcmd_unchecked = self._subcmd_unchecked.clone()
|
|
509
|
+
subcmd_unchecked._set_underlying_git(self)
|
|
510
|
+
return subcmd_unchecked
|
|
511
|
+
|
|
512
|
+
def session(self, **commands: list[str] | Callable[[], Popen[bytes]]) -> GitSession:
|
|
513
|
+
cmds: dict[str, Callable[[], Popen[bytes]]] = {}
|
|
514
|
+
|
|
515
|
+
# curb the lambda-late-binding-trap
|
|
516
|
+
# resource: https://medium.com/skiller-whale/late-binding-variables-its-a-trap-c17af980164f
|
|
517
|
+
def lambda_for_subcmd_popen(_runnable_cmd: list[str]):
|
|
518
|
+
return lambda: self.subcmd_unchecked().popen(_runnable_cmd)
|
|
519
|
+
|
|
520
|
+
for cmd_name, runnable_cmd in commands.items():
|
|
521
|
+
if callable(runnable_cmd):
|
|
522
|
+
cmds[cmd_name] = runnable_cmd
|
|
523
|
+
else:
|
|
524
|
+
cmds[cmd_name] = lambda_for_subcmd_popen(runnable_cmd)
|
|
525
|
+
return GitSession(self, **cmds)
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
class CLISimpleGitCommand(SimpleGitCommand):
|
|
529
|
+
"""
|
|
530
|
+
A simple git command that can run using CLI params.
|
|
531
|
+
"""
|
|
532
|
+
|
|
533
|
+
def __init__(
|
|
534
|
+
self,
|
|
535
|
+
git_root_dir: Path = Path.cwd(),
|
|
536
|
+
runner: GitCommandRunner = SimpleGitCR(),
|
|
537
|
+
*,
|
|
538
|
+
opts: list[str] | None = None,
|
|
539
|
+
envs: dict[str, str] | None = None,
|
|
540
|
+
prefer_cli: bool = False,
|
|
541
|
+
version_subcmd: VersionCommand | None = None,
|
|
542
|
+
ls_tree_subcmd: LsTreeCommand | None = None,
|
|
543
|
+
add_subcmd: AddCommand | None = None,
|
|
544
|
+
hash_object_subcmd: HashObjectCommand | None = None,
|
|
545
|
+
writing_hash_object_subcmd: WritingHashObjectCommand | None = None,
|
|
546
|
+
worktree_subcmd: WorktreeCommand | None = None,
|
|
547
|
+
subcmd_unchecked: UncheckedSubcmd | None = None,
|
|
548
|
+
):
|
|
549
|
+
"""
|
|
550
|
+
:param opts: main git cli options.
|
|
551
|
+
:param envs: main git cli environment variables (env vars). Not supplying any env
|
|
552
|
+
vars (default behavior: ``None``) simply supplies all the env vars to the underlying runner.
|
|
553
|
+
:param prefer_cli: cli opts and envs will be given priority over programmatically set opts and envs. Setting
|
|
554
|
+
this param to ``True`` will make cli opts and envs appear later in the opts and envs strings which will
|
|
555
|
+
make them override previously programmatically set opts and envs.
|
|
556
|
+
"""
|
|
557
|
+
super().__init__(
|
|
558
|
+
git_root_dir,
|
|
559
|
+
runner,
|
|
560
|
+
version_subcmd=version_subcmd,
|
|
561
|
+
ls_tree_subcmd=ls_tree_subcmd,
|
|
562
|
+
add_subcmd=add_subcmd,
|
|
563
|
+
hash_object_subcmd=hash_object_subcmd,
|
|
564
|
+
writing_hash_object_subcmd=writing_hash_object_subcmd,
|
|
565
|
+
worktree_subcmd=worktree_subcmd,
|
|
566
|
+
subcmd_unchecked=subcmd_unchecked,
|
|
567
|
+
)
|
|
568
|
+
self._main_cmd_cli_opts = opts
|
|
569
|
+
self._cmd_cli_envs = envs
|
|
570
|
+
self.prefer_cli = prefer_cli
|
|
571
|
+
|
|
572
|
+
@override
|
|
573
|
+
def build_main_cmd_args(self) -> list[str]:
|
|
574
|
+
super_cli_cmd_opts = super().build_main_cmd_args()
|
|
575
|
+
_main_cmd_cli_opts = self._main_cmd_cli_opts
|
|
576
|
+
if _main_cmd_cli_opts:
|
|
577
|
+
if self.prefer_cli:
|
|
578
|
+
if "-C" in _main_cmd_cli_opts:
|
|
579
|
+
# clean previous -C cli opts if -C is present in the current opts
|
|
580
|
+
super_cli_cmd_opts = git_main_opts_clean_cap_c(super_cli_cmd_opts)
|
|
581
|
+
return super_cli_cmd_opts + _main_cmd_cli_opts
|
|
582
|
+
else:
|
|
583
|
+
if "-C" in super_cli_cmd_opts:
|
|
584
|
+
# clean current -C cli opts if -C is present in the previous opts
|
|
585
|
+
_main_cmd_cli_opts = git_main_opts_clean_cap_c(_main_cmd_cli_opts)
|
|
586
|
+
return _main_cmd_cli_opts + super_cli_cmd_opts
|
|
587
|
+
return super_cli_cmd_opts
|
|
588
|
+
|
|
589
|
+
@override
|
|
590
|
+
def build_git_envs(self) -> dict[str, str] | None:
|
|
591
|
+
if self._cmd_cli_envs is None:
|
|
592
|
+
return super().build_git_envs()
|
|
593
|
+
if self.prefer_cli:
|
|
594
|
+
return (super().build_git_envs() or {}) | self._cmd_cli_envs
|
|
595
|
+
else:
|
|
596
|
+
return self._cmd_cli_envs | (super().build_git_envs() or {})
|
|
597
|
+
|
|
598
|
+
@override
|
|
599
|
+
def _subclass_clone(self) -> CLISimpleGitCommand:
|
|
600
|
+
return CLISimpleGitCommand(
|
|
601
|
+
self.root_dir,
|
|
602
|
+
self.runner,
|
|
603
|
+
opts=self._main_cmd_cli_opts,
|
|
604
|
+
envs=self._cmd_cli_envs,
|
|
605
|
+
prefer_cli=self.prefer_cli,
|
|
606
|
+
version_subcmd=self.version_subcmd(),
|
|
607
|
+
ls_tree_subcmd=self.ls_tree_subcmd(),
|
|
608
|
+
add_subcmd=self.add_subcmd(),
|
|
609
|
+
hash_object_subcmd=self.hash_object_subcmd(),
|
|
610
|
+
writing_hash_object_subcmd=self.writing_hash_object_subcmd(),
|
|
611
|
+
worktree_subcmd=self._worktree_subcmd_clone(),
|
|
612
|
+
subcmd_unchecked=self.subcmd_unchecked(),
|
|
613
|
+
)
|