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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#![allow(unknown_lints)]
#![warn(clippy::all)]
use posix_errors::{error_from_output, to_posix_error, PosixError};
use std::process::Command;
use std::process::Output;
macro_rules! cmd {
($args:expr) => {
Command::new("git").args($args).output()
};
($name:expr, $args:expr) => {
Command::new("git").arg($name).args($args).output()
};
}
macro_rules! cmd_in_dir {
( $working_dir:expr, $args:expr ) => {
Command::new("git")
.args(&["-C", $working_dir])
.args($args)
.output()
};
($working_dir:expr, $name:expr, $args: expr) => {
Command::new("git")
.args(&["-C", $working_dir])
.arg($name)
.args($args)
.output()
};
}
pub fn git_cmd_out(working_dir: String, args: Vec<&str>) -> Result<Output, PosixError> {
let result = cmd_in_dir!(&working_dir, args);
if let Ok(value) = result {
return Ok(value);
}
Err(to_posix_error(result.unwrap_err()))
}
pub fn git_cmd(args: Vec<&str>) -> Result<Output, PosixError> {
let result = cmd!(args);
if let Ok(value) = result {
return Ok(value);
}
Err(to_posix_error(result.unwrap_err()))
}
pub fn ls_remote(args: &[&str]) -> Result<Output, PosixError> {
let result = cmd!("ls-remote", args);
if let Ok(value) = result {
return Ok(value);
}
Err(to_posix_error(result.unwrap_err()))
}
pub fn tags_from_remote(url: &str) -> Result<Vec<String>, PosixError> {
let mut vec = Vec::new();
let output = ls_remote(&["--refs", "--tags", &url])?;
if output.status.success() {
let tmp = String::from_utf8(output.stdout).unwrap();
for s in tmp.lines() {
let mut split = s.splitn(3, '/');
split.next();
split.next();
let split_result = split.next();
if let Some(value) = split_result {
vec.push(String::from(value));
}
}
Ok(vec)
} else {
Err(error_from_output(output))
}
}
pub fn top_level() -> Result<String, PosixError> {
let output = git_cmd(vec!["rev-parse", "--show-toplevel"])?;
if output.status.success() {
Ok(String::from_utf8(output.stdout)
.unwrap()
.trim_end()
.to_string())
} else {
Err(error_from_output(output))
}
}
pub fn config_set(
working_dir: &str,
file: &str,
key: &str,
value: &str,
) -> Result<bool, PosixError> {
let output = cmd_in_dir!(working_dir, "config", vec!["--file", file, key, value])
.expect("Failed to execute git config");
if output.status.success() {
Ok(true)
} else {
Err(error_from_output(output))
}
}
pub fn sparse_checkout_add(working_dir: &str, pattern: &str) -> Result<bool, PosixError> {
let output = cmd_in_dir!(working_dir, "sparse-checkout", vec!["add", pattern])
.expect("Failed to execute git sparse-checkout");
if output.status.success() {
Ok(true)
} else {
Err(error_from_output(output))
}
}
pub fn is_sparse(working_dir: &str) -> bool {
let output = cmd_in_dir!(working_dir, "config", vec!["core.sparseCheckout"])
.expect("Failed to execute git config");
String::from_utf8(output.stdout).unwrap() == "true"
}
pub fn subtree_add(
working_dir: &str,
prefix: &str,
url: &str,
git_ref: &str,
msg: &str,
) -> Result<bool, PosixError> {
let output = cmd_in_dir!(
working_dir,
"subtree",
vec!["add", "-P", prefix, url, git_ref, "-m", msg]
)
.expect("Failed to execute git subtree");
if output.status.success() {
Ok(true)
} else {
Err(error_from_output(output))
}
}
pub fn subtree_files(working_dir: &str) -> Result<Vec<String>, PosixError> {
let output = git_cmd_out(
working_dir.to_string(),
vec!["ls-files", "--", "*.gitsubtrees"],
)?;
if output.status.success() {
let tmp = String::from_utf8(output.stdout).unwrap();
Ok(tmp.lines().map(str::to_string).collect())
} else {
Err(error_from_output(output))
}
}
pub fn is_working_dir_clean(working_dir: &str) -> Result<bool, PosixError> {
let output = git_cmd_out(working_dir.to_string(), vec!["diff", "--quiet"]);
Ok(output?.status.success())
}
pub fn resolve_head(remote: &str) -> Result<String, PosixError> {
let proc =
cmd!("ls-remote", vec!["--symref", remote, "HEAD"]).expect("Failed to execute git command");
if proc.status.success() {
let stdout = String::from_utf8(proc.stdout).unwrap();
let mut lines = stdout.lines();
let first_line = lines.next().expect("Failed to parse HEAD from remote");
let mut split = first_line
.split('\t')
.next()
.expect("Failed to parse HEAD from remote")
.splitn(3, '/');
split.next();
split.next();
return Ok(split.next().unwrap().to_string());
}
Err(error_from_output(proc))
}
pub fn remote_ref_to_id(remote: &str, name: &str) -> Result<String, PosixError> {
let proc = cmd!("ls-remote", vec![remote, name]).expect("Failed to execute git ls-remote");
if proc.status.success() {
let stdout = String::from_utf8(proc.stdout).unwrap();
let mut lines = stdout.lines();
let first_line = lines.next().expect("Failed to parse id from remote");
return Ok(first_line.split('\t').next().unwrap().to_string());
}
Err(error_from_output(proc))
}
pub fn short_ref(working_dir: &str, long_ref: &str) -> Result<String, PosixError> {
let proc = git_cmd_out(
working_dir.to_string(),
vec!["rev-parse", "--short", long_ref],
)?;
if proc.status.success() {
return Ok(String::from_utf8(proc.stdout)
.unwrap()
.trim_end()
.to_string());
}
Err(error_from_output(proc))
}
pub fn clone(url: &str, directory: &str) -> Result<bool, PosixError> {
let proc = git_cmd(vec!["clone", "--", url, directory])?;
if proc.status.success() {
return Ok(true);
}
Err(error_from_output(proc))
}
pub fn rev_list(working_dir: &str, args: Vec<&str>) -> Result<String, PosixError> {
let proc = cmd_in_dir!(working_dir, "rev-list", args).expect("Failed to run rev-list");
if proc.status.success() {
return Ok(String::from_utf8(proc.stdout).unwrap().trim_end().to_string());
}
Err(error_from_output(proc))
}
pub fn is_ancestor(working_dir: &str, first: &str, second: &str) -> Result<bool, PosixError> {
let args = vec!["--is-ancestor", first, second];
let proc = cmd_in_dir!(working_dir, "merge-base", args).expect("Failed to run rev-list");
Ok(proc.status.success())
}