1#[must_use]
3pub fn new() -> RevParse<'static> {
4 RevParse::new()
5}
6
7#[derive(Debug)]
11pub struct RevParse<'a> {
12 base: crate::RepoBase<'a>,
13 abbrev_ref: bool,
14 symbolic_full_name: bool,
15 verify: bool,
16 quiet: bool,
17 rev: Option<&'a str>,
18}
19
20crate::impl_repo_base!(RevParse);
21
22impl<'a> RevParse<'a> {
23 #[must_use]
24 fn new() -> Self {
25 Self {
26 base: crate::RepoBase::default(),
27 abbrev_ref: false,
28 symbolic_full_name: false,
29 verify: false,
30 quiet: false,
31 rev: None,
32 }
33 }
34
35 crate::flag_methods! {
36 pub fn abbrev_ref / abbrev_ref_if, abbrev_ref, "Conditionally output short ref name."
40 }
41
42 crate::flag_methods! {
43 pub fn symbolic_full_name / symbolic_full_name_if, symbolic_full_name, "Conditionally output full symbolic ref name."
47 }
48
49 crate::flag_methods! {
50 pub fn verify / verify_if, verify, "Conditionally verify that the revision resolves."
54 }
55
56 crate::flag_methods! {
57 pub fn quiet / quiet_if, quiet, "Conditionally suppress error output."
62 }
63
64 #[must_use]
66 pub fn rev(mut self, rev: &'a str) -> Self {
67 self.rev = Some(rev);
68 self
69 }
70}
71
72impl Default for RevParse<'_> {
73 fn default() -> Self {
74 Self::new()
75 }
76}
77
78impl crate::Build for RevParse<'_> {
79 fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
80 Ok(self
81 .base
82 .command()?
83 .argument("rev-parse")
84 .optional_flag(self.abbrev_ref, "--abbrev-ref")
85 .optional_flag(self.symbolic_full_name, "--symbolic-full-name")
86 .optional_flag(self.verify, "--verify")
87 .optional_flag(self.quiet, "--quiet")
88 .optional_argument(self.rev))
89 }
90}
91
92#[cfg(feature = "test-utils")]
93impl RevParse<'_> {
94 pub fn test_eq(&self, other: &cmd_proc::Command) {
98 let command = crate::Build::build(Self {
99 base: self.base,
100 abbrev_ref: self.abbrev_ref,
101 symbolic_full_name: self.symbolic_full_name,
102 verify: self.verify,
103 quiet: self.quiet,
104 rev: self.rev,
105 })
106 .unwrap();
107 command.test_eq(other);
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114 use crate::Build;
115
116 #[tokio::test]
117 async fn test_rev_parse_head() {
118 let output = RevParse::new()
119 .rev("HEAD")
120 .build()
121 .unwrap()
122 .stdout_capture()
123 .string()
124 .await
125 .unwrap();
126 assert!(!output.trim().is_empty());
127 }
128
129 #[tokio::test]
130 async fn test_rev_parse_abbrev_ref() {
131 let output = RevParse::new()
132 .abbrev_ref()
133 .rev("HEAD")
134 .build()
135 .unwrap()
136 .stdout_capture()
137 .string()
138 .await
139 .unwrap();
140 assert!(!output.trim().is_empty());
141 }
142
143 #[cfg(feature = "test-utils")]
144 #[test]
145 fn test_rev_parse_verify_quiet_command() {
146 let expected = cmd_proc::Command::new("git")
147 .argument("rev-parse")
148 .argument("--verify")
149 .argument("--quiet")
150 .argument("refs/remotes/origin/HEAD");
151
152 RevParse::new()
153 .verify()
154 .quiet()
155 .rev("refs/remotes/origin/HEAD")
156 .test_eq(&expected);
157 }
158}