1use std::ffi::OsStr;
2
3#[must_use]
5pub fn new() -> Commit<'static> {
6 Commit::new()
7}
8
9#[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 #[must_use]
46 pub fn message(mut self, message: &'a str) -> Self {
47 self.message = Some(message);
48 self
49 }
50
51 #[must_use]
55 pub fn author(mut self, author: &'a str) -> Self {
56 self.author = Some(author);
57 self
58 }
59
60 #[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 pub fn allow_empty / allow_empty_if, allow_empty, "Conditionally allow creating a commit with no changes."
74 }
75
76 crate::flag_methods! {
77 pub fn allow_empty_message / allow_empty_message_if, allow_empty_message, "Conditionally allow creating a commit with an empty message."
81 }
82
83 #[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 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 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}