diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-11-22 20:41:27 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-22 20:41:27 -0500 |
commit | 792d3189bea4c203d85dd2cf20277b300b1f296d (patch) | |
tree | 18f360635cf17c3937f44a6ed463bf9cf48965c3 | |
parent | f52464c1af557a5759566a988d2478ccb6e86d3e (diff) | |
parent | af27c560f7f272bb70306461146984882afa8e56 (diff) |
Merge pull request #403 from dandavison/392-copied-files
Handle copied files
-rw-r--r-- | src/cli.rs | 4 | ||||
-rw-r--r-- | src/config.rs | 2 | ||||
-rw-r--r-- | src/delta.rs | 8 | ||||
-rw-r--r-- | src/options/set.rs | 1 | ||||
-rw-r--r-- | src/parse.rs | 8 | ||||
-rw-r--r-- | src/tests/test_example_diffs.rs | 24 |
6 files changed, 45 insertions, 2 deletions
@@ -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, diff --git a/src/config.rs b/src/config.rs index 9a26b677..28983b6d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, diff --git a/src/delta.rs b/src/delta.rs index 180a3893..af4106a3 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -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); diff --git a/src/options/set.rs b/src/options/set.rs index 67a5e21d..87eef7d4 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -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, diff --git a/src/parse.rs b/src/parse.rs index 05b0ceb5..7e39c772 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -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), diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index 022748df..2bea133d 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -47,6 +47,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(&[]); let output = integration_test_utils::run_delta(RENAMED_FILE_WITH_CHANGES_INPUT, &config); @@ -1658,6 +1669,19 @@ 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 // #121 const DIFF_WITH_UNRECOGNIZED_PRECEDING_MATERIAL_1: &str = " |