stax 0.96.2

Fast stacked Git branches and PRs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#![allow(clippy::result_large_err)]

use super::operation::report_operation;
use super::{
    OperationError, OperationErrorDetails, OperationErrorKind, OperationEvent, OperationOutcome,
    OperationProgress, OperationReceipt, OperationReporter, OperationRequest, OperationResult,
    OperationSideEffects, OperationStage, OperationWarning, RepositorySession, TransactionSummary,
};
use crate::application::repository::MutationTargets;
use crate::engine::{BranchMetadata, Stack};
use crate::git::GitRepo;
use crate::ops::receipt::OpKind;
use crate::ops::tx::Transaction;

impl RepositorySession {
    /// Delete a local branch while preserving its descendants unchanged.
    pub fn delete_branch(
        &self,
        branch: &str,
        force: bool,
        reporter: &mut dyn OperationReporter,
    ) -> OperationResult {
        let request = OperationRequest::DeleteBranch {
            branch: branch.to_owned(),
            force,
        };
        report_operation(request.clone(), reporter, |reporter| {
            self.delete_branch_unframed(&request, branch, force, reporter)
        })
    }

    pub(super) fn delete_branch_unframed(
        &self,
        request: &OperationRequest,
        branch: &str,
        force: bool,
        reporter: &mut dyn OperationReporter,
    ) -> OperationResult {
        self.with_mutation(
            request,
            MutationTargets::branches([branch.to_string()]),
            || delete_branch_inner(self, request, branch, force, reporter),
        )
    }
}

