Skip to main content

git_proc/
worktree.rs

1use std::path::Path;
2
3/// Create a new `git worktree list` command builder.
4#[must_use]
5pub fn list() -> List<'static> {
6    List::new()
7}
8
9/// Create a new `git worktree add` command builder.
10#[must_use]
11pub fn add(path: &Path) -> Add<'_> {
12    Add::new(path)
13}
14
15/// Create a new `git worktree remove` command builder.
16#[must_use]
17pub fn remove(worktree: &Path) -> Remove<'_> {
18    Remove::new(worktree)
19}
20
21/// Builder for `git worktree list` command.
22///
23/// See `git worktree --help` for full documentation.
24#[derive(Debug)]
25pub struct List<'a> {
26    base: crate::RepoBase<'a>,
27    porcelain: bool,
28}
29
30crate::impl_repo_base!(List);
31crate::impl_porcelain!(List);
32
33impl<'a> List<'a> {
34    #[must_use]
35    fn new() -> Self {
36        Self {
37            base: crate::RepoBase::default(),
38            porcelain: false,
39        }
40    }
41}
42
43impl Default for List<'_> {
44    fn default() -> Self {
45        Self::new()
46    }
47}
48
49impl crate::Build for List<'_> {
50    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
51        Ok(self
52            .base
53            .command()?
54            .argument("worktree")
55            .argument("list")
56            .optional_flag(self.porcelain, "--porcelain"))
57    }
58}
59
60#[cfg(feature = "test-utils")]
61impl List<'_> {
62    /// Compare the built command with another command using debug representation.
63    pub fn test_eq(&self, other: &cmd_proc::Command) {
64        let command = crate::Build::build(Self {
65            base: self.base,
66            porcelain: self.porcelain,
67        })
68        .unwrap();
69        command.test_eq(other);
70    }
71}
72
73/// Builder for `git worktree add` command.
74///
75/// See `git worktree --help` for full documentation.
76#[derive(Debug)]
77pub struct Add<'a> {
78    base: crate::RepoBase<'a>,
79    path: &'a Path,
80    branch: Option<&'a str>,
81    new_branch: Option<&'a str>,
82    commit_ish: Option<&'a str>,
83}
84
85crate::impl_repo_base!(Add);
86
87impl<'a> Add<'a> {
88    #[must_use]
89    fn new(path: &'a Path) -> Self {
90        Self {
91            base: crate::RepoBase::default(),
92            path,
93            branch: None,
94            new_branch: None,
95            commit_ish: None,
96        }
97    }
98
99    /// Checkout existing branch in the new worktree.
100    #[must_use]
101    pub fn branch(mut self, branch: &'a str) -> Self {
102        self.branch = Some(branch);
103        self
104    }
105
106    /// Create a new branch and checkout in the new worktree.
107    ///
108    /// Corresponds to `-b <branch>`.
109    #[must_use]
110    pub fn new_branch(mut self, branch: &'a str) -> Self {
111        self.new_branch = Some(branch);
112        self
113    }
114
115    /// Set the commit-ish to create the worktree from.
116    #[must_use]
117    pub fn commit_ish(mut self, commit_ish: &'a str) -> Self {
118        self.commit_ish = Some(commit_ish);
119        self
120    }
121
122    /// Execute the command and return the exit status.
123    pub async fn status(self) -> Result<(), crate::Error> {
124        Ok(crate::Build::build(self)?.status().await?)
125    }
126}
127
128impl crate::Build for Add<'_> {
129    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
130        Ok(self
131            .base
132            .command()?
133            .argument("worktree")
134            .argument("add")
135            .optional_option("-b", self.new_branch)
136            .argument(self.path)
137            .optional_argument(self.branch)
138            .optional_argument(self.commit_ish))
139    }
140}
141
142#[cfg(feature = "test-utils")]
143impl Add<'_> {
144    /// Compare the built command with another command using debug representation.
145    pub fn test_eq(&self, other: &cmd_proc::Command) {
146        let command = crate::Build::build(Self {
147            base: self.base,
148            path: self.path,
149            branch: self.branch,
150            new_branch: self.new_branch,
151            commit_ish: self.commit_ish,
152        })
153        .unwrap();
154        command.test_eq(other);
155    }
156}
157
158/// Builder for `git worktree remove` command.
159///
160/// See `git worktree --help` for full documentation.
161#[derive(Debug)]
162pub struct Remove<'a> {
163    base: crate::RepoBase<'a>,
164    worktree: &'a Path,
165    force: bool,
166}
167
168crate::impl_repo_base!(Remove);
169
170impl<'a> Remove<'a> {
171    #[must_use]
172    fn new(worktree: &'a Path) -> Self {
173        Self {
174            base: crate::RepoBase::default(),
175            worktree,
176            force: false,
177        }
178    }
179
180    crate::flag_methods! {
181        /// Force removal even if worktree is dirty.
182        ///
183        /// Corresponds to `--force`.
184        pub fn force / force_if, force, "Conditionally force removal."
185    }
186
187    /// Execute the command and return the exit status.
188    pub async fn status(self) -> Result<(), crate::Error> {
189        Ok(crate::Build::build(self)?.status().await?)
190    }
191}
192
193impl crate::Build for Remove<'_> {
194    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
195        Ok(self
196            .base
197            .command()?
198            .argument("worktree")
199            .argument("remove")
200            .optional_flag(self.force, "--force")
201            .argument(self.worktree))
202    }
203}
204
205#[cfg(feature = "test-utils")]
206impl Remove<'_> {
207    /// Compare the built command with another command using debug representation.
208    pub fn test_eq(&self, other: &cmd_proc::Command) {
209        let command = crate::Build::build(Self {
210            base: self.base,
211            worktree: self.worktree,
212            force: self.force,
213        })
214        .unwrap();
215        command.test_eq(other);
216    }
217}