1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! `git rev-parse` — pick out and massage parameters.
//!
//! `rev-parse` is the swiss army knife of git plumbing: resolve refs to SHAs,
//! query the `.git` directory, show the top-level, check whether the cwd is
//! inside a working tree, etc. This wrapper exposes the common modes and
//! returns stdout trimmed as [`String`] so callers can parse as needed.
//!
//! ```no_run
//! use git_spawn::{GitCommand, RevParseCommand};
//!
//! # async fn example() -> git_spawn::Result<()> {
//! let mut cmd = RevParseCommand::new();
//! cmd.arg_str("HEAD").current_dir("/some/repo");
//! let sha = cmd.execute().await?;
//! println!("HEAD -> {sha}");
//! # Ok(())
//! # }
//! ```
use crate::command::{CommandExecutor, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
/// Builder for `git rev-parse`.
#[derive(Debug, Clone, Default)]
pub struct RevParseCommand {
/// Shared executor.
pub executor: CommandExecutor,
/// Arguments / refs / flags to pass to `rev-parse`.
pub rev_args: Vec<String>,
/// `--verify`.
pub verify: bool,
/// `--abbrev-ref`.
pub abbrev_ref: bool,
/// `--short[=N]`.
pub short: Option<Option<u32>>,
/// `--show-toplevel`.
pub show_toplevel: bool,
/// `--git-dir`.
pub git_dir: bool,
/// `--is-inside-work-tree`.
pub is_inside_work_tree: bool,
/// `--is-bare-repository`.
pub is_bare_repository: bool,
/// `--absolute-git-dir`.
pub absolute_git_dir: bool,
}
impl RevParseCommand {
/// New empty `rev-parse` command.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Add a ref/rev string (e.g. `"HEAD"`, `"main"`, `"HEAD~3"`).
pub fn arg_str(&mut self, s: impl Into<String>) -> &mut Self {
self.rev_args.push(s.into());
self
}
/// `--verify`: error if the argument is not a valid object.
pub fn verify(&mut self) -> &mut Self {
self.verify = true;
self
}
/// `--abbrev-ref`: print the short ref name.
pub fn abbrev_ref(&mut self) -> &mut Self {
self.abbrev_ref = true;
self
}
/// `--short` with default length.
pub fn short(&mut self) -> &mut Self {
self.short = Some(None);
self
}
/// `--short=N`.
pub fn short_len(&mut self, n: u32) -> &mut Self {
self.short = Some(Some(n));
self
}
/// `--show-toplevel`.
pub fn show_toplevel(&mut self) -> &mut Self {
self.show_toplevel = true;
self
}
/// `--git-dir`.
pub fn git_dir(&mut self) -> &mut Self {
self.git_dir = true;
self
}
/// `--absolute-git-dir`.
pub fn absolute_git_dir(&mut self) -> &mut Self {
self.absolute_git_dir = true;
self
}
/// `--is-inside-work-tree`.
pub fn is_inside_work_tree(&mut self) -> &mut Self {
self.is_inside_work_tree = true;
self
}
/// `--is-bare-repository`.
pub fn is_bare_repository(&mut self) -> &mut Self {
self.is_bare_repository = true;
self
}
}
#[async_trait]
impl GitCommand for RevParseCommand {
/// Trimmed stdout — typically a SHA, path, or `true`/`false` string.
type Output = String;
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["rev-parse".to_string()];
if self.verify {
args.push("--verify".into());
}
if self.abbrev_ref {
args.push("--abbrev-ref".into());
}
match self.short {
Some(None) => args.push("--short".into()),
Some(Some(n)) => args.push(format!("--short={n}")),
None => {}
}
if self.show_toplevel {
args.push("--show-toplevel".into());
}
if self.git_dir {
args.push("--git-dir".into());
}
if self.absolute_git_dir {
args.push("--absolute-git-dir".into());
}
if self.is_inside_work_tree {
args.push("--is-inside-work-tree".into());
}
if self.is_bare_repository {
args.push("--is-bare-repository".into());
}
args.extend(self.rev_args.iter().cloned());
args
}
async fn execute(&self) -> Result<String> {
let out = self.execute_raw().await?;
Ok(out.stdout_trimmed().to_string())
}
}