diff --git a/src/conflicts.rs b/src/conflicts.rs index f97ba1215d..1490fb9d50 100644 --- a/src/conflicts.rs +++ b/src/conflicts.rs @@ -18,9 +18,9 @@ const START_RHS_MARKER: &str = "======="; const END_RHS_MARKER: &str = ">>>>>>>"; pub struct ConflictFiles { - pub lhs_name: String, + pub lhs_name: Option, pub lhs_content: String, - pub rhs_name: String, + pub rhs_name: Option, pub rhs_content: String, pub num_conflicts: usize, } @@ -28,8 +28,8 @@ pub struct ConflictFiles { /// Convert a string with conflict markers into the two conflicting /// file contents. pub fn apply_conflict_markers(s: &str) -> Result { - let mut lhs_name = String::new(); - let mut rhs_name = String::new(); + let mut lhs_name: Option = None; + let mut rhs_name: Option = None; let mut lhs_content = String::with_capacity(s.len()); let mut rhs_content = String::with_capacity(s.len()); @@ -44,8 +44,14 @@ pub fn apply_conflict_markers(s: &str) -> Result { conflict_start_line = Some(i); let hunk_lhs_name = hunk_lhs_name.trim(); - if hunk_lhs_name.len() > lhs_name.len() { - lhs_name = hunk_lhs_name.to_owned(); + if !hunk_lhs_name.is_empty() { + let should_replace = match &lhs_name { + Some(prev_name) => hunk_lhs_name.len() > prev_name.len(), + None => true, + }; + if should_replace { + lhs_name = Some(hunk_lhs_name.to_owned()); + } } continue; @@ -62,8 +68,14 @@ pub fn apply_conflict_markers(s: &str) -> Result { state = NoConflict; let hunk_rhs_name = hunk_rhs_name.trim(); - if hunk_rhs_name.len() > rhs_name.len() { - rhs_name = hunk_rhs_name.to_owned(); + if !hunk_rhs_name.is_empty() { + let should_replace = match &rhs_name { + Some(prev_name) => hunk_rhs_name.len() > prev_name.len(), + None => true, + }; + if should_replace { + rhs_name = Some(hunk_rhs_name.to_owned()); + } } continue; } @@ -117,8 +129,8 @@ mod tests { assert_eq!(conflict_files.lhs_content, "before\nnew in left\nafter"); assert_eq!(conflict_files.rhs_content, "before\nnew in right\nafter"); - assert_eq!(conflict_files.lhs_name, "Temporary merge branch 1"); - assert_eq!(conflict_files.rhs_name, "Temporary merge branch 2"); + assert_eq!(conflict_files.lhs_name.unwrap(), "Temporary merge branch 1"); + assert_eq!(conflict_files.rhs_name.unwrap(), "Temporary merge branch 2"); } #[test] @@ -131,7 +143,7 @@ mod tests { assert_eq!(conflict_files.lhs_content, "before\nnew in left\nafter"); assert_eq!(conflict_files.rhs_content, "before\nnew in right\nafter"); - assert_eq!(conflict_files.lhs_name, "Temporary merge branch 1"); - assert_eq!(conflict_files.rhs_name, "Temporary merge branch 2"); + assert_eq!(conflict_files.lhs_name.unwrap(), "Temporary merge branch 1"); + assert_eq!(conflict_files.rhs_name.unwrap(), "Temporary merge branch 2"); } } diff --git a/src/main.rs b/src/main.rs index 9503e91720..2c3e810585 100644 --- a/src/main.rs +++ b/src/main.rs @@ -363,15 +363,23 @@ fn diff_conflicts_file( if conflict_files.num_conflicts == 0 { eprintln!( - "warning: Expected a file with conflict markers {}, but none were found.", + "warning: Expected a file with conflict markers {}, but none were found. See --help for usage instructions.\n", START_LHS_MARKER, ); - eprintln!("Difftastic parses conflict markers from a single file argument. Did you forget a second file argument?"); } + let lhs_name = match conflict_files.lhs_name { + Some(name) => format!("'{}'", name), + None => "the left file".to_owned(), + }; + let rhs_name = match conflict_files.rhs_name { + Some(name) => format!("'{}'", name), + None => "the right file".to_owned(), + }; + let extra_info = format!( - "Comparing '{}' with '{}'", - conflict_files.lhs_name, conflict_files.rhs_name + "Showing the result of replacing every conflict in {} with {}.", + lhs_name, rhs_name ); diff_file_content(