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
use crate::diagnostics::{Diagnostic, Level};
use crate::diff::{parse_diff, FileChanges};
use crate::intervals::intersect_intervals;
use crate::reporters::{report_diagnostic, OutputKind};
use anyhow::{bail, Context, Result};
use clap::{crate_authors, crate_description, crate_version, value_t, App, AppSettings, Arg};
use std::process::{Command, Stdio};
use std::{
env,
io::{self, BufRead, BufReader, Write},
};
mod diagnostics;
mod diff;
mod intervals;
mod reporters;
pub fn build_app(binary_name: &str, subcommand: Option<(&str, &[&str])>) -> Result<()> {
let app_args: Vec<String>;
let subcommand_extra_args: Vec<String>;
if subcommand.is_none() {
app_args = env::args().collect();
subcommand_extra_args = vec![];
} else {
let args: Vec<_> = env::args().collect();
if let Some(split_pos) = args.iter().position(|v| v == "--") {
app_args = args[0..split_pos].into();
if split_pos + 1 == args.len() {
subcommand_extra_args = vec![];
} else {
subcommand_extra_args = args[(split_pos + 1)..].into();
}
} else {
app_args = args;
subcommand_extra_args = vec![];
}
}
let matches = App::new(binary_name)
.version(crate_version!())
.author(crate_authors!("\n"))
.about(crate_description!())
.setting(AppSettings::TrailingVarArg)
.setting(AppSettings::AllowLeadingHyphen)
.arg(
Arg::with_name("output")
.short("o")
.long("output")
.value_name("FORMAT")
.help("Format of the output")
.possible_values(&OutputKind::variants())
.case_insensitive(true),
)
.arg(
Arg::with_name("args")
.value_name("FORMAT")
.help("Additional arguments to pass to `git diff`")
.multiple(true),
)
.get_matches_from(&app_args);
let git_diff_args = matches.values_of("args").unwrap_or_default();
let output = Command::new("git")
.arg("diff")
.arg("--unified=0")
.args(git_diff_args)
.output()
.with_context(|| "Failed to start `git diff`")?;
if !output.stderr.is_empty() {
io::stderr()
.write_all(&output.stderr)
.with_context(|| "Failed to report the stderr of `git diff`")?;
}
if !output.status.success() {
bail!(
"`git diff` terminated with exit status {:?}",
output.status.code().unwrap()
);
}
let diff = String::from_utf8_lossy(&output.stdout);
let file_changes = parse_diff(&diff)?;
if let Some((subcommand_name, subcommand_args)) = subcommand {
let output_kind = value_t!(matches, "output", OutputKind).unwrap_or(OutputKind::Rendered);
let json_arg = if matches!(output_kind, OutputKind::GitHub) {
"--message-format=json"
} else {
"--message-format=json-diagnostic-rendered-ansi"
};
let mut child = Command::new(subcommand_name)
.args(subcommand_args)
.arg(json_arg)
.args(&subcommand_extra_args)
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.with_context(|| {
format!(
"Failed to start subprocess {:?} with arguments {:?}",
subcommand, subcommand_args,
)
})?;
let stdout = child
.stdout
.as_mut()
.with_context(|| "Failed to open standard output of subprocess")?;
process_stream(BufReader::new(stdout), &file_changes, output_kind)?;
let exit_status = child
.wait()
.with_context(|| "Failed to wait for subprocess")?;
if !exit_status.success() {
bail!(
"Subprocess terminated with exit code {}",
exit_status.code().unwrap_or(-1)
)
}
} else {
let output_kind = value_t!(matches, "output", OutputKind).unwrap_or(OutputKind::Json);
process_stream(io::stdin().lock(), &file_changes, output_kind)?;
}
Ok(())
}
fn process_stream<T: BufRead>(
stream: T,
file_changes: &FileChanges,
output: OutputKind,
) -> Result<()> {
for maybe_line in stream.lines() {
let json_line =
maybe_line.with_context(|| "Failed to read line from standard output of subprocess")?;
let diagnostic: Diagnostic = serde_json::from_str(&json_line).with_context(|| {
format!("Failed to parse JSON from standard input: {:?}", json_line)
})?;
if should_report_diagnostic(&diagnostic, &file_changes) {
report_diagnostic(&json_line, &diagnostic, output);
}
}
Ok(())
}
fn should_report_diagnostic(diagnostic: &Diagnostic, file_changes: &FileChanges) -> bool {
if let Some(ref message) = diagnostic.message {
if matches!(message.level, Level::Warning) {
let mut intersects_changes = false;
for span in &message.spans {
if let Some(file_changes) = file_changes.get(&span.file_name).as_ref() {
if intersect_intervals(span.line_start, span.line_end, file_changes) {
intersects_changes = true;
break;
}
}
}
if !intersects_changes {
return false;
}
}
}
true
}