Skip to main content

git_proc/
reset.rs

1//! `git reset` command builder.
2//!
3//! Uses the same typestate as `checkout` for the optional commit-ish target,
4//! but `Build`/`status` are available in **both** states because `git reset`
5//! without a target is valid (defaults to `HEAD`).
6
7use std::marker::PhantomData;
8use std::path::Path;
9
10use crate::commit_ish::{CommitIsh, NoTarget, WithTarget};
11
12/// Create a new `git reset` command builder.
13#[must_use]
14pub fn new() -> Reset<'static, NoTarget> {
15    Reset {
16        base: crate::RepoBase::default(),
17        hard: false,
18        state: NoTarget,
19        _phantom: PhantomData,
20    }
21}
22
23/// Builder for `git reset` command.
24///
25/// See `git reset --help` for full documentation.
26#[derive(Debug)]
27pub struct Reset<'a, S> {
28    base: crate::RepoBase<'a>,
29    hard: bool,
30    state: S,
31    _phantom: PhantomData<&'a ()>,
32}
33
34impl<'a, S> Reset<'a, S> {
35    /// Set the repository path (`-C <path>`).
36    #[must_use]
37    pub fn repo_path(mut self, path: &'a Path) -> Self {
38        self.base.repo_path = Some(path);
39        self
40    }
41
42    /// Set the [`EnvPolicy`](crate::EnvPolicy) controlling which inherited
43    /// `GIT_*` variables are cleared before spawning `git`.
44    #[must_use]
45    pub fn env_policy(mut self, policy: crate::EnvPolicy) -> Self {
46        self.base.env_policy = policy;
47        self
48    }
49
50    crate::flag_methods! {
51        /// Reset the index and working tree.
52        ///
53        /// Corresponds to `--hard`.
54        pub fn hard / hard_if, hard, "Conditionally reset the index and working tree."
55    }
56}
57
58impl<'a, S> crate::RepoPath<'a> for Reset<'a, S> {
59    fn repo_path(self, path: &'a Path) -> Self {
60        self.repo_path(path)
61    }
62}
63
64impl<'a> Reset<'a, NoTarget> {
65    /// Select the commit-ish to reset to.
66    ///
67    /// Calling this once locks the target in — selecting a second is a
68    /// compile error.
69    #[must_use]
70    pub fn commit_ish(self, commit_ish: impl Into<CommitIsh<'a>>) -> Reset<'a, WithTarget<'a>> {
71        Reset {
72            base: self.base,
73            hard: self.hard,
74            state: WithTarget {
75                target: commit_ish.into(),
76            },
77            _phantom: PhantomData,
78        }
79    }
80}
81
82impl<'a, S> Reset<'a, S>
83where
84    Self: crate::Build,
85{
86    /// Execute the command and return the exit status.
87    pub async fn status(self) -> Result<(), crate::Error> {
88        Ok(crate::Build::build(self)?.status().await?)
89    }
90}
91
92impl crate::Build for Reset<'_, NoTarget> {
93    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
94        Ok(self
95            .base
96            .command()?
97            .argument("reset")
98            .optional_flag(self.hard, "--hard"))
99    }
100}
101
102impl<'a> crate::Build for Reset<'a, WithTarget<'a>> {
103    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
104        Ok(self
105            .base
106            .command()?
107            .argument("reset")
108            .optional_flag(self.hard, "--hard")
109            .argument(self.state.target))
110    }
111}
112
113#[cfg(feature = "test-utils")]
114impl<'a> Reset<'a, NoTarget> {
115    /// Compare the built command with another command using debug representation.
116    pub fn test_eq(&self, other: &cmd_proc::Command) {
117        let command = crate::Build::build(Self {
118            base: self.base,
119            hard: self.hard,
120            state: NoTarget,
121            _phantom: PhantomData,
122        })
123        .unwrap();
124        command.test_eq(other);
125    }
126}
127
128#[cfg(feature = "test-utils")]
129impl<'a> Reset<'a, WithTarget<'a>> {
130    /// Compare the built command with another command using debug representation.
131    pub fn test_eq(&self, other: &cmd_proc::Command) {
132        let command = crate::Build::build(Self {
133            base: self.base,
134            hard: self.hard,
135            state: WithTarget {
136                target: self.state.target,
137            },
138            _phantom: PhantomData,
139        })
140        .unwrap();
141        command.test_eq(other);
142    }
143}