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
use std::iter::FromIterator;
use std::io::{Error, ErrorKind};
use std::process::{Command, Output};
pub fn in_git_repository() -> Result<Output, Error> {
git(&["rev-parse", "--is-inside-work-tree"])
}
pub fn last_tag() -> Result<Option<String>, Error> {
last_tags(1).map(|mut v| v.pop())
}
pub fn commits_in_range(range: &[String]) -> Result<Vec<String>, Error> {
let mut log = vec!["log", "--format=format:%H"];
for r in range {
log.push(r)
}
git(&log).map(|o| read_lines(&o))
}
pub fn get_commit_message(sha: &str) -> Result<Vec<String>, Error> {
git(
&[
"log",
"--format=format:%H%n%an%n%aD%n%s%n%b",
"--max-count=1",
sha,
],
).map(|o| read_lines(&o))
}
fn last_tags(n: i32) -> Result<Vec<String>, Error> {
git(
&[
"for-each-ref",
&format!("--count={}", n),
"--sort=-taggerdate",
"--format=%(refname:short)",
"refs/tags/*",
],
).map(|o| read_lines(&o))
}
fn git(args: &[&str]) -> Result<Output, Error> {
debug!("git {}", args.join(" "));
let output = Command::new("git").args(args).output()?;
if output.status.success() {
Ok(output)
} else {
error!("{}", String::from_utf8_lossy(&output.stderr));
Err(Error::from(ErrorKind::InvalidData))
}
}
fn read_lines<T: FromIterator<String>>(o: &Output) -> T {
String::from_utf8_lossy(&o.stdout)
.lines()
.map(String::from)
.collect::<T>()
}