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
pub mod diff;
use std::env::Args;
use std::ffi::OsStr;
use std::io::stdout;
use std::io::BufRead as _;
use std::io::BufReader;
use std::io::Error;
use std::io::ErrorKind;
use std::io::Result;
use std::io::Write as _;
use std::ops::Deref as _;
use std::process::Child;
use std::process::ChildStdout;
use std::process::Command;
use std::process::Stdio;
use diff::File;
pub const GIT: &str = "/usr/bin/git";
pub fn await_child<S>(program: S, child: Child) -> Result<Option<ChildStdout>>
where
S: AsRef<OsStr>,
{
let mut child = child;
let status = child.wait()?;
if !status.success() {
let error = format!("process `{}` failed", program.as_ref().to_string_lossy());
if let Some(stderr) = child.stderr {
let mut stderr = BufReader::new(stderr);
let mut line = String::new();
if stderr.read_line(&mut line).is_ok() {
let line = line.trim();
return Err(Error::new(ErrorKind::Other, format!("{error}: {line}")))
}
}
return Err(Error::new(ErrorKind::Other, error))
}
Ok(child.stdout)
}
pub fn blame<A>(diffs: &[(File, File)], args: A) -> Result<()>
where
A: Fn() -> Args,
{
let out = stdout();
let mut out = out.lock();
for (src, dst) in diffs {
writeln!(out, "--- {}", src.file)?;
writeln!(out, "+++ {}", dst.file)?;
let () = out.flush()?;
let child = Command::new(GIT)
.arg("--no-pager")
.arg("blame")
.arg("-s")
.arg(format!("-L{},+{}", src.line, src.count))
.args(args().skip(1))
.arg("--")
.arg(src.file.deref())
.arg("HEAD")
.stdin(Stdio::null())
.stdout(Stdio::inherit())
.stderr(Stdio::piped())
.spawn()?;
let _ = await_child(GIT, child)?;
}
Ok(())
}