Only print rename information when we're called from git

If the user explicitly runs `$ difft old.js new.js` there's no point
talking about renames.
pull/286/head
Wilfred Hughes 2022-05-10 09:37:36 +07:00
parent 8e48c303dc
commit 902c30f6cf
5 changed files with 21 additions and 2 deletions

@ -57,7 +57,9 @@ pub fn print(
i + 1,
hunks.len(),
lang_name,
// TODO: Just pass display_options.
display_options.use_color,
display_options.in_vcs,
display_options.background_color
)
);

@ -422,6 +422,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
1,
&lang_name,
display_options.use_color,
display_options.in_vcs,
display_options.background_color
)
);
@ -477,6 +478,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
1,
"binary",
display_options.use_color,
display_options.in_vcs,
display_options.background_color
)
);
@ -498,6 +500,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
1,
"binary",
display_options.use_color,
display_options.in_vcs,
display_options.background_color
)
);

@ -27,6 +27,7 @@ pub struct DisplayOptions {
pub print_unchanged: bool,
pub tab_width: usize,
pub display_width: usize,
pub in_vcs: bool,
pub syntax_highlight: bool,
}
@ -232,12 +233,13 @@ pub fn parse_args() -> Mode {
info!("CLI arguments: {:?}", args);
// TODO: document these different ways of calling difftastic.
let (lhs_display_path, rhs_display_path, lhs_path, rhs_path) = match &args[..] {
let (lhs_display_path, rhs_display_path, lhs_path, rhs_path, in_vcs) = match &args[..] {
[lhs_path, rhs_path] => (
lhs_path.to_owned(),
rhs_path.to_owned(),
lhs_path.to_owned(),
rhs_path.to_owned(),
false,
),
[display_path, lhs_tmp_file, _lhs_hash, _lhs_mode, rhs_tmp_file, _rhs_hash, _rhs_mode] => {
// https://git-scm.com/docs/git#Documentation/git.txt-codeGITEXTERNALDIFFcode
@ -246,6 +248,7 @@ pub fn parse_args() -> Mode {
display_path.to_owned(),
lhs_tmp_file.to_owned(),
rhs_tmp_file.to_owned(),
true,
)
}
[old_name, lhs_tmp_file, _lhs_hash, _lhs_mode, rhs_tmp_file, _rhs_hash, _rhs_mode, new_name, _similarity] =>
@ -257,6 +260,7 @@ pub fn parse_args() -> Mode {
new_name.to_owned(),
lhs_tmp_file.to_owned(),
rhs_tmp_file.to_owned(),
true,
)
}
_ => {
@ -349,6 +353,7 @@ pub fn parse_args() -> Mode {
display_mode,
display_width,
syntax_highlight,
in_vcs,
};
Mode::Diff {

@ -84,6 +84,7 @@ fn display_single_column(
src: &str,
is_lhs: bool,
use_color: bool,
in_vcs: bool,
background: BackgroundColor,
) -> String {
let column_width = format_line_num(src.lines().count().into()).len();
@ -96,6 +97,7 @@ fn display_single_column(
1,
lang_name,
use_color,
in_vcs,
background,
));
result.push('\n');
@ -341,6 +343,7 @@ pub fn print(
&rhs_colored_src,
false,
display_options.use_color,
display_options.in_vcs,
display_options.background_color
)
);
@ -355,7 +358,9 @@ pub fn print(
lang_name,
&lhs_colored_src,
true,
// TODO: pass display_options directly.
display_options.use_color,
display_options.in_vcs,
display_options.background_color
)
);
@ -396,6 +401,7 @@ pub fn print(
hunks.len(),
lang_name,
display_options.use_color,
display_options.in_vcs,
display_options.background_color
)
);
@ -640,6 +646,7 @@ mod tests {
"print(123)\n",
false,
true,
false,
BackgroundColor::Dark,
);
assert!(res.len() > 10);
@ -720,6 +727,7 @@ mod tests {
tab_width: 8,
display_width: 80,
syntax_highlight: true,
in_vcs: true,
};
// Simple smoke test.

@ -344,6 +344,7 @@ pub fn header(
hunk_total: usize,
language_name: &str,
use_color: bool,
in_vcs: bool,
background: BackgroundColor,
) -> String {
let divider = if hunk_total == 1 {
@ -353,7 +354,7 @@ pub fn header(
};
let rhs_path_pretty = apply_header_color(rhs_display_path, use_color, background);
if hunk_num == 1 && lhs_display_path != rhs_display_path {
if hunk_num == 1 && lhs_display_path != rhs_display_path && in_vcs {
let lhs_path_pretty = apply_header_color(lhs_display_path, use_color, background);
let renamed = format!("Renamed {} to {}", lhs_path_pretty, rhs_path_pretty,);
format!(