basstler-stack 4.2.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.
- basstler/maintenance/__init__.py +0 -0
- basstler/maintenance/__main__.py +128 -0
- basstler/maintenance/board.py +113 -0
- basstler/maintenance/check_overview.py +80 -0
- basstler/maintenance/commands.py +595 -0
- basstler/maintenance/default_branch_sync.py +444 -0
- basstler/maintenance/exceptions.py +36 -0
- basstler/maintenance/fast_forward.py +223 -0
- basstler/maintenance/finish_summary.py +515 -0
- basstler/maintenance/fork_pull_request_action.py +38 -0
- basstler/maintenance/landed_closing.py +85 -0
- basstler/maintenance/landed_closing_follow_up.py +85 -0
- basstler/maintenance/promotion.py +248 -0
- basstler/maintenance/promotion_context.py +221 -0
- basstler/maintenance/report.py +304 -0
- basstler/maintenance/restack_procedure.py +279 -0
- basstler/maintenance/restack_steps.py +769 -0
- basstler/maintenance/upstream_pull_request_synchronization.py +189 -0
- basstler/maintenance/upstream_read_route.py +37 -0
- basstler/stack/__init__.py +0 -0
- basstler/stack/__main__.py +377 -0
- basstler/stack/branch_status.py +54 -0
- basstler/stack/checkout.py +416 -0
- basstler/stack/commit_move.py +218 -0
- basstler/stack/exceptions.py +233 -0
- basstler/stack/label_write.py +68 -0
- basstler/stack/merge_probe.py +195 -0
- basstler/stack/model.py +696 -0
- basstler/stack/promotability.py +103 -0
- basstler/stack/promotion_link.py +138 -0
- basstler/stack/report.py +304 -0
- basstler/stack/settings.py +453 -0
- basstler/stack/stack.toml +31 -0
- basstler_stack-4.2.0.dist-info/METADATA +16 -0
- basstler_stack-4.2.0.dist-info/RECORD +38 -0
- basstler_stack-4.2.0.dist-info/WHEEL +4 -0
- basstler_stack-4.2.0.dist-info/licenses/LICENSE +202 -0
- basstler_stack-4.2.0.dist-info/licenses/NOTICE +13 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"""
|
|
2
|
+
What a pass did, as a document, as printed lines, and as an exit status.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
from collections.abc import Sequence
|
|
9
|
+
from dataclasses import asdict, dataclass
|
|
10
|
+
from enum import StrEnum
|
|
11
|
+
|
|
12
|
+
from basstler.exit_codes import StackExitCode
|
|
13
|
+
from basstler.maintenance.fast_forward import FastForwardOutcome, FastForwardReport
|
|
14
|
+
from basstler.maintenance.landed_closing import ClosedAsLanded
|
|
15
|
+
from basstler.maintenance.promotion import Promotion
|
|
16
|
+
from basstler.maintenance.restack_steps import BranchOutcome, RestackOutcome
|
|
17
|
+
from basstler.maintenance.upstream_pull_request_synchronization import (
|
|
18
|
+
UpstreamPullRequestSynchronizationChange,
|
|
19
|
+
)
|
|
20
|
+
from basstler.stack.checkout import ForkRefresh
|
|
21
|
+
from basstler.stack.model import Reparent, SkippedPullRequest, Stack
|
|
22
|
+
from basstler.standard_streams import StandardStreamHandler
|
|
23
|
+
|
|
24
|
+
logger = StandardStreamHandler.logger_for(__name__)
|
|
25
|
+
"""
|
|
26
|
+
This module's logger, which is also what its command prints through.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
# %% vocabulary
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class MaintenanceReportKey(StrEnum):
|
|
33
|
+
"""
|
|
34
|
+
The keys :meth:`MaintenanceReport.as_json` adds beside the report's own fields.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
STATUS = "status"
|
|
38
|
+
"""
|
|
39
|
+
The exit status, by the name a caller reads.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
EXIT_CODE = "exit_code"
|
|
43
|
+
"""
|
|
44
|
+
The exit status, as the process exit code.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# %% the report a caller renders or emits
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass(frozen=True)
|
|
52
|
+
class MaintenanceReport:
|
|
53
|
+
"""
|
|
54
|
+
Everything one pass did, and the one thing it leaves for its caller.
|
|
55
|
+
|
|
56
|
+
``reparents`` is that one thing: retargeting a base is the single write GitHub
|
|
57
|
+
refuses to the credential this runs on, so it is reported rather than performed.
|
|
58
|
+
|
|
59
|
+
Every field defaults to nothing done, so a single command reports the part of the
|
|
60
|
+
pass it performed through the same object - and therefore through the same exit
|
|
61
|
+
status - as a whole pass does.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
fast_forward: FastForwardReport | None = None
|
|
65
|
+
"""
|
|
66
|
+
What became of the fork's base branch, absent when it was not attempted.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
restacked: tuple[BranchOutcome, ...] = ()
|
|
70
|
+
"""
|
|
71
|
+
What became of each branch in the restack plan.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
promoted: tuple[Promotion, ...] = ()
|
|
75
|
+
"""
|
|
76
|
+
The branches whose upstream link was built and recorded this pass.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
promotion_labels_cleared: tuple[str, ...] = ()
|
|
80
|
+
"""
|
|
81
|
+
The branches whose spent link label was removed this pass.
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
upstream_pull_request_synchronization_changes: tuple[
|
|
85
|
+
UpstreamPullRequestSynchronizationChange, ...
|
|
86
|
+
] = ()
|
|
87
|
+
"""
|
|
88
|
+
The writes bringing each pull request's in-review label and upstream link in step
|
|
89
|
+
this pass.
|
|
90
|
+
"""
|
|
91
|
+
|
|
92
|
+
reparents: tuple[Reparent, ...] = ()
|
|
93
|
+
"""
|
|
94
|
+
The children whose base has landed, for the caller to retarget.
|
|
95
|
+
"""
|
|
96
|
+
|
|
97
|
+
landed: tuple[str, ...] = ()
|
|
98
|
+
"""
|
|
99
|
+
The branches whose own commits are already in the upstream base and whose pull
|
|
100
|
+
request this pass did not close.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
closed_as_landed: tuple[ClosedAsLanded, ...] = ()
|
|
104
|
+
"""
|
|
105
|
+
The pull requests this pass closed because their work had landed upstream.
|
|
106
|
+
"""
|
|
107
|
+
|
|
108
|
+
closing_follow_ups: tuple[str, ...] = ()
|
|
109
|
+
"""
|
|
110
|
+
What became of each follow-up to those closings that had something to do.
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
promotable: tuple[str, ...] = ()
|
|
114
|
+
"""
|
|
115
|
+
The branches approved and unblocked, whether or not a link was built this pass.
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
skipped: tuple[SkippedPullRequest, ...] = ()
|
|
119
|
+
"""
|
|
120
|
+
The pull requests whose head is not on the fork, which the pass left alone.
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
fork_refresh: ForkRefresh = ForkRefresh()
|
|
124
|
+
"""
|
|
125
|
+
What refreshing the fork's references left out, which the pass read as it was.
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
@classmethod
|
|
129
|
+
def of_pass(
|
|
130
|
+
cls,
|
|
131
|
+
stack: Stack,
|
|
132
|
+
fast_forward_report: FastForwardReport | None,
|
|
133
|
+
restacked: Sequence[BranchOutcome],
|
|
134
|
+
promoted: Sequence[Promotion] = (),
|
|
135
|
+
promotion_labels_cleared: Sequence[str] = (),
|
|
136
|
+
upstream_pull_request_synchronization_changes: Sequence[
|
|
137
|
+
UpstreamPullRequestSynchronizationChange
|
|
138
|
+
] = (),
|
|
139
|
+
closed_as_landed: Sequence[ClosedAsLanded] = (),
|
|
140
|
+
closing_follow_ups: Sequence[str] = (),
|
|
141
|
+
) -> MaintenanceReport:
|
|
142
|
+
"""
|
|
143
|
+
Assemble one pass's outcomes and its leftovers into a single report.
|
|
144
|
+
|
|
145
|
+
:param stack: The derived stack, read for what the caller still has to do.
|
|
146
|
+
:param fast_forward_report: What became of the fork's base branch, if attempted.
|
|
147
|
+
:param restacked: What became of each branch in the restack plan.
|
|
148
|
+
:param promoted: The branches whose upstream link was built this pass.
|
|
149
|
+
:param promotion_labels_cleared: The branches whose spent link label was
|
|
150
|
+
removed.
|
|
151
|
+
:param upstream_pull_request_synchronization_changes: The writes bringing each pull request's in-review
|
|
152
|
+
label and upstream link in step.
|
|
153
|
+
:param closed_as_landed: The pull requests closed because their work landed.
|
|
154
|
+
:param closing_follow_ups: What became of each follow-up to those closings.
|
|
155
|
+
:return: The report.
|
|
156
|
+
"""
|
|
157
|
+
return cls(
|
|
158
|
+
fast_forward=fast_forward_report,
|
|
159
|
+
restacked=tuple(restacked),
|
|
160
|
+
promoted=tuple(promoted),
|
|
161
|
+
promotion_labels_cleared=tuple(promotion_labels_cleared),
|
|
162
|
+
upstream_pull_request_synchronization_changes=tuple(
|
|
163
|
+
upstream_pull_request_synchronization_changes
|
|
164
|
+
),
|
|
165
|
+
reparents=tuple(stack.reparents()),
|
|
166
|
+
landed=tuple(
|
|
167
|
+
branch.name
|
|
168
|
+
for branch in stack.landed_branches()
|
|
169
|
+
if branch.name not in {closed.branch for closed in closed_as_landed}
|
|
170
|
+
),
|
|
171
|
+
closed_as_landed=tuple(closed_as_landed),
|
|
172
|
+
closing_follow_ups=tuple(closing_follow_ups),
|
|
173
|
+
promotable=tuple(branch.name for branch in stack.promotion_order()),
|
|
174
|
+
skipped=tuple(stack.skipped_pull_requests()),
|
|
175
|
+
fork_refresh=stack.fork_refresh,
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
def as_json(self) -> str:
|
|
179
|
+
""":return: The report as one machine-readable document."""
|
|
180
|
+
status = self.exit_code
|
|
181
|
+
return json.dumps(
|
|
182
|
+
{
|
|
183
|
+
MaintenanceReportKey.STATUS: status.name_for_a_caller,
|
|
184
|
+
MaintenanceReportKey.EXIT_CODE: int(status),
|
|
185
|
+
**asdict(self),
|
|
186
|
+
},
|
|
187
|
+
indent=2,
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
@property
|
|
191
|
+
def branches_left_unpublished(self) -> tuple[BranchOutcome, ...]:
|
|
192
|
+
""":return: Every branch the pass could not leave in the state it wanted."""
|
|
193
|
+
return tuple(
|
|
194
|
+
outcome
|
|
195
|
+
for outcome in self.restacked
|
|
196
|
+
if outcome.outcome not in {RestackOutcome.PUSHED, RestackOutcome.UP_TO_DATE}
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
@property
|
|
200
|
+
def fast_forward_was_refused(self) -> bool:
|
|
201
|
+
""":return: Whether the fork's base was left behind the upstream."""
|
|
202
|
+
return (
|
|
203
|
+
self.fast_forward is not None
|
|
204
|
+
and self.fast_forward.outcome is FastForwardOutcome.REFUSED_NOT_FAST_FORWARD
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
@property
|
|
208
|
+
def exit_code(self) -> StackExitCode:
|
|
209
|
+
"""
|
|
210
|
+
One pass's exit status, from what it actually left behind.
|
|
211
|
+
|
|
212
|
+
Every command that produces a report derives its status here, so none of them
|
|
213
|
+
can disagree about what counts as a clean pass.
|
|
214
|
+
"""
|
|
215
|
+
if self.fast_forward_was_refused:
|
|
216
|
+
return StackExitCode.NOT_FAST_FORWARD
|
|
217
|
+
unpublished = self.branches_left_unpublished
|
|
218
|
+
if any(outcome.outcome is RestackOutcome.REFUSED for outcome in unpublished):
|
|
219
|
+
return StackExitCode.MOVE_REFUSED
|
|
220
|
+
if unpublished:
|
|
221
|
+
return StackExitCode.BRANCH_NEEDS_ATTENTION
|
|
222
|
+
return StackExitCode.SUCCESS
|
|
223
|
+
|
|
224
|
+
def print_summary(self) -> None:
|
|
225
|
+
"""
|
|
226
|
+
Print one line per thing the pass did, in the order the pass did them.
|
|
227
|
+
"""
|
|
228
|
+
self.print_fast_forward()
|
|
229
|
+
self.print_restack()
|
|
230
|
+
self.print_promotions()
|
|
231
|
+
self.print_upstream_pull_request_synchronization_changes()
|
|
232
|
+
self.print_skipped()
|
|
233
|
+
self.print_missing_on_fork()
|
|
234
|
+
|
|
235
|
+
def print_fast_forward(self) -> None:
|
|
236
|
+
"""
|
|
237
|
+
Print what became of the fork's base branch, if it was attempted.
|
|
238
|
+
"""
|
|
239
|
+
if self.fast_forward is None:
|
|
240
|
+
return
|
|
241
|
+
report = self.fast_forward
|
|
242
|
+
logger.info(f"{report.fork_reference}\t{report.outcome}\t{report.commit}")
|
|
243
|
+
if report.explanation:
|
|
244
|
+
logger.error(report.explanation)
|
|
245
|
+
|
|
246
|
+
def print_restack(self) -> None:
|
|
247
|
+
"""
|
|
248
|
+
Print what became of each branch in the restack plan.
|
|
249
|
+
"""
|
|
250
|
+
for outcome in self.restacked:
|
|
251
|
+
detail = (
|
|
252
|
+
",".join(outcome.conflicting_paths)
|
|
253
|
+
or ",".join(outcome.refusals)
|
|
254
|
+
or outcome.pushed_commit
|
|
255
|
+
or outcome.explanation
|
|
256
|
+
or ""
|
|
257
|
+
)
|
|
258
|
+
logger.info(f"{outcome.branch}\t{outcome.outcome}\t{detail}")
|
|
259
|
+
|
|
260
|
+
def print_promotions(self) -> None:
|
|
261
|
+
"""
|
|
262
|
+
Print every upstream link built this pass, and every spent link label removed.
|
|
263
|
+
"""
|
|
264
|
+
for promotion in self.promoted:
|
|
265
|
+
logger.info(
|
|
266
|
+
f"{promotion.branch}\t#{promotion.pull_request_number}\t{promotion.url}"
|
|
267
|
+
)
|
|
268
|
+
if promotion.body_was_truncated:
|
|
269
|
+
logger.error(
|
|
270
|
+
f"{promotion.branch}: the prefilled description was shortened to "
|
|
271
|
+
f"fit the URL limit"
|
|
272
|
+
)
|
|
273
|
+
for branch in self.promotion_labels_cleared:
|
|
274
|
+
logger.info(f"{branch}\tlink-label-cleared\t")
|
|
275
|
+
|
|
276
|
+
def print_upstream_pull_request_synchronization_changes(self) -> None:
|
|
277
|
+
"""
|
|
278
|
+
Print every in-review label and upstream link written this pass.
|
|
279
|
+
"""
|
|
280
|
+
for change in self.upstream_pull_request_synchronization_changes:
|
|
281
|
+
logger.info(
|
|
282
|
+
f"{change.branch}\t#{change.pull_request_number}\t{change.action}"
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
def print_skipped(self) -> None:
|
|
286
|
+
"""
|
|
287
|
+
Print every pull request the pass left alone, with why and where its head lives.
|
|
288
|
+
"""
|
|
289
|
+
for skipped in self.skipped:
|
|
290
|
+
pull_request = skipped.pull_request
|
|
291
|
+
logger.info(
|
|
292
|
+
f"{pull_request.head}\t#{pull_request.number}\t{skipped.reason}"
|
|
293
|
+
f"\t{pull_request.head_repository or ''}"
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
def print_missing_on_fork(self) -> None:
|
|
297
|
+
"""
|
|
298
|
+
Warn about each branch the board names that the fork does not have.
|
|
299
|
+
"""
|
|
300
|
+
for branch in self.fork_refresh.missing_branches:
|
|
301
|
+
logger.warning(
|
|
302
|
+
f"{branch}: the board names it but the fork does not have it, so it "
|
|
303
|
+
f"was not refreshed and its state may be stale"
|
|
304
|
+
)
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The restack itself: which steps a branch is put through, in which order, and where.
|
|
3
|
+
|
|
4
|
+
The order is the procedure, so it is stated here rather than discovered - a branch is
|
|
5
|
+
published only once its move has been checked. The steps run in a worktree of their own
|
|
6
|
+
so that switching branches never takes the pass's own tooling away from it.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import tempfile
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import TYPE_CHECKING
|
|
15
|
+
|
|
16
|
+
from basstler.git.commands import GitCommandRunner
|
|
17
|
+
from basstler.github.repository_access import ForkPullRequests
|
|
18
|
+
from basstler.maintenance.restack_steps import (
|
|
19
|
+
BranchOutcome,
|
|
20
|
+
BranchUnderRestack,
|
|
21
|
+
ConflictResponse,
|
|
22
|
+
IntegrateParent,
|
|
23
|
+
LeaveUnpublishedLocalCommits,
|
|
24
|
+
PublishBranch,
|
|
25
|
+
RefuseAnUnsafeMove,
|
|
26
|
+
ReportAParentThatIsGone,
|
|
27
|
+
RestackOutcome,
|
|
28
|
+
RestackStep,
|
|
29
|
+
SkipBranchAlreadyCurrent,
|
|
30
|
+
WithholdBranchStillConflicting,
|
|
31
|
+
)
|
|
32
|
+
from basstler.stack.commit_move import BranchAncestry, CommitMoveChecks
|
|
33
|
+
from basstler.stack.model import Stack
|
|
34
|
+
|
|
35
|
+
if TYPE_CHECKING:
|
|
36
|
+
from types import TracebackType
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# %% the order that is the procedure
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def restack_steps() -> tuple[RestackStep, ...]:
|
|
43
|
+
"""
|
|
44
|
+
Every step a branch is put through, in the order that is the procedure.
|
|
45
|
+
|
|
46
|
+
Unlike :meth:`basstler.maintenance.commands.MaintenanceCommand.every_command`, these are
|
|
47
|
+
listed rather than found from their own subclasses: a branch is published only once
|
|
48
|
+
its move has been checked, so this order is a decision about what a pass does, not
|
|
49
|
+
bookkeeping. Stating it here keeps it where it is read, rather than making it a
|
|
50
|
+
consequence of where the classes happen to be defined.
|
|
51
|
+
|
|
52
|
+
:return: The steps, in the order a branch is put through them.
|
|
53
|
+
"""
|
|
54
|
+
return (
|
|
55
|
+
WithholdBranchStillConflicting(),
|
|
56
|
+
ReportAParentThatIsGone(),
|
|
57
|
+
SkipBranchAlreadyCurrent(),
|
|
58
|
+
LeaveUnpublishedLocalCommits(),
|
|
59
|
+
IntegrateParent(),
|
|
60
|
+
RefuseAnUnsafeMove(),
|
|
61
|
+
PublishBranch(),
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass
|
|
66
|
+
class RestackConcludedNothingError(RuntimeError):
|
|
67
|
+
"""
|
|
68
|
+
Raised when no step concluded a branch, which the last step always must.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
branch: str
|
|
72
|
+
"""
|
|
73
|
+
The branch left without an outcome.
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
def __str__(self) -> str:
|
|
77
|
+
""":return: Which branch was left unconcluded."""
|
|
78
|
+
return f"no restack step concluded '{self.branch}'"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
# %% where the branch switching happens
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@dataclass(frozen=True)
|
|
85
|
+
class DetachedCheckout:
|
|
86
|
+
"""
|
|
87
|
+
The invoking checkout, detached so its branch can be restacked elsewhere.
|
|
88
|
+
|
|
89
|
+
git refuses to check one branch out in two worktrees at once, and the caller of a
|
|
90
|
+
pass is usually sitting on a branch of the stack. Detaching releases the name while
|
|
91
|
+
changing nothing else - same commit, same files, same work in progress - and the
|
|
92
|
+
branch is checked out again afterwards, which is also how the caller picks up a
|
|
93
|
+
restack of their own branch.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
git: GitCommandRunner
|
|
97
|
+
"""
|
|
98
|
+
The invoking checkout.
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
branch: str
|
|
102
|
+
"""
|
|
103
|
+
The branch it was on, empty when it was already detached.
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
@classmethod
|
|
107
|
+
def of(cls, git: GitCommandRunner) -> DetachedCheckout:
|
|
108
|
+
""":param git: The checkout to detach.
|
|
109
|
+
:return: The detachment, to be used as a context manager so it is undone."""
|
|
110
|
+
return cls(git, git.checked_out_branch())
|
|
111
|
+
|
|
112
|
+
def __enter__(self) -> DetachedCheckout:
|
|
113
|
+
""":return: This detachment, once the checkout is off its branch."""
|
|
114
|
+
if self.branch:
|
|
115
|
+
self.git.run("checkout", "--quiet", "--detach")
|
|
116
|
+
return self
|
|
117
|
+
|
|
118
|
+
def __exit__(
|
|
119
|
+
self,
|
|
120
|
+
exception_type: type[BaseException] | None,
|
|
121
|
+
exception: BaseException | None,
|
|
122
|
+
traceback: TracebackType | None,
|
|
123
|
+
) -> None:
|
|
124
|
+
"""
|
|
125
|
+
Put the checkout back on the branch it was on.
|
|
126
|
+
|
|
127
|
+
Attempted rather than depended on, so failing to restore never replaces the
|
|
128
|
+
exception on its way out; the checkout is then left detached at the commit it
|
|
129
|
+
started from, with everything it was carrying.
|
|
130
|
+
"""
|
|
131
|
+
if self.branch:
|
|
132
|
+
self.git.attempt("checkout", "--quiet", self.branch)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@dataclass(frozen=True)
|
|
136
|
+
class RestackWorktree:
|
|
137
|
+
"""
|
|
138
|
+
A checkout of its own for the branch switching a restack does.
|
|
139
|
+
|
|
140
|
+
Every step of the pass shells out to this file, which is tracked content in the
|
|
141
|
+
checkout the pass is invoked from. Most branches in a stack were cut before that
|
|
142
|
+
tooling landed, so checking one out there deletes the tooling the rest of the pass
|
|
143
|
+
needs and leaves the caller on a branch that is not theirs. This worktree is added
|
|
144
|
+
outside the project instead, out of reach of the branches a restack switches to.
|
|
145
|
+
|
|
146
|
+
Its refs are the same refs, so a branch it moves is moved for the whole repository.
|
|
147
|
+
"""
|
|
148
|
+
|
|
149
|
+
git: GitCommandRunner
|
|
150
|
+
"""
|
|
151
|
+
The runner every branch switch of a restack goes through.
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
origin: GitCommandRunner
|
|
155
|
+
"""
|
|
156
|
+
The invoking checkout, which the worktree is added to and removed from.
|
|
157
|
+
"""
|
|
158
|
+
|
|
159
|
+
@classmethod
|
|
160
|
+
def added_to(cls, origin: GitCommandRunner) -> RestackWorktree:
|
|
161
|
+
"""
|
|
162
|
+
Add a worktree, detached at whatever the invoking checkout has.
|
|
163
|
+
|
|
164
|
+
:param origin: The checkout to add it to.
|
|
165
|
+
:return: The worktree, to be used as a context manager so it is removed again.
|
|
166
|
+
"""
|
|
167
|
+
path = Path(tempfile.mkdtemp(prefix="stack-restack-"))
|
|
168
|
+
origin.run("worktree", "add", "--quiet", "--detach", str(path), "HEAD")
|
|
169
|
+
return cls(GitCommandRunner(working_directory=path), origin)
|
|
170
|
+
|
|
171
|
+
def __enter__(self) -> GitCommandRunner:
|
|
172
|
+
""":return: The runner to restack through."""
|
|
173
|
+
return self.git
|
|
174
|
+
|
|
175
|
+
def __exit__(
|
|
176
|
+
self,
|
|
177
|
+
exception_type: type[BaseException] | None,
|
|
178
|
+
exception: BaseException | None,
|
|
179
|
+
traceback: TracebackType | None,
|
|
180
|
+
) -> None:
|
|
181
|
+
"""
|
|
182
|
+
Remove the worktree, whether the restack finished or was abandoned.
|
|
183
|
+
|
|
184
|
+
Removal is attempted rather than depended on, so a failure to tidy up never
|
|
185
|
+
replaces the exception that is on its way out.
|
|
186
|
+
"""
|
|
187
|
+
self.origin.attempt(
|
|
188
|
+
"worktree", "remove", "--force", str(self.git.working_directory)
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
# %% the restack itself
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def restack(
|
|
196
|
+
stack: Stack,
|
|
197
|
+
git: GitCommandRunner,
|
|
198
|
+
fork: ForkPullRequests,
|
|
199
|
+
stacked_on: str | None = None,
|
|
200
|
+
) -> list[BranchOutcome]:
|
|
201
|
+
"""
|
|
202
|
+
Put every branch whose parent moved through :func:`restack_steps`, bottom up.
|
|
203
|
+
|
|
204
|
+
A branch above one whose outcome :attr:`RestackOutcome.blocks_branches_above` is not
|
|
205
|
+
attempted: its parent does not hold what the pass meant it to, so it is reported
|
|
206
|
+
blocked by that branch.
|
|
207
|
+
|
|
208
|
+
The steps run in a :class:`RestackWorktree` rather than in the invoking checkout,
|
|
209
|
+
which lends its branch through a :class:`DetachedCheckout` and gets it back with its
|
|
210
|
+
own files still in place. The worktree goes first so it is gone before the branch is
|
|
211
|
+
wanted again.
|
|
212
|
+
|
|
213
|
+
Naming a branch in *stacked_on* carries whatever that branch now holds into the
|
|
214
|
+
branches stacked on it, and leaves the rest of the board alone. It also decides who
|
|
215
|
+
answers a collision: naming a subtree says the caller is present and will judge it,
|
|
216
|
+
where a whole-board pass has to tell each branch's owner - see
|
|
217
|
+
:class:`basstler.maintenance.restack_steps.ConflictResponse`.
|
|
218
|
+
|
|
219
|
+
:param stack: The derived stack, whose plan this executes.
|
|
220
|
+
:param git: The runner naming the checkout to add the worktree to.
|
|
221
|
+
:param fork: The fork, read for conflict state and written to when reporting.
|
|
222
|
+
:param stacked_on: The branch whose subtree to restack, or ``None`` for every branch.
|
|
223
|
+
:return: One outcome per branch in the plan, parent before child.
|
|
224
|
+
"""
|
|
225
|
+
conflict_response = ConflictResponse.chosen_for(stacked_on)
|
|
226
|
+
with DetachedCheckout.of(git), RestackWorktree.added_to(git) as switching:
|
|
227
|
+
checks = CommitMoveChecks(
|
|
228
|
+
stack=stack,
|
|
229
|
+
checked_out_branch="",
|
|
230
|
+
is_ancestor=BranchAncestry(stack.configuration, switching).is_ancestor,
|
|
231
|
+
)
|
|
232
|
+
by_name = {branch.name: branch for branch in stack.branches}
|
|
233
|
+
blocking_ancestors: dict[str, str] = {}
|
|
234
|
+
outcomes = []
|
|
235
|
+
for entry in stack.restack_plan(stacked_on):
|
|
236
|
+
restacking = BranchUnderRestack(
|
|
237
|
+
branch=by_name[entry.branch],
|
|
238
|
+
parent=entry.parent,
|
|
239
|
+
strategy=entry.strategy,
|
|
240
|
+
stack=stack,
|
|
241
|
+
git=switching,
|
|
242
|
+
fork=fork,
|
|
243
|
+
checks=checks,
|
|
244
|
+
conflict_response=conflict_response,
|
|
245
|
+
)
|
|
246
|
+
outcome = _restack_branch(restacking, blocking_ancestors.get(entry.parent))
|
|
247
|
+
if outcome.outcome.blocks_branches_above:
|
|
248
|
+
blocking_ancestors[entry.branch] = (
|
|
249
|
+
outcome.blocking_ancestor or entry.branch
|
|
250
|
+
)
|
|
251
|
+
outcomes.append(outcome)
|
|
252
|
+
return outcomes
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _restack_branch(
|
|
256
|
+
restacking: BranchUnderRestack, blocking_ancestor: str | None
|
|
257
|
+
) -> BranchOutcome:
|
|
258
|
+
""":param restacking: The branch to restack.
|
|
259
|
+
:param blocking_ancestor: The branch below it this pass left without its parent's
|
|
260
|
+
tip, if any.
|
|
261
|
+
:return: A blocked outcome naming that branch when there is one, otherwise the
|
|
262
|
+
outcome of the first step that concluded it.
|
|
263
|
+
:raises RestackConcludedNothingError: If no step did."""
|
|
264
|
+
if blocking_ancestor is not None:
|
|
265
|
+
return restacking.concluded(
|
|
266
|
+
RestackOutcome.BLOCKED_BY_ANCESTOR,
|
|
267
|
+
blocking_ancestor=blocking_ancestor,
|
|
268
|
+
explanation=f"not attempted: `{blocking_ancestor}` below it was left "
|
|
269
|
+
"unpublished this pass",
|
|
270
|
+
)
|
|
271
|
+
local_before = restacking.git.local_commit(restacking.branch.name)
|
|
272
|
+
for step in restack_steps():
|
|
273
|
+
outcome = step.attempt(restacking)
|
|
274
|
+
if outcome is None:
|
|
275
|
+
continue
|
|
276
|
+
if outcome.outcome is not RestackOutcome.PUSHED:
|
|
277
|
+
restacking.git.restore_branch(restacking.branch.name, local_before)
|
|
278
|
+
return outcome
|
|
279
|
+
raise RestackConcludedNothingError(restacking.branch.name)
|