Skip to main content

git_stk/commands/
adopt.rs

1use anyhow::{Context, Result};
2use clap::ArgAction;
3use clap_complete::engine::ArgValueCompleter;
4
5use crate::commands::Run;
6use crate::completions;
7
8/// Attach an existing branch to a parent. With no arguments, adopts the
9/// branch you are on onto the trunk.
10#[derive(Debug, clap::Args)]
11pub struct Adopt {
12    /// The branch to adopt (defaults to the current branch).
13    #[arg(add = ArgValueCompleter::new(completions::branch_candidates))]
14    branch: Option<String>,
15    /// The stack parent (defaults to the trunk).
16    #[arg(long, add = ArgValueCompleter::new(completions::branch_candidates))]
17    parent: Option<String>,
18    /// Print what would change without writing any metadata.
19    #[arg(long, short = 'n', action = ArgAction::SetTrue)]
20    dry_run: bool,
21}
22
23impl Run for Adopt {
24    fn run(self) -> Result<()> {
25        let branch = match self.branch {
26            Some(branch) => branch,
27            None => crate::git::current_branch()?,
28        };
29        let parent = match self.parent {
30            Some(parent) => parent,
31            None => crate::stack::trunk_branch(&crate::git::local_branches()?)
32                .context("could not detect the trunk branch; pass --parent")?,
33        };
34        crate::stack::adopt_branch(&branch, &parent, self.dry_run)
35    }
36}