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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) 2017-2018 ETH Zurich
// Fabian Schuiki <fschuiki@iis.ee.ethz.ch>
//! A git repository and context for command execution.
#![deny(missing_docs)]
use std::ffi::OsStr;
use std::path::Path;
use futures::TryFutureExt;
use tokio::process::Command;
use crate::error::*;
/// A git repository.
///
/// This struct is used to interact with git repositories on disk. It makes
/// heavy use of futures to execute the different tasks.
#[derive(Copy, Clone)]
pub struct Git<'ctx> {
/// The path to the repository.
pub path: &'ctx Path,
/// The session within which commands will be executed.
pub git: &'ctx String,
}
impl<'git, 'ctx> Git<'ctx> {
/// Create a new git context.
pub fn new(path: &'ctx Path, git: &'ctx String) -> Git<'ctx> {
Git { path, git }
}
/// Create a new git command.
///
/// The command will have the form `git <subcommand>` and be pre-configured
/// to operate in the repository's path.
pub fn command(self, subcommand: &str) -> Command {
let mut cmd = Command::new(self.git);
cmd.arg(subcommand);
cmd.current_dir(self.path);
cmd
}
/// Schedule a command for execution.
///
/// Configures the command's stdout and stderr to be captured and wires up
/// appropriate error handling. In case the command fails, the exact
/// arguments to the command are emitted together with the captured output.
/// The command is spawned asynchronously on the session's reactor core.
/// Returns a future that will resolve to the command's stdout.
///
/// If `check` is false, the stdout will be returned regardless of the
/// command's exit code.
#[allow(clippy::format_push_string)]
pub async fn spawn(self, mut cmd: Command, check: bool) -> Result<String> {
let output = cmd.output().map_err(|cause| {
if cause
.to_string()
.to_lowercase()
.contains("too many open files")
{
println!(
"Please consider increasing your `ulimit -n`, e.g. by running `ulimit -n 4096`"
);
println!("This is a known issue (#52).");
Error::chain("Failed to spawn child process.", cause)
} else {
Error::chain("Failed to spawn child process.", cause)
}
});
let result = output.and_then(|output| async move {
debugln!("git: {:?} in {:?}", cmd, self.path);
if output.status.success() || !check {
String::from_utf8(output.stdout).map_err(|cause| {
Error::chain(
format!(
"Output of git command ({:?}) in directory {:?} is not valid UTF-8.",
cmd, self.path
),
cause,
)
})
} else {
let mut msg = format!("Git command ({:?}) in directory {:?}", cmd, self.path);
match output.status.code() {
Some(code) => msg.push_str(&format!(" failed with exit code {}", code)),
None => msg.push_str(" failed"),
};
match String::from_utf8(output.stderr) {
Ok(txt) => {
msg.push_str(":\n\n");
msg.push_str(&txt);
}
Err(err) => msg.push_str(&format!(". Stderr is not valid UTF-8, {}.", err)),
};
Err(Error::new(msg))
}
});
result.await
}
/// Assemble a command and schedule it for execution.
///
/// This is a convenience function that creates a command, passes it to the
/// closure `f` for configuration, then passes it to the `spawn` function
/// and returns the future.
pub async fn spawn_with<F>(self, f: F) -> Result<String>
where
F: FnOnce(&mut Command) -> &mut Command,
{
let mut cmd = Command::new(self.git);
cmd.current_dir(self.path);
f(&mut cmd);
self.spawn(cmd, true).await
}
/// Assemble a command and schedule it for execution.
///
/// This is the same as `spawn_with()`, but returns the stdout regardless of
/// whether the command failed or not.
pub async fn spawn_unchecked_with<F>(self, f: F) -> Result<String>
where
F: FnOnce(&mut Command) -> &mut Command,
{
let mut cmd = Command::new(self.git);
cmd.current_dir(self.path);
f(&mut cmd);
self.spawn(cmd, false).await
}
/// Assemble a command and execute it interactively.
///
/// This is the same as `spawn_with()`, but inherits stdin, stdout, and stderr
/// from the caller.
pub async fn spawn_interactive_with<F>(self, f: F) -> Result<()>
where
F: FnOnce(&mut Command) -> &mut Command,
{
let mut cmd = Command::new(self.git);
cmd.current_dir(self.path);
f(&mut cmd);
cmd.spawn()?.wait().await?;
Ok(())
}
/// Fetch the tags and refs of a remote.
pub async fn fetch(self, remote: &str) -> Result<()> {
let r1 = String::from(remote);
let r2 = String::from(remote);
self.spawn_with(|c| c.arg("fetch").arg("--prune").arg(r1))
.and_then(|_| self.spawn_with(|c| c.arg("fetch").arg("--tags").arg("--prune").arg(r2)))
.await
.map(|_| ())
}
/// Stage all local changes.
pub async fn add_all(self) -> Result<()> {
self.spawn_with(|c| c.arg("add").arg("--all"))
.await
.map(|_| ())
}
/// Commit the staged changes.
///
/// If message is None, this starts an interactive commit session.
pub async fn commit(self, message: Option<&String>) -> Result<()> {
match message {
Some(msg) => self
.spawn_with(|c| {
c.arg("-c")
.arg("commit.gpgsign=false")
.arg("commit")
.arg("-m")
.arg(msg)
})
.await
.map(|_| ()),
None => self
.spawn_interactive_with(|c| c.arg("-c").arg("commit.gpgsign=false").arg("commit"))
.await
.map(|_| ()),
}
}
/// List all refs and their hashes.
pub async fn list_refs(self) -> Result<Vec<(String, String)>> {
self.spawn_unchecked_with(|c| c.arg("show-ref").arg("--dereference"))
.and_then(|raw| async move {
let mut all_revs = raw
.lines()
.map(|line| {
// Parse the line
let mut fields = line.split_whitespace().map(String::from);
let rev = fields.next().unwrap();
let rf = fields.next().unwrap();
(rev, rf)
})
.collect::<Vec<_>>();
// Ensure only commit hashes are returned by using dereferenced values in case they exist
let deref_revs = all_revs
.clone()
.into_iter()
.filter(|tup| tup.1.ends_with("^{}"));
for item in deref_revs {
let index = all_revs
.iter()
.position(|x| *x.1 == item.1.replace("^{}", ""))
.unwrap();
all_revs.remove(index);
let index = all_revs.iter().position(|x| *x.1 == item.1).unwrap();
all_revs.remove(index);
all_revs.push((item.0, item.1.replace("^{}", "")));
}
// Return future
Ok(all_revs)
})
.await
}
/// List all revisions.
pub async fn list_revs(self) -> Result<Vec<String>> {
self.spawn_with(|c| c.arg("rev-list").arg("--all").arg("--date-order"))
.await
.map(|raw| raw.lines().map(String::from).collect())
}
/// Determine the currently checked out revision.
pub async fn current_checkout(self) -> Result<Option<String>> {
self.spawn_with(|c| c.arg("rev-parse").arg("--revs-only").arg("HEAD^{commit}"))
.await
.map(|raw| raw.lines().take(1).map(String::from).next())
}
/// List files in the directory.
///
/// Calls `git ls-tree` under the hood.
pub async fn list_files<R: AsRef<OsStr>, P: AsRef<OsStr>>(
self,
rev: R,
path: Option<P>,
) -> Result<Vec<TreeEntry>> {
self.spawn_with(|c| {
c.arg("ls-tree").arg(rev);
if let Some(p) = path {
c.arg(p);
}
c
})
.await
.map(|raw| raw.lines().map(TreeEntry::parse).collect())
}
/// Read the content of a file.
pub async fn cat_file<O: AsRef<OsStr>>(self, hash: O) -> Result<String> {
self.spawn_with(|c| c.arg("cat-file").arg("blob").arg(hash))
.await
}
}
/// A single entry in a git tree.
///
/// The `list_files` command returns a vector of these.
pub struct TreeEntry {
/// The name of the file.
pub name: String,
/// The hash of the entry.
pub hash: String,
/// The kind of the entry. Usually `blob` or `tree`.
pub kind: String,
/// The mode of the entry, i.e. its permissions.
pub mode: String,
}
impl TreeEntry {
/// Parse a single line of output of `git ls-tree`.
pub fn parse(input: &str) -> TreeEntry {
let tab = input.find('\t').unwrap();
let (metadata, name) = input.split_at(tab);
let mut iter = metadata.split(' ');
let mode = iter.next().unwrap();
let kind = iter.next().unwrap();
let hash = iter.next().unwrap();
TreeEntry {
name: name.into(),
hash: hash.into(),
kind: kind.into(),
mode: mode.into(),
}
}
}