Skip to main content

git_proc/
checkout.rs

1//! `git checkout` command builder using a typestate to enforce that a target
2//! is chosen before the command can be executed.
3//!
4//! # Example
5//!
6//! ```
7//! use git_proc::{Build, branch::Branch, checkout};
8//! let main = Branch::from_static_or_panic("main");
9//! let _cmd = checkout::new().commit_ish(&main).build();
10//! ```
11//!
12//! # Typestate guarantees
13//!
14//! Executing without a target is a compile error:
15//!
16//! ```compile_fail
17//! use git_proc::{Build, checkout};
18//! let _cmd = checkout::new().build();
19//! ```
20//!
21//! Selecting more than one target is a compile error:
22//!
23//! ```compile_fail
24//! use git_proc::{branch::Branch, checkout, tag::Tag};
25//! let b = Branch::from_static_or_panic("main");
26//! let t = Tag::from_static_or_panic("v1.0.0");
27//! let _ = checkout::new().commit_ish(&b).commit_ish(&t);
28//! ```
29
30use std::marker::PhantomData;
31use std::path::Path;
32
33use crate::commit_ish::{CommitIsh, NoTarget, WithTarget};
34
35/// Create a new `git checkout` command builder.
36#[must_use]
37pub fn new() -> Checkout<'static, NoTarget> {
38    Checkout {
39        base: crate::RepoBase::default(),
40        state: NoTarget,
41        _phantom: PhantomData,
42    }
43}
44
45/// Builder for `git checkout`.
46///
47/// `S` encodes whether a target has been chosen. Only
48/// `Checkout<_, WithTarget>` exposes `status()` / `Build`, so failing to
49/// specify a target is a compile error.
50///
51/// See `git checkout --help` for full documentation.
52#[derive(Debug)]
53pub struct Checkout<'a, S> {
54    base: crate::RepoBase<'a>,
55    state: S,
56    _phantom: PhantomData<&'a ()>,
57}
58
59impl<'a, S> Checkout<'a, S> {
60    /// Set the repository path (`-C <path>`).
61    #[must_use]
62    pub fn repo_path(mut self, path: &'a Path) -> Self {
63        self.base.repo_path = Some(path);
64        self
65    }
66
67    /// Set the [`EnvPolicy`](crate::EnvPolicy) controlling which inherited
68    /// `GIT_*` variables are cleared before spawning `git`.
69    #[must_use]
70    pub fn env_policy(mut self, policy: crate::EnvPolicy) -> Self {
71        self.base.env_policy = policy;
72        self
73    }
74}
75
76impl<'a, S> crate::RepoPath<'a> for Checkout<'a, S> {
77    fn repo_path(self, path: &'a Path) -> Self {
78        self.repo_path(path)
79    }
80}
81
82impl<'a> Checkout<'a, NoTarget> {
83    /// Select the commit-ish to check out.
84    #[must_use]
85    pub fn commit_ish(self, commit_ish: impl Into<CommitIsh<'a>>) -> Checkout<'a, WithTarget<'a>> {
86        Checkout {
87            base: self.base,
88            state: WithTarget {
89                target: commit_ish.into(),
90            },
91            _phantom: PhantomData,
92        }
93    }
94}
95
96impl<'a> Checkout<'a, WithTarget<'a>> {
97    /// Execute the command and return the exit status.
98    pub async fn status(self) -> Result<(), crate::Error> {
99        Ok(crate::Build::build(self)?.status().await?)
100    }
101}
102
103impl<'a> crate::Build for Checkout<'a, WithTarget<'a>> {
104    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
105        Ok(self
106            .base
107            .command()?
108            .argument("checkout")
109            .argument(self.state.target))
110    }
111}
112
113#[cfg(feature = "test-utils")]
114impl<'a> Checkout<'a, WithTarget<'a>> {
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            state: WithTarget {
120                target: self.state.target,
121            },
122            _phantom: PhantomData,
123        })
124        .unwrap();
125        command.test_eq(other);
126    }
127}