-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcommands.rs
144 lines (124 loc) · 4.32 KB
/
commands.rs
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
use std::collections::BTreeSet;
use std::io::Error as IOError;
use std::process::{Command, ExitStatus, Output, Stdio};
use branches::Branches;
use error::Error;
use options::Options;
pub fn run_command_with_no_output(args: &[&str]) {
Command::new(args[0])
.args(&args[1..])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.output()
.unwrap_or_else(|e| panic!("Error with command: {}", e));
}
pub fn output(args: &[&str]) -> String {
let result = run_command(args);
String::from_utf8(result.stdout).unwrap().trim().to_owned()
}
pub fn run_command(args: &[&str]) -> Output {
run_command_with_result(args).unwrap_or_else(|e| panic!("Error with command: {}", e))
}
pub fn run_command_with_result(args: &[&str]) -> Result<Output, IOError> {
Command::new(args[0]).args(&args[1..]).output()
}
pub fn run_command_with_status(args: &[&str]) -> Result<ExitStatus, IOError> {
Command::new(args[0])
.args(&args[1..])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
}
pub fn validate_git_installation() -> Result<(), Error> {
match Command::new("git").output() {
Ok(_) => Ok(()),
Err(_) => Err(Error::GitInstallation),
}
}
pub fn delete_local_branches(branches: &Branches) -> String {
// https://git-scm.com/docs/git-branch
// With a -d or -D option, <branchname> will be deleted. You may specify more than one branch
// for deletion.
//
// So we can work without xargs.
if branches.vec.is_empty() {
String::default()
} else {
let delete_branches_args =
branches
.vec
.iter()
.fold(vec!["git", "branch", "-D"], |mut acc, b| {
acc.push(b);
acc
});
let delete_branches_cmd = run_command(&delete_branches_args);
String::from_utf8(delete_branches_cmd.stdout).unwrap()
}
}
pub fn delete_remote_branches(branches: &Branches, options: &Options) -> String {
let remote_branches_cmd = run_command(&["git", "branch", "-r"]);
let s = String::from_utf8(remote_branches_cmd.stdout).unwrap();
let all_remote_branches = s.split('\n').collect::<Vec<&str>>();
let origin_for_trim = &format!("{}/", &options.remote)[..];
let b_tree_remotes = all_remote_branches
.iter()
.map(|b| b.trim().trim_start_matches(origin_for_trim).to_owned())
.collect::<BTreeSet<String>>();
let mut b_tree_branches = BTreeSet::new();
for branch in branches.vec.clone() {
b_tree_branches.insert(branch);
}
let intersection: Vec<_> = b_tree_remotes
.intersection(&b_tree_branches)
.cloned()
.collect();
let stderr = if intersection.is_empty() {
String::default()
} else {
let delete_branches_args = intersection.iter().fold(
vec!["git", "push", &options.remote, "--delete"],
|mut acc, b| {
acc.push(b);
acc
},
);
let delete_remote_branches_cmd = run_command(&delete_branches_args);
String::from_utf8(delete_remote_branches_cmd.stderr).unwrap()
};
// Everything is written to stderr, so we need to process that
let split = stderr.split('\n');
let vec: Vec<&str> = split.collect();
let mut output = vec![];
for s in vec {
if s.contains("error: unable to delete '") {
let branch = s
.trim_start_matches("error: unable to delete '")
.trim_end_matches("': remote ref does not exist");
output.push(branch.to_owned() + " was already deleted in the remote.");
} else if s.contains(" - [deleted]") {
output.push(s.to_owned());
}
}
output.join("\n")
}
#[cfg(test)]
mod test {
use regex::Regex;
// `spawn_piped` was removed so this test is somewhat outdated.
// It now tests the match operation for which `grep` was used before.
#[test]
fn test_spawn_piped() {
let echo = Regex::new("foo\n").unwrap();
assert_eq!(
echo.captures_iter("foo\nbar\nbaz")
.fold(String::new(), |mut acc, e| {
acc.push_str(&e[0]);
acc
}),
"foo\n"
);
}
}