Skip to content

Commit

Permalink
Handle copied files
Browse files Browse the repository at this point in the history
Fixes #392
  • Loading branch information
dandavison committed Nov 23, 2020
1 parent c6dd835 commit af27c56
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/options/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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),
}
}
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit af27c56

Please sign in to comment.