Skip to main content

git_stk/commands/
new.rs

1use anyhow::Result;
2use clap::ArgAction;
3
4use crate::commands::Run;
5
6/// Create a new child branch from the current branch.
7#[derive(Debug, clap::Args)]
8pub struct New {
9    /// Name for the new child branch.
10    branch: String,
11    /// Insert above the current branch, moving its children onto the new one.
12    #[arg(long, conflicts_with = "prepend")]
13    insert: bool,
14    /// Insert below the current branch, moving it onto the new one.
15    #[arg(long, conflicts_with = "insert")]
16    prepend: bool,
17    /// Create the branch in a worktree of its own instead of checking it out
18    /// here, under `stk.worktreeDir`. git-stk owns the worktrees it creates and
19    /// removes them on cleanup once the branch lands.
20    #[arg(long, conflicts_with_all = ["insert", "prepend"])]
21    worktree: bool,
22    /// Print what would change (the branch, and any retargeted children)
23    /// without creating or moving anything.
24    #[arg(long, short = 'n', action = ArgAction::SetTrue)]
25    dry_run: bool,
26}
27
28impl Run for New {
29    fn run(self) -> Result<()> {
30        if self.worktree {
31            crate::stack::create_branch_in_worktree(&self.branch, self.dry_run)
32        } else if self.insert {
33            crate::stack::insert_branch(&self.branch, self.dry_run)
34        } else if self.prepend {
35            crate::stack::prepend_branch(&self.branch, self.dry_run)
36        } else {
37            crate::stack::create_branch(&self.branch, self.dry_run)
38        }
39    }
40}