fn delete_branch_inner(
    session: &RepositorySession,
    request: &OperationRequest,
    branch: &str,
    force: bool,
    reporter: &mut dyn OperationReporter,
) -> OperationResult {
    let branch = branch.trim();
    if branch.is_empty() {
        return Err(operation_error(
            request,
            OperationErrorKind::InvalidInput,
            OperationErrorDetails::None,
            "Branch name is required",
            "Choose a local branch and retry",
            "delete branch input was empty",
            OperationSideEffects::None,
        ));
    }
    let repo = session.open_repo().map_err(|error| {
        source_error(
            request,
            OperationErrorKind::RepositoryUnavailable,
            OperationErrorDetails::None,
            "Could not open the repository",
            "Check the repository path and retry",
            error,
            OperationSideEffects::None,
        )
    })?;
    let trunk = repo.trunk_branch().map_err(|error| {
        source_error(
            request,
            OperationErrorKind::LocalGit,
            OperationErrorDetails::None,
            "Could not determine the trunk branch",
            "Resolve the stax metadata error and retry",
            error,
            OperationSideEffects::None,
        )
    })?;
    if branch == trunk {
        return Err(operation_error(
            request,
            OperationErrorKind::PreconditionFailed,
            OperationErrorDetails::Branch {
                branch: branch.to_string(),
            },
            format!("Cannot delete the trunk branch '{trunk}'"),
            "Choose a non-trunk branch and retry",
            "delete target is the configured trunk branch",
            OperationSideEffects::None,
        ));
    }
    let current = repo.current_branch().map_err(|error| {
        source_error(
            request,
            OperationErrorKind::LocalGit,
            OperationErrorDetails::None,
            "Could not determine the current branch",
            "Resolve the Git HEAD error and retry",
            error,
            OperationSideEffects::None,
        )
    })?;
    if branch == current {
        return Err(operation_error(
            request,
            OperationErrorKind::PreconditionFailed,
            OperationErrorDetails::Branch {
                branch: branch.to_string(),
            },
            "Cannot delete the current branch",
            "Check out a different branch and retry",
            "delete target is checked out in the current worktree",
            OperationSideEffects::None,
        ));
    }
    repo.branch_commit(branch).map_err(|error| {
        source_error(
            request,
            OperationErrorKind::InvalidInput,
            OperationErrorDetails::Branch {
                branch: branch.to_string(),
            },
            format!("Branch '{branch}' does not exist locally"),
            "Choose an existing local branch and retry",
            error,
            OperationSideEffects::None,
        )
    })?;
    if let Some(hint) = repo
        .branch_delete_resolution_hint(branch)
        .map_err(|error| {
            source_error(
                request,
                OperationErrorKind::LocalGit,
                OperationErrorDetails::Branch {
                    branch: branch.to_string(),
                },
                "Could not inspect branch worktrees",
                "Resolve the Git worktree error and retry",
                error,
                OperationSideEffects::None,
            )
        })?
    {
        return Err(operation_error(
            request,
            OperationErrorKind::PreconditionFailed,
            OperationErrorDetails::Branch {
                branch: branch.to_string(),
            },
            format!("Branch '{branch}' is checked out in another worktree"),
            hint,
            "delete target is checked out in a linked worktree",
            OperationSideEffects::None,
        ));
    }

    let metadata = BranchMetadata::read(repo.inner(), branch).map_err(|error| {
        source_error(
            request,
            OperationErrorKind::LocalGit,
            OperationErrorDetails::Branch {
                branch: branch.to_string(),
            },
            format!("Could not read metadata for '{branch}'"),
            "Resolve the stax metadata error and retry",
            error,
            OperationSideEffects::None,
        )
    })?;
    if !force && !is_merged_into_parent_or_trunk(&repo, branch, &trunk, metadata.as_ref()) {
        return Err(operation_error(
            request,
            OperationErrorKind::PreconditionFailed,
            OperationErrorDetails::Branch {
                branch: branch.to_string(),
            },
            format!("Branch '{branch}' is not merged"),
            "Retry with force only if discarding its commits is intended",
            "branch is not an ancestor of its parent or trunk",
            OperationSideEffects::None,
        ));
    }

    let stack = Stack::load(&repo).map_err(|error| {
        source_error(
            request,
            OperationErrorKind::LocalGit,
            OperationErrorDetails::None,
            "Could not load stack metadata",
            "Resolve the stax metadata error and retry",
            error,
            OperationSideEffects::None,
        )
    })?;
    let descendants = stack.descendants(branch);
    let warnings = (!descendants.is_empty())
        .then(|| OperationWarning::DescendantsRetained {
            deleted_branch: branch.to_string(),
            descendants: descendants.clone(),
        })
        .into_iter()
        .collect::<Vec<_>>();

    let mut transaction = Transaction::begin(OpKind::Delete, &repo, true).map_err(|error| {
        source_error(
            request,
            OperationErrorKind::LocalGit,
            OperationErrorDetails::None,
            "Could not start the deletion transaction",
            "Resolve the receipt error and retry",
            error,
            OperationSideEffects::None,
        )
    })?;
    transaction.plan_branch(&repo, branch).map_err(|error| {
        source_error(
            request,
            OperationErrorKind::LocalGit,
            OperationErrorDetails::None,
            "Could not prepare branch recovery for deletion",
            "Resolve the receipt error and retry",
            error,
            OperationSideEffects::None,
        )
    })?;
    transaction
        .plan_metadata_ref(&repo, branch)
        .map_err(|error| {
            source_error(
                request,
                OperationErrorKind::LocalGit,
                OperationErrorDetails::None,
                "Could not prepare metadata recovery for deletion",
                "Resolve the receipt error and retry",
                error,
                OperationSideEffects::None,
            )
        })?;
    transaction.snapshot().map_err(|error| {
        source_error(
            request,
            OperationErrorKind::LocalGit,
            OperationErrorDetails::None,
            "Could not save the deletion recovery snapshot",
            "Resolve the receipt error and retry",
            error,
            OperationSideEffects::None,
        )
    })?;

    reporter.report(OperationEvent::Progress(OperationProgress {
        stage: OperationStage::DeletingBranch,
        completed: 0,
        total: Some(1),
        branch: Some(branch.to_string()),
        message: format!("Deleting {branch}"),
    }));
    if let Err(error) = repo.delete_branch(branch, force) {
        return Err(finish_delete_error(
            request,
            transaction,
            &repo,
            branch,
            &descendants,
            warnings,
            if force {
                OperationErrorKind::LocalGit
            } else {
                OperationErrorKind::PreconditionFailed
            },
            format!("Could not delete branch '{branch}'"),
            "Resolve the Git branch error and retry",
            error,
            "delete-ref",
            OperationSideEffects::None,
        ));
    }
    if let Err(error) = BranchMetadata::delete(repo.inner(), branch) {
        return Err(finish_delete_error(
            request,
            transaction,
            &repo,
            branch,
            &descendants,
            warnings,
            OperationErrorKind::LocalGit,
            "Branch deleted, but its metadata could not be removed",
            "Run `stax undo`, resolve the metadata error, and retry",
            error,
            "delete-metadata",
            OperationSideEffects::RepositoryChanged,
        ));
    }
    if let Err(error) = record_after_states(&mut transaction, &repo, branch) {
        return Err(finish_delete_error(
            request,
            transaction,
            &repo,
            branch,
            &descendants,
            warnings,
            OperationErrorKind::LocalGit,
            "Branch deleted, but its recovery state could not be recorded",
            "Inspect the repository and retry if needed",
            error,
            "record-after",
            OperationSideEffects::RepositoryChanged,
        ));
    }
    transaction.set_head_branch_after(&current);
    let finalized = transaction.finish_ok_preserving_receipt();
    let receipt = delete_receipt(
        request,
        branch,
        &descendants,
        warnings,
        Some(TransactionSummary::from(&finalized.receipt)),
        OperationSideEffects::RepositoryChanged,
    );
    if let Some(error) = finalized.persistence_error {
        return Err(OperationError::from_source(
            request.clone(),
            OperationErrorKind::LocalGit,
            OperationErrorDetails::None,
            "Branch deleted, but its receipt could not be saved",
            "Inspect the repository and retry if needed",
            &error,
            Some(receipt),
            OperationSideEffects::RepositoryChanged,
        ));
    }
    Ok(receipt)
}

