1use std::marker::PhantomData;
8use std::path::Path;
9
10use crate::commit_ish::{CommitIsh, NoTarget, WithTarget};
11
12#[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#[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 #[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 #[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 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 #[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 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 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 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}