Skip to content

Commit

Permalink
Keep trying to parse even if first line is not recognized
Browse files Browse the repository at this point in the history
Fixes #100 #121
  • Loading branch information
dandavison committed Apr 12, 2020
1 parent a176e87 commit 7f6f350
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,17 @@ pub fn delta<I>(
where
I: Iterator<Item = String>,
{
let mut lines_peekable = lines.peekable();
let mut painter = Painter::new(writer, config, assets);
let mut minus_file = "".to_string();
let mut plus_file;
let mut state = State::Unknown;
let source = detect_source(&mut lines_peekable);
let mut source = Source::Unknown;

for raw_line in lines_peekable {
for raw_line in lines {
let line = strip_ansi_codes(&raw_line).to_string();
if source == Source::Unknown {
writeln!(painter.writer, "{}", raw_line)?;
continue;
source = detect_source(&line);
}

let line = strip_ansi_codes(&raw_line).to_string();
if line.starts_with("commit ") {
painter.paint_buffered_lines();
state = State::CommitMeta;
Expand Down Expand Up @@ -160,32 +157,22 @@ where
Ok(())
}

/// Try to detect what is producing the input for delta by examining the first line
/// Try to detect what is producing the input for delta.
///
/// Currently can detect:
/// * git diff
/// * diff -u
///
/// If the source is not recognized, delta will print the unaltered
/// input back out
fn detect_source<I>(lines: &mut std::iter::Peekable<I>) -> Source
where
I: Iterator<Item = String>,
{
lines.peek().map_or(Source::Unknown, |first_line| {
let line = strip_ansi_codes(&first_line).to_string();

if line.starts_with("commit ") || line.starts_with("diff --git ") {
Source::GitDiff
} else if line.starts_with("diff -u ")
|| line.starts_with("diff -U")
|| line.starts_with("--- ")
{
Source::DiffUnified
} else {
Source::Unknown
}
})
fn detect_source(line: &str) -> Source {
if line.starts_with("commit ") || line.starts_with("diff --git ") {
Source::GitDiff
} else if line.starts_with("diff -u ")
|| line.starts_with("diff -U")
|| line.starts_with("--- ")
{
Source::DiffUnified
} else {
Source::Unknown
}
}

fn handle_commit_meta_header_line(
Expand Down Expand Up @@ -678,6 +665,7 @@ mod tests {
}

#[test]
#[ignore] // Ideally, delta would make this test pass. See #121.
fn test_delta_ignores_non_diff_input() {
let options = get_command_line_options();
let output = strip_ansi_codes(&run_delta(NOT_A_DIFF_OUTPUT, &options)).to_string();
Expand Down

0 comments on commit 7f6f350

Please sign in to comment.