/
main.rs
530 lines (441 loc) · 14.6 KB
/
main.rs
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Clippy settings, full list here:
// <https://rust-lang.github.io/rust-clippy/master/index.html>
#![allow(clippy::clippy::clippy::needless_return)]
//
// Fail build on Clippy warnings
#![deny(warnings)]
#[macro_use]
extern crate lazy_static;
use backtrace::Backtrace;
use constants::*;
use git_version::git_version;
use isatty::{stdin_isatty, stdout_isatty};
use regex::Regex;
use std::env;
use std::io::{self, BufRead, BufReader, BufWriter, Write};
use std::panic;
use std::path;
use std::process::exit;
use std::process::{Command, Stdio};
use std::str;
mod constants;
mod refiner;
mod token_collector;
mod tokenizer;
const HELP_TEXT: &str = r#"
Usage:
diff ... | riff
riff [-b] <file1> <file2>
riff [-b] <directory1> <directory2>
Colors diff output, highlighting the changed parts of every line.
Git integration:
git config --global pager.diff riff
git config --global pager.show riff
git config --global interactive.diffFilter riff
Options:
-b: Ignore changes in amount of whitespace
--help: Print this text
--version: Print version number
"#;
const HELP_TEXT_FOOTER: &str = r#"
Report issues at <https://github.com/walles/riff>.
"#;
const CRASH_FOOTER: &str = r#"
Please copy the above crash report and report it at one of:
* <https://github.com/walles/riff/issues> (preferred)
* <johan.walles@gmail.com>
"#;
const HUNK_HEADER: &str = "\x1b[36m"; // Cyan
const PAGER_FORKBOMB_STOP: &str = "_RIFF_IGNORE_PAGER";
const GIT_VERSION: &str = git_version!();
lazy_static! {
static ref STATIC_HEADER_PREFIXES: Vec<(&'static str, &'static str)> = vec![
("diff ", BOLD),
("index ", BOLD),
("--- ", BOLD),
("+++ ", BOLD),
("@@ ", HUNK_HEADER),
];
static ref ANSI_COLOR_REGEX: Regex = Regex::new("\x1b[^m]*m").unwrap();
}
enum LastLineKind {
Initial,
Old,
New,
}
#[must_use]
fn get_fixed_highlight(line: &str) -> &str {
for static_header_prefix in STATIC_HEADER_PREFIXES.iter() {
let prefix = static_header_prefix.0;
if line.starts_with(prefix) {
return static_header_prefix.1;
}
}
return "";
}
fn print(stream: &mut BufWriter<&mut dyn Write>, text: &str) {
stream.write_all(text.as_bytes()).unwrap();
}
fn println(stream: &mut BufWriter<&mut dyn Write>, text: &str) {
print(stream, &text);
print(stream, "\n");
}
fn highlight_diff(input: &mut dyn io::Read, output: &mut dyn io::Write) {
let mut old_text = String::new();
let mut new_text = String::new();
let input = BufReader::new(input);
let output = &mut BufWriter::new(output);
let mut last_line_kind = LastLineKind::Initial;
for line in input.lines() {
let line = line.unwrap();
// Strip out incoming ANSI formatting. This enables us to highlight
// already-colored input.
let line = ANSI_COLOR_REGEX.replace_all(&line, "");
let fixed_highlight = get_fixed_highlight(&line);
if !fixed_highlight.is_empty() {
// Drain outstanding adds / removes
for line in refiner::format(&old_text, &new_text) {
println(output, &line);
}
old_text.clear();
new_text.clear();
print(output, fixed_highlight);
print(output, &line);
println(output, NORMAL);
continue;
}
// Collect adds / removes
if !line.is_empty() && line.starts_with('+') {
new_text.push_str(&line[1..]);
new_text.push('\n');
last_line_kind = LastLineKind::New;
continue;
} else if !line.is_empty() && line.starts_with('-') {
old_text.push_str(&line[1..]);
old_text.push('\n');
last_line_kind = LastLineKind::Old;
continue;
}
if line == NO_EOF_NEWLINE_MARKER {
match last_line_kind {
LastLineKind::Old => {
assert!(old_text.pop().unwrap() == '\n');
continue;
}
LastLineKind::New => {
assert!(new_text.pop().unwrap() == '\n');
continue;
}
LastLineKind::Initial => {
// This block intentionally left blank
}
}
}
last_line_kind = LastLineKind::Initial;
// Drain outstanding adds / removes
for line in refiner::format(&old_text, &new_text) {
println(output, &line);
}
old_text.clear();
new_text.clear();
// Print current line
if line == NO_EOF_NEWLINE_MARKER {
print(output, NO_EOF_NEWLINE_COLOR);
print(output, &line);
println(output, NORMAL);
} else {
println(output, &line);
}
}
for line in refiner::format(&old_text, &new_text) {
println(output, &line);
}
}
/// Try paging using the named pager (`$PATH` will be searched).
///
/// Returns `true` if the pager was found, `false` otherwise.
#[must_use]
fn try_pager(input: &mut dyn io::Read, pager_name: &str) -> bool {
let mut command = Command::new(pager_name);
if env::var(PAGER_FORKBOMB_STOP).is_ok() {
// Try preventing fork bombing if $PAGER is set to riff
return false;
}
command.env(PAGER_FORKBOMB_STOP, "1");
if env::var("LESS").is_err() {
// Set by git when paging
command.env("LESS", "FRX");
}
if env::var("LV").is_err() {
// Set by git when paging
command.env("LV", "-c");
}
command.stdin(Stdio::piped());
match command.spawn() {
Ok(mut pager) => {
let pager_stdin = pager.stdin.as_mut().unwrap();
highlight_diff(input, pager_stdin);
// FIXME: Report pager exit status if non-zero, together with
// contents of pager stderr as well if possible.
pager.wait().expect("Waiting for pager failed");
return true;
}
Err(_) => {
return false;
}
}
}
/// If `option` is found in `argv`, all instances of `option` will be removed
/// from `argv`.
///
/// Returns `true` if `option` was found and consumed, false otherwise.
#[must_use]
fn consume(option: &str, argv: &mut Vec<String>) -> bool {
if !argv.contains(&option.to_string()) {
// Not found
return false;
}
argv.retain(|x| x != option);
return true;
}
fn print_help(output: &mut dyn io::Write) {
output.write_all(HELP_TEXT.trim().as_bytes()).unwrap();
output.write_all(b"\n").unwrap();
output.write_all(b"\n").unwrap();
output
.write_all(b"Installing riff in the $PATH:\n")
.unwrap();
output
.write_all(b" sudo cp riff /usr/local/bin\n")
.unwrap();
output.write_all(b"\n").unwrap();
output
.write_all(HELP_TEXT_FOOTER.trim().as_bytes())
.unwrap();
output.write_all(b"\n").unwrap();
}
fn panic_handler(panic_info: &panic::PanicInfo) {
eprintln!("\n\n-v-v-v----------- RIFF CRASHED ---------------v-v-v-\n",);
// Panic message
eprintln!("Panic message: <{:#?}>", panic_info);
eprintln!("");
// Backtrace
eprintln!("{:?}", Backtrace::new());
eprintln!("Riff version: {}", GIT_VERSION);
eprintln!("");
eprintln!("Command line arguments: {:?}", env::args());
eprintln!("\n-^-^-^------- END OF RIFF CRASH REPORT -------^-^-^-\n",);
eprintln!("{}", CRASH_FOOTER);
}
fn highlight_stream(input: &mut dyn io::Read) {
if !stdout_isatty() {
// We're being piped, just do stdin -> stdout
highlight_diff(input, &mut io::stdout());
return;
}
if let Ok(pager_value) = env::var("PAGER") {
if try_pager(input, &pager_value) {
return;
}
// FIXME: Print warning at the end if $PAGER was set to something that
// doesn't exist.
}
if try_pager(input, "moar") {
return;
}
if try_pager(input, "less") {
return;
}
// No pager found, wth?
highlight_diff(input, &mut io::stdout());
}
pub fn type_string(path: &path::Path) -> &str {
if !path.exists() {
return "Not found";
}
if path.is_file() {
return "File";
}
if path.is_dir() {
return "Directory";
}
return "Not file not dir";
}
fn exec_diff_highlight(path1: &str, path2: &str, ignore_space_change: bool) {
let path1 = path::Path::new(path1);
let path2 = path::Path::new(path2);
let both_paths_are_files = path1.is_file() && path2.is_file();
let both_paths_are_dirs = path1.is_dir() && path2.is_dir();
if !(both_paths_are_files || both_paths_are_dirs) {
eprintln!("Can only compare file to file or directory to directory, not like this:",);
eprintln!(" {:<9}: {}", type_string(path1), path1.to_string_lossy());
eprintln!(" {:<9}: {}", type_string(path2), path2.to_string_lossy());
exit(1);
}
// Run "diff -ur file1 file2"
let mut command: &mut Command = &mut Command::new("diff");
if ignore_space_change {
command = command.arg("-b");
}
let command = command
.arg("-ur") // "-u = unified diff, -r = recurse subdirectories"
.arg(path1)
.arg(path2)
.stdout(Stdio::piped());
let pretty_command = format!("{:#?}", command);
let mut diff_subprocess = command.spawn().expect(&pretty_command);
let diff_stdout = diff_subprocess.stdout.as_mut().unwrap();
highlight_stream(diff_stdout);
let diff_result = diff_subprocess.wait().unwrap();
if !diff_result.success() {
eprintln!("{}", pretty_command);
let diff_exit_code = diff_result.code().or(Some(1));
exit(diff_exit_code.unwrap());
}
}
fn main() {
panic::set_hook(Box::new(|panic_info: &panic::PanicInfo| {
panic_handler(panic_info);
}));
let mut args: Vec<String> = env::args().collect();
if consume("--help", &mut args) || consume("-h", &mut args) {
print_help(&mut io::stdout());
return;
}
if consume("--version", &mut args) {
println!("riff {}", GIT_VERSION);
println!();
println!("Source code available at <https://github.com/walles/riff>.");
return;
}
let ignore_space_change = consume("-b", &mut args);
if consume("--please-panic", &mut args) {
panic!("Panicking on purpose");
}
if args.len() == 3 {
// "riff file1 file2"
exec_diff_highlight(
args.get(1).unwrap(),
args.get(2).unwrap(),
ignore_space_change,
);
return;
}
if ignore_space_change {
eprintln!(
"ERROR: -b is only supported when diffing two named paths (\"riff -b a.txt b.txt\")"
);
eprintln!("");
print_help(&mut io::stderr());
exit(1);
}
if args.len() != 1 {
eprintln!("ERROR: Unknown command line: {:?}", args);
eprintln!("");
print_help(&mut io::stderr());
exit(1);
}
if stdin_isatty() {
eprintln!("ERROR: Expected input from a pipe");
eprintln!("");
print_help(&mut io::stderr());
exit(1);
}
highlight_stream(&mut io::stdin().lock());
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[cfg(test)]
use pretty_assertions::assert_eq;
fn old(text: &str) -> String {
return format!("{}{}{}", OLD, text, NORMAL);
}
fn new(text: &str) -> String {
return format!("{}{}{}", NEW, text, NORMAL);
}
#[test]
fn test_remove_trailing_newline() {
let mut input = "-hej\n\
+hej\n\
\\ No newline at end of file\n\
"
.as_bytes();
let expected = format!(
"{}\n{}\n{}\n",
old(&format!("-hej{}⏎", INVERSE_VIDEO)),
new("+hej"),
format!(
"{}\\ No newline at end of file{}",
NO_EOF_NEWLINE_COLOR, NORMAL
)
);
let mut actual: Vec<u8> = Vec::new();
highlight_diff(&mut input, &mut actual);
assert_eq!(std::str::from_utf8(&actual).unwrap(), expected);
}
#[test]
fn test_trailing_newline_context() {
let mut input = "+bepa\n apa\n\\ No newline at end of file\n".as_bytes();
let expected = format!(
"{}\n{}\n{}\n",
new("+bepa"),
" apa",
format!(
"{}\\ No newline at end of file{}",
NO_EOF_NEWLINE_COLOR, NORMAL
)
);
let mut output: Vec<u8> = Vec::new();
highlight_diff(&mut input, &mut output);
assert_eq!(std::str::from_utf8(&output).unwrap(), expected);
}
#[test]
fn test_testdata_examples() {
// Example value: `/Users/johan/src/riff/target/debug/deps/riff-7a8916c06b0d3d6c`
let exe_path = std::env::current_exe().unwrap();
// Example value: `/Users/johan/src/riff`
let project_path = exe_path
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();
// Example value: `/Users/johan/src/riff/testdata`
let testdata_path = &project_path.join("testdata");
assert!(testdata_path.is_dir());
// Iterate all files in there
for diff in fs::read_dir(testdata_path).unwrap() {
let diff = diff.unwrap();
let diff = diff.path();
let diff = diff.as_path();
if !diff.is_file() {
continue;
}
if diff.extension().unwrap() != "diff" {
continue;
}
println!("Evaluating example file <{}>...", diff.to_str().unwrap());
// Run highlighting on the file into a memory buffer
let mut actual_result: Vec<u8> = Vec::new();
highlight_diff(&mut fs::File::open(diff).unwrap(), &mut actual_result);
let actual_result = str::from_utf8(&actual_result).unwrap();
// Load the corresponding .riff-output file into a string
let basename = diff.file_stem().unwrap().to_str().unwrap();
let expected_path = format!(
"{}/{}.riff-output",
testdata_path.to_str().unwrap(),
basename
);
let expected_result = fs::read_to_string(expected_path).unwrap();
// Assert that the highlighting output matches the contents of .riff-output
let actual_lines: Vec<&str> = actual_result.split('\n').collect();
let expected_lines: Vec<&str> = expected_result.split('\n').collect();
assert_eq!(actual_lines, expected_lines);
}
}
}