Skip to main content

git_stk/commands/
adopt.rs

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