Improve wording of conflict information

Fixes #555
syntax_id
Wilfred Hughes 2023-08-15 17:52:02 +07:00
parent e0a1405453
commit e1f97e614f
2 changed files with 36 additions and 16 deletions

@ -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");
}
}

@ -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(