summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilfred Hughes <me@wilfred.me.uk>2023-08-15 17:52:02 -0700
committerWilfred Hughes <me@wilfred.me.uk>2023-08-15 17:52:02 -0700
commite1f97e614fda1b34e37557da246eaf2613f5afcb (patch)
tree350f556a231db5289ec8af11054bbd6c895d6f5a
parente0a14054539fa90f8defe67b4a46dec5e7a26c28 (diff)
Improve wording of conflict information
Fixes #555
-rw-r--r--src/conflicts.rs36
-rw-r--r--src/main.rs16
2 files changed, 36 insertions, 16 deletions
diff --git a/src/conflicts.rs b/src/conflicts.rs
index f97ba1215..1490fb9d5 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<String>,
pub lhs_content: String,
- pub rhs_name: String,
+ pub rhs_name: Option<String>,
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<ConflictFiles, String> {
- let mut lhs_name = String::new();
- let mut rhs_name = String::new();
+ let mut lhs_name: Option<String> = None;
+ let mut rhs_name: Option<String> = 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<ConflictFiles, String> {
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<ConflictFiles, String> {
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 9503e9172..2c3e81058 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(