-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.rs
More file actions
177 lines (145 loc) · 3.77 KB
/
cli.rs
File metadata and controls
177 lines (145 loc) · 3.77 KB
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
use anyhow::Result;
use chrono::DateTime;
use log::warn;
use pbr::ProgressBar;
use std::io::{BufRead, BufReader};
use subprocess::{Exec, Redirection};
use crate::analysis::{Analysis, Change, Commit};
use crate::errors::AnalysisError;
use crate::git::{parse_stat_line, Analyser};
fn call_git(args: Vec<&'static str>) -> Result<String> {
let mut command = Exec::cmd("git");
for arg in &args {
command = command.arg(arg);
}
let capture = command
.stdout(Redirection::Pipe)
.stderr(Redirection::Pipe)
.capture()?;
if capture.success() {
Ok(capture.stdout_str().trim().to_string())
} else {
Err(AnalysisError::Git {
message: capture.stderr_str(),
}
.into())
}
}
fn call_git_stream(args: Vec<&'static str>) -> Result<Box<dyn std::io::Read>> {
let mut command = Exec::cmd("git");
for arg in &args {
command = command.arg(arg);
}
Ok(Box::new(command.stream_stdout()?))
}
struct LossyLineReader {
br: BufReader<Box<dyn std::io::Read>>,
}
impl LossyLineReader {
pub fn new(stream: Box<dyn std::io::Read>) -> Self {
LossyLineReader {
br: BufReader::new(stream),
}
}
}
impl Iterator for LossyLineReader {
type Item = String;
fn next(&mut self) -> Option<String> {
let mut buf = vec![];
if let Ok(len) = self.br.read_until(b'\n', &mut buf) {
return match len {
0 => None,
1 => Some("".to_string()),
_ => match std::str::from_utf8(&buf[0..len - 1]) {
Ok(line) => Some(line.to_string()),
Err(e) => {
warn!("Invalid UTF-8: {:?}", e);
Some("".to_string())
}
},
};
}
None
}
}
pub struct CliAnalyser {}
impl CliAnalyser {
pub fn new() -> Result<CliAnalyser> {
call_git(vec!["--version"])?;
Ok(CliAnalyser {})
}
}
impl Analyser for CliAnalyser {
fn get_path(&self) -> Result<std::path::PathBuf> {
let out = call_git(vec!["rev-parse", "--show-toplevel"])?;
Ok(std::path::PathBuf::from(out.trim_end()))
}
fn get_commit_count(&self) -> Result<u64> {
let count_str = call_git(vec!["rev-list", "--count", "HEAD"])?;
Ok(count_str.parse::<u64>()?)
}
fn get_current_revision(&self) -> Result<String> {
let rev = call_git(vec!["rev-parse", "HEAD"])?;
Ok(rev)
}
fn analyse(&self, data: &mut Analysis, pb: &mut ProgressBar<std::io::Stdout>) -> Result<()> {
let log_stream = call_git_stream(vec![
"log",
"--all",
"--numstat",
"--date=rfc2822",
"--use-mailmap",
"--pretty=format:#%H#%ad#%aN#%f",
"--no-renames",
"--no-expand-tabs",
"--no-merges",
"--encoding=UTF-8",
])?;
let log_reader = LossyLineReader::new(log_stream);
let mut commit = None;
for line in log_reader {
if line.len() < 2 {
if let Some(commit) = commit.take() {
data.commits.push(commit);
}
continue;
}
if let Some(marker) = line.strip_prefix('#') {
pb.inc();
let mut parts = marker.splitn(4, '#');
let id = parts.next().ok_or(AnalysisError::Parse {
message: "Missing commit hash",
})?;
let time_str = parts.next().ok_or(AnalysisError::Parse {
message: "Missing time",
})?;
let time = DateTime::parse_from_rfc2822(time_str)?;
let author_name = parts.next().unwrap_or("Unknown");
let words = match parts.next() {
Some(words) => data.filter_stop_words(words.split('-')),
None => vec![],
};
let user_index = data.get_user_index(author_name);
data.users[user_index].contributions += 1;
commit = Some(Commit {
id: id.to_string(),
time,
user: user_index,
files: vec![],
words,
});
} else if let Some(ref mut commit) = commit {
let (path, additions, deletions) = parse_stat_line(&line, true)?;
commit.files.push(Change {
file: data.get_file_index(path),
additions,
deletions,
});
}
}
if let Some(commit) = commit.take() {
data.commits.push(commit);
}
Ok(())
}
}