Merge pull request #403 from dandavison/392-copied-files

Handle copied files
This commit is contained in:
Dan Davison 2020-11-22 20:41:27 -05:00 committed by GitHub
commit 792d3189be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 2 deletions

View File

@ -437,6 +437,10 @@ pub struct Opt {
/// Text to display in front of a added file path.
pub file_added_label: String,
#[structopt(long = "file-copied-label", default_value = "copied:")]
/// Text to display in front of a copied file path.
pub file_copied_label: String,
#[structopt(long = "file-renamed-label", default_value = "renamed:")]
/// Text to display in front of a renamed file path.
pub file_renamed_label: String,

View File

@ -23,6 +23,7 @@ pub struct Config {
pub commit_style: Style,
pub decorations_width: cli::Width,
pub file_added_label: String,
pub file_copied_label: String,
pub file_modified_label: String,
pub file_removed_label: String,
pub file_renamed_label: String,
@ -151,6 +152,7 @@ impl From<cli::Opt> for Config {
commit_style,
decorations_width: opt.computed.decorations_width,
file_added_label: opt.file_added_label,
file_copied_label: opt.file_copied_label,
file_modified_label: opt.file_modified_label,
file_removed_label: opt.file_removed_label,
file_renamed_label: opt.file_renamed_label,

View File

@ -98,7 +98,9 @@ where
state = State::FileMeta;
handled_file_meta_header_line_file_pair = None;
} else if (state == State::FileMeta || source == Source::DiffUnified)
&& (line.starts_with("--- ") || line.starts_with("rename from "))
&& (line.starts_with("--- ")
|| line.starts_with("rename from ")
|| line.starts_with("copy from "))
{
let parsed_file_meta_line =
parse::parse_file_meta_line(&line, source == Source::GitDiff);
@ -114,7 +116,9 @@ where
));
}
} else if (state == State::FileMeta || source == Source::DiffUnified)
&& (line.starts_with("+++ ") || line.starts_with("rename to "))
&& (line.starts_with("+++ ")
|| line.starts_with("rename to ")
|| line.starts_with("copy to "))
{
let parsed_file_meta_line =
parse::parse_file_meta_line(&line, source == Source::GitDiff);

View File

@ -129,6 +129,7 @@ pub fn set_options(
commit_decoration_style,
commit_style,
file_added_label,
file_copied_label,
file_decoration_style,
file_modified_label,
file_removed_label,

View File

@ -22,6 +22,7 @@ pub fn get_file_extension_from_marker_line(line: &str) -> Option<&str> {
#[derive(Debug, PartialEq)]
pub enum FileEvent {
Change,
Copy,
Rename,
NoEvent,
}
@ -47,6 +48,12 @@ pub fn parse_file_meta_line(line: &str, git_diff_name: bool) -> (String, FileEve
line if line.starts_with("rename to ") => {
(line[10..].to_string(), FileEvent::Rename) // "rename to ".len()
}
line if line.starts_with("copy from ") => {
(line[10..].to_string(), FileEvent::Copy) // "copy from ".len()
}
line if line.starts_with("copy to ") => {
(line[8..].to_string(), FileEvent::Copy) // "copy to ".len()
}
_ => ("".to_string(), FileEvent::NoEvent),
}
}
@ -103,6 +110,7 @@ pub fn get_file_change_description_from_file_paths(
"{}{} ⟶ {}",
format_label(match file_event {
FileEvent::Rename => &config.file_renamed_label,
FileEvent::Copy => &config.file_copied_label,
_ => "",
}),
format_file(minus_file),

View File

@ -46,6 +46,17 @@ mod tests {
));
}
#[test]
fn test_copied_file() {
let config = integration_test_utils::make_config_from_args(&[]);
let output = integration_test_utils::run_delta(GIT_DIFF_WITH_COPIED_FILE, &config);
let output = strip_ansi_codes(&output);
assert!(test_utils::contains_once(
&output,
"\ncopied: first_file ⟶ copied_file\n"
));
}
#[test]
fn test_renamed_file_with_changes() {
let config = integration_test_utils::make_config_from_args(&[]);
@ -1656,6 +1667,19 @@ diff --git a/foo b/foo
new file mode 100644
index 0000000..b572921
Binary files /dev/null and b/foo differ
";
const GIT_DIFF_WITH_COPIED_FILE: &str = "
commit f600ed5ced4d98295ffa97571ed240cd86c34ac6 (HEAD -> master)
Author: Dan Davison <dandavison7@gmail.com>
Date: Fri Nov 20 20:18:30 2020 -0500
copy
diff --git a/first_file b/copied_file
similarity index 100%
copy from first_file
copy to copied_file
";
// git --no-pager show -p --cc --format= --numstat --stat