Skip to content

Commit

Permalink
Improve wording of conflict information
Browse files Browse the repository at this point in the history
Fixes #555
  • Loading branch information
Wilfred committed Aug 16, 2023
1 parent e0a1405 commit e1f97e6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
36 changes: 24 additions & 12 deletions src/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ 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,
}

/// 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());
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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]
Expand All @@ -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");
}
}
16 changes: 12 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e1f97e6

Please sign in to comment.