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.
syntax_id
Wilfred Hughes 2023-08-14 08:41:42 +07:00
parent f1ba399504
commit f06e95ca02
5 changed files with 29 additions and 27 deletions

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

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

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

@ -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,
)
}

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