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
use error::GitError;
use std::env;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;
use types::{BranchName, GitUrl, Result};
pub mod error;
pub mod types;
pub struct Repository {
location: PathBuf,
}
impl Repository {
pub fn new<P: AsRef<Path>>(p: P) -> Repository {
let p = p.as_ref();
Repository {
location: PathBuf::from(p),
}
}
pub fn clone<P: AsRef<Path>>(url: GitUrl, p: P) -> Result<Repository> {
let p = p.as_ref();
let cwd = env::current_dir().map_err(|_| GitError::WorkingDirectoryInaccessible)?;
execute_git(cwd, &["clone", url.value.as_str(), p.to_str().unwrap()]).map(|_| Repository {
location: PathBuf::from(p),
})
}
pub fn init<P: AsRef<Path>>(p: P) -> Result<Repository> {
let p = p.as_ref();
execute_git(&p, &["init"])?;
Ok(Repository {
location: PathBuf::from(p),
})
}
pub fn create_local_branch(&self, branch_name: &BranchName) -> Result<()> {
execute_git(
&self.location,
&["checkout", "-b", branch_name.value.as_str()],
)
}
pub fn switch_branch(&self, branch_name: &BranchName) -> Result<()> {
execute_git(&self.location, &["checkout", branch_name.value.as_str()])
}
pub fn add(&self, pathspecs: Vec<&str>) -> Result<()> {
let mut args = pathspecs.clone();
args.insert(0, "add");
execute_git(&self.location, args)
}
pub fn remove(&self, pathspecs: Vec<&str>, force: bool) -> Result<()> {
let mut args = pathspecs.clone();
args.insert(0, "rm");
if force {
args.push("-f");
}
execute_git(&self.location, args)
}
pub fn commit_all(&self, message: &str) -> Result<()> {
execute_git(&self.location, &["commit", "-am", message])
}
pub fn push(&self) -> Result<()> {
execute_git(&self.location, &["push"])
}
pub fn push_to_upstream(&self, upstream: &str, upstream_branch: &BranchName) -> Result<()> {
execute_git(
&self.location,
&["push", "-u", upstream, upstream_branch.value.as_str()],
)
}
pub fn add_remote(&self, name: &str, url: &GitUrl) -> Result<()> {
execute_git(&self.location, &["remote", "add", name, url.value.as_str()])
}
pub fn fetch_remote(&self, remote: &str) -> Result<()> {
execute_git(&self.location, &["fetch", remote])
}
pub fn create_branch_from_startpoint(
&self,
branch_name: &BranchName,
startpoint: &str,
) -> Result<()> {
execute_git(
&self.location,
&[
"checkout",
"-b",
branch_name.to_string().as_str(),
startpoint,
],
)
}
pub fn list_branches(&self) -> Result<Vec<String>> {
execute_git_fn(
&self.location,
&["branch", "--format=%(refname:short)"],
|output| Ok(output.lines().map(|line| line.to_owned()).collect()),
)
}
pub fn list_added(&self) -> Result<Vec<String>> {
git_status(&self, "A")
}
pub fn list_modified(&self) -> Result<Vec<String>> {
git_status(&self, " M")
}
pub fn list_untracked(&self) -> Result<Vec<String>> {
git_status(&self, "??")
}
pub fn list_tracked(&self) -> Result<Vec<String>> {
execute_git_fn(&self.location, &["ls-files"], |output| {
Ok(output.lines().map(|line| line.to_owned()).collect())
})
}
pub fn show_remote_uri(&self, remote_name: &str) -> Result<String> {
execute_git_fn(&self.location, &["config", "--get", format!("remote.{}.url", remote_name).as_str()], |output| {
Ok(output.trim().to_owned())
})
}
pub fn list_remotes(&self) -> Result<Vec<String>> {
execute_git_fn(&self.location, &["remote", "show"], |output| {
if output.trim().is_empty() {
Err(GitError::NoRemoteRepositorySet)
} else {
Ok(output.lines().map(|line| line.to_owned()).collect())
}
})
}
pub fn get_hash(&self, short: bool) -> Result<String> {
let args: &[_] = if short {
&["rev-parse", "--short", "HEAD"]
} else {
&["rev-parse", "HEAD"]
};
execute_git_fn(&self.location, args, |output| Ok(output.trim().to_owned()))
}
pub fn cmd<I, S>(&self, args: I) -> Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
execute_git(&self.location, args)
}
pub fn cmd_out<I, S>(&self, args: I) -> Result<Vec<String>>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
execute_git_fn(&self.location, args, |output| {
Ok(output.lines().map(|line| line.to_owned()).collect())
})
}
}
fn git_status(repo: &Repository, prefix: &str) -> Result<Vec<String>> {
execute_git_fn(&repo.location, &["status", "-s"], |output| {
Ok(output
.lines()
.filter(|line| line.starts_with(&prefix))
.map(|line| line[3..].to_owned())
.collect())
})
}
fn execute_git<I, S, P>(p: P, args: I) -> Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
P: AsRef<Path>,
{
execute_git_fn(p, args, |_| Ok(()))
}
fn execute_git_fn<I, S, P, F, R>(p: P, args: I, process: F) -> Result<R>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
P: AsRef<Path>,
F: Fn(&str) -> Result<R>,
{
let output = Command::new("git").current_dir(p).args(args).output();
output.map_err(|_| GitError::Execution).and_then(|output| {
if output.status.success() {
if let Ok(message) = str::from_utf8(&output.stdout) {
process(message)
} else {
Err(GitError::Undecodable)
}
} else {
if let Ok(stdout) = str::from_utf8(&output.stdout) {
if let Ok(stderr) = str::from_utf8(&output.stderr) {
Err(GitError::GitError {
stdout: stdout.to_owned(),
stderr: stderr.to_owned(),
})
} else {
Err(GitError::Undecodable)
}
} else {
Err(GitError::Undecodable)
}
}
})
}