Skip to main content

git_proc/
commit.rs

1use std::ffi::OsStr;
2
3/// Create a new `git commit` command builder.
4#[must_use]
5pub fn new() -> Commit<'static> {
6    Commit::new()
7}
8
9/// Builder for `git commit` command.
10///
11/// See `git commit --help` for full documentation.
12#[derive(Debug)]
13pub struct Commit<'a> {
14    base: crate::RepoBase<'a>,
15    message: Option<&'a str>,
16    author: Option<&'a str>,
17    date: Option<&'a str>,
18    allow_empty: bool,
19    allow_empty_message: bool,
20    porcelain: bool,
21    env_vars: Vec<(cmd_proc::EnvVariableName, &'a OsStr)>,
22}
23
24crate::impl_repo_base!(Commit);
25crate::impl_porcelain!(Commit);
26
27impl<'a> Commit<'a> {
28    #[must_use]
29    fn new() -> Self {
30        Self {
31            base: crate::RepoBase::default(),
32            message: None,
33            author: None,
34            date: None,
35            allow_empty: false,
36            allow_empty_message: false,
37            porcelain: false,
38            env_vars: Vec::new(),
39        }
40    }
41
42    /// Set the commit message.
43    ///
44    /// Corresponds to `--message` or `-m`.
45    #[must_use]
46    pub fn message(mut self, message: &'a str) -> Self {
47        self.message = Some(message);
48        self
49    }
50
51    /// Set the commit author.
52    ///
53    /// Corresponds to `--author`. Format: `Name <email>`.
54    #[must_use]
55    pub fn author(mut self, author: &'a str) -> Self {
56        self.author = Some(author);
57        self
58    }
59
60    /// Set the author date.
61    ///
62    /// Corresponds to `--date`.
63    #[must_use]
64    pub fn date(mut self, date: &'a str) -> Self {
65        self.date = Some(date);
66        self
67    }
68
69    crate::flag_methods! {
70        /// Allow creating a commit with no changes.
71        ///
72        /// Corresponds to `--allow-empty`.
73        pub fn allow_empty / allow_empty_if, allow_empty, "Conditionally allow creating a commit with no changes."
74    }
75
76    crate::flag_methods! {
77        /// Allow creating a commit with an empty message.
78        ///
79        /// Corresponds to `--allow-empty-message`.
80        pub fn allow_empty_message / allow_empty_message_if, allow_empty_message, "Conditionally allow creating a commit with an empty message."
81    }
82
83    /// Set an environment variable for the command.
84    #[must_use]
85    pub fn env(mut self, key: cmd_proc::EnvVariableName, value: &'a OsStr) -> Self {
86        self.env_vars.push((key, value));
87        self
88    }
89
90    /// Execute the command and return the exit status.
91    pub async fn status(self) -> Result<(), crate::Error> {
92        Ok(crate::Build::build(self)?.status().await?)
93    }
94}
95
96impl Default for Commit<'_> {
97    fn default() -> Self {
98        Self::new()
99    }
100}
101
102impl crate::Build for Commit<'_> {
103    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
104        Ok(self
105            .base
106            .command()?
107            .argument("commit")
108            .optional_option("--message", self.message)
109            .optional_option("--author", self.author)
110            .optional_option("--date", self.date)
111            .optional_flag(self.allow_empty, "--allow-empty")
112            .optional_flag(self.allow_empty_message, "--allow-empty-message")
113            .optional_flag(self.porcelain, "--porcelain")
114            .envs(self.env_vars))
115    }
116}
117
118#[cfg(feature = "test-utils")]
119impl Commit<'_> {
120    /// Compare the built command with another command using debug representation.
121    pub fn test_eq(&self, other: &cmd_proc::Command) {
122        let command = crate::Build::build(Self {
123            base: self.base,
124            message: self.message,
125            author: self.author,
126            date: self.date,
127            allow_empty: self.allow_empty,
128            allow_empty_message: self.allow_empty_message,
129            porcelain: self.porcelain,
130            env_vars: self.env_vars.clone(),
131        })
132        .unwrap();
133        command.test_eq(other);
134    }
135}