summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilfred Hughes <me@wilfred.me.uk>2023-08-14 08:41:42 -0700
committerWilfred Hughes <me@wilfred.me.uk>2023-08-14 08:41:42 -0700
commitf06e95ca0207f7d2ca17039d9d3e8bcde7134342 (patch)
tree9c7fbbb28d7a9a8f9d3517d7d47f0c94b59ca950
parentf1ba399504525c758add111d1a75b12c2a7ae3f7 (diff)
Renamed `old_path` to `extra_info` and format it during option parsing
This allows us to use this field for other purposes that aren't renames.
-rw-r--r--src/display/inline.rs4
-rw-r--r--src/display/style.rs9
-rw-r--r--src/main.rs36
-rw-r--r--src/options.rs3
-rw-r--r--src/summary.rs4
5 files changed, 29 insertions, 27 deletions
diff --git a/src/display/inline.rs b/src/display/inline.rs
index 3affa674d..d334be040 100644
--- a/src/display/inline.rs
+++ b/src/display/inline.rs
@@ -19,7 +19,7 @@ pub fn print(
rhs_positions: &[MatchedPos],
hunks: &[Hunk],
display_path: &str,
- old_path: &Option<String>,
+ extra_info: &Option<String>,
file_format: &FileFormat,
) {
let (lhs_colored_lines, rhs_colored_lines) = if display_options.use_color {
@@ -65,7 +65,7 @@ pub fn print(
"{}",
style::header(
display_path,
- old_path,
+ extra_info,
i + 1,
hunks.len(),
file_format,
diff --git a/src/display/style.rs b/src/display/style.rs
index 60c69ed95..efca5dde1 100644
--- a/src/display/style.rs
+++ b/src/display/style.rs
@@ -451,7 +451,7 @@ pub(crate) fn apply_line_number_color(
pub fn header(
display_path: &str,
- old_path: &Option<String>,
+ extra_info: &Option<String>,
hunk_num: usize,
hunk_total: usize,
file_format: &FileFormat,
@@ -475,10 +475,9 @@ pub fn header(
trailer = trailer.dimmed().to_string();
}
- match old_path {
- Some(old_path) if hunk_num == 1 => {
- let renamed = format!("Renamed from {} to {}", old_path, display_path);
- format!("{}{}\n{}", display_path_pretty, trailer, renamed)
+ match extra_info {
+ Some(extra_info) if hunk_num == 1 => {
+ format!("{}{}\n{}", display_path_pretty, trailer, extra_info)
}
_ => {
format!("{}{}", display_path_pretty, trailer)
diff --git a/src/main.rs b/src/main.rs
index 1a87d4c1f..8eea8b07d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -267,7 +267,7 @@ fn main() {
/// Print a diff between two files.
fn diff_file(
display_path: &str,
- old_path: Option<String>,
+ extra_info: Option<String>,
lhs_path: &FileArgument,
rhs_path: &FileArgument,
display_options: &DisplayOptions,
@@ -279,7 +279,7 @@ fn diff_file(
let (lhs_src, rhs_src) = match (guess_content(&lhs_bytes), guess_content(&rhs_bytes)) {
(ProbableFileKind::Binary, _) | (_, ProbableFileKind::Binary) => {
return DiffResult {
- old_path,
+ extra_info,
display_path: display_path.to_owned(),
file_format: FileFormat::Binary,
lhs_src: FileContent::Binary,
@@ -296,7 +296,7 @@ fn diff_file(
diff_file_content(
display_path,
- old_path,
+ extra_info,
lhs_path,
rhs_path,
&lhs_src,
@@ -310,7 +310,7 @@ fn diff_file(
fn check_only_text(
file_format: &FileFormat,
display_path: &str,
- old_path: Option<String>,
+ extra_info: Option<String>,
lhs_src: &str,
rhs_src: &str,
) -> DiffResult {
@@ -318,7 +318,7 @@ fn check_only_text(
DiffResult {
display_path: display_path.to_string(),
- old_path,
+ extra_info,
file_format: file_format.clone(),
lhs_src: FileContent::Text(lhs_src.into()),
rhs_src: FileContent::Text(rhs_src.into()),
@@ -332,7 +332,7 @@ fn check_only_text(
fn diff_file_content(
display_path: &str,
- old_path: Option<String>,
+ extra_info: Option<String>,
_lhs_path: &FileArgument,
rhs_path: &FileArgument,
lhs_src: &str,
@@ -359,7 +359,7 @@ fn diff_file_content(
// If the two files are completely identical, return early
// rather than doing any more work.
return DiffResult {
- old_path,
+ extra_info,
display_path: display_path.to_string(),
file_format,
lhs_src: FileContent::Text("".into()),
@@ -376,7 +376,7 @@ fn diff_file_content(
None => {
let file_format = FileFormat::PlainText;
if diff_options.check_only {
- return check_only_text(&file_format, display_path, old_path, &lhs_src, &rhs_src);
+ return check_only_text(&file_format, display_path, extra_info, &lhs_src, &rhs_src);
}
let lhs_positions = line_parser::change_positions(&lhs_src, &rhs_src);
@@ -405,7 +405,7 @@ fn diff_file_content(
let has_syntactic_changes = lhs != rhs;
return DiffResult {
- old_path,
+ extra_info,
display_path: display_path.to_string(),
file_format,
lhs_src: FileContent::Text(lhs_src.to_owned()),
@@ -498,7 +498,7 @@ fn diff_file_content(
return check_only_text(
&file_format,
display_path,
- old_path,
+ extra_info,
&lhs_src,
&rhs_src,
);
@@ -522,7 +522,7 @@ fn diff_file_content(
return check_only_text(
&file_format,
display_path,
- old_path,
+ extra_info,
&lhs_src,
&rhs_src,
);
@@ -551,7 +551,7 @@ fn diff_file_content(
let has_syntactic_changes = !hunks.is_empty();
DiffResult {
- old_path,
+ extra_info,
display_path: display_path.to_string(),
file_format,
lhs_src: FileContent::Text(lhs_src.to_owned()),
@@ -616,7 +616,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
"{}",
display::style::header(
&summary.display_path,
- &summary.old_path,
+ &summary.extra_info,
1,
1,
&summary.file_format,
@@ -643,7 +643,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
"{}",
display::style::header(
&summary.display_path,
- &summary.old_path,
+ &summary.extra_info,
1,
1,
&summary.file_format,
@@ -672,7 +672,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
&summary.rhs_positions,
hunks,
&summary.display_path,
- &summary.old_path,
+ &summary.extra_info,
&summary.file_format,
);
}
@@ -681,7 +681,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
hunks,
display_options,
&summary.display_path,
- &summary.old_path,
+ &summary.extra_info,
&summary.file_format,
lhs_src,
rhs_src,
@@ -697,7 +697,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
"{}",
display::style::header(
&summary.display_path,
- &summary.old_path,
+ &summary.extra_info,
1,
1,
&FileFormat::Binary,
@@ -718,7 +718,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
"{}",
display::style::header(
&summary.display_path,
- &summary.old_path,
+ &summary.extra_info,
1,
1,
&FileFormat::Binary,
diff --git a/src/options.rs b/src/options.rs
index 89582bb81..8752f5549 100644
--- a/src/options.rs
+++ b/src/options.rs
@@ -603,12 +603,13 @@ pub fn parse_args() -> Mode {
let old_name = old_name.to_string_lossy().to_string();
let new_name = new_name.to_string_lossy().to_string();
+ let renamed = format!("Renamed from {} to {}", old_name, new_name);
(
new_name,
FileArgument::from_path_argument(lhs_tmp_file),
FileArgument::from_path_argument(rhs_tmp_file),
- Some(old_name),
+ Some(renamed),
true,
)
}
diff --git a/src/summary.rs b/src/summary.rs
index f889d89f9..c5695086a 100644
--- a/src/summary.rs
+++ b/src/summary.rs
@@ -38,7 +38,9 @@ impl Display for FileFormat {
#[derive(Debug)]
pub struct DiffResult {
pub display_path: String,
- pub old_path: Option<String>,
+ /// Additional information to display about this file, such as
+ /// "Renamed from x.js to y.js".
+ pub extra_info: Option<String>,
pub file_format: FileFormat,
pub lhs_src: FileContent,