fn is_merged_into_parent_or_trunk(
    repo: &GitRepo,
    branch: &str,
    trunk: &str,
    metadata: Option<&BranchMetadata>,
) -> bool {
    let mut candidates = metadata
        .map(|metadata| vec![metadata.parent_branch_name.as_str()])
        .unwrap_or_default();
    if !candidates.contains(&trunk) {
        candidates.push(trunk);
    }
    candidates
        .into_iter()
        .any(|base| repo.is_ancestor(branch, base).unwrap_or(false))
}

fn record_after_states(
    transaction: &mut Transaction,
    repo: &GitRepo,
    branch: &str,
) -> anyhow::Result<()> {
    transaction.record_optional_after(repo, branch)?;
    transaction.record_metadata_ref_after(repo, branch)?;
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn finish_delete_error(
    request: &OperationRequest,
    mut transaction: Transaction,
    repo: &GitRepo,
    branch: &str,
    descendants: &[String],
    warnings: Vec<OperationWarning>,
    kind: OperationErrorKind,
    primary: impl Into<String>,
    action: impl Into<String>,
    source: anyhow::Error,
    failed_step: &'static str,
    side_effects: OperationSideEffects,
) -> OperationError {
    let primary = primary.into();
    let mut diagnostic_chain = format!("{source:#}");
    if let Err(error) = record_after_states(&mut transaction, repo, branch) {
        diagnostic_chain.push_str("\nfailed to record final ref state: ");
        diagnostic_chain.push_str(&format!("{error:#}"));
    }
    let finalized =
        transaction.finish_err_preserving_receipt(&primary, Some(failed_step), Some(branch));
    if let Some(error) = finalized.persistence_error {
        diagnostic_chain.push_str("\nreceipt persistence failure: ");
        diagnostic_chain.push_str(&format!("{error:#}"));
    }
    let receipt = delete_receipt(
        request,
        branch,
        descendants,
        warnings,
        Some(TransactionSummary::from(&finalized.receipt)),
        side_effects,
    );
    OperationError {
        request: request.clone(),
        kind,
        details: OperationErrorDetails::Branch {
            branch: branch.to_string(),
        },
        primary,
        action: action.into(),
        diagnostic_chain,
        receipt: Some(receipt),
        side_effects,
    }
}

fn delete_receipt(
    request: &OperationRequest,
    branch: &str,
    descendants: &[String],
    warnings: Vec<OperationWarning>,
    transaction: Option<TransactionSummary>,
    side_effects: OperationSideEffects,
) -> OperationReceipt {
    let mut affected_branches = vec![branch.to_string()];
    affected_branches.extend(descendants.iter().cloned());
    OperationReceipt {
        request: request.clone(),
        summary: format!("Deleted {branch}"),
        affected_branches,
        outcome: OperationOutcome::BranchDeleted {
            branch: branch.to_string(),
            retained_descendants: descendants.to_vec(),
        },
        transaction,
        warnings,
        side_effects,
    }
}

fn source_error(
    request: &OperationRequest,
    kind: OperationErrorKind,
    details: OperationErrorDetails,
    primary: impl Into<String>,
    action: impl Into<String>,
    source: anyhow::Error,
    side_effects: OperationSideEffects,
) -> OperationError {
    OperationError::from_source(
        request.clone(),
        kind,
        details,
        primary,
        action,
        &source,
        None,
        side_effects,
    )
}

fn operation_error(
    request: &OperationRequest,
    kind: OperationErrorKind,
    details: OperationErrorDetails,
    primary: impl Into<String>,
    action: impl Into<String>,
    diagnostic_chain: impl Into<String>,
    side_effects: OperationSideEffects,
) -> OperationError {
    OperationError {
        request: request.clone(),
        kind,
        details,
        primary: primary.into(),
        action: action.into(),
        diagnostic_chain: diagnostic_chain.into(),
        receipt: None,
        side_effects,
    }
}