summaryrefslogtreecommitdiffstats
path: root/src/parse.rs
diff options
context:
space:
mode:
authorPrat T <pt2121@users.noreply.github.com>2020-05-03 09:57:46 -0700
committerPrat T <pt2121@users.noreply.github.com>2020-05-03 10:25:44 -0700
commitdf3ecd09d3e7897403c46c5f1042ce82a783f1c5 (patch)
treea568e0389297826a7d33d5ee1e5d541edac00f08 /src/parse.rs
parent289763fd64cafa68251d80f5546c2629fa8b40ab (diff)
Use file meta lines as the source of the file extension.
* Fix panicking when diffing filenames with space enclosed dashes (#139). * Handle the diff.noprefix flag.
Diffstat (limited to 'src/parse.rs')
-rw-r--r--src/parse.rs61
1 files changed, 30 insertions, 31 deletions
diff --git a/src/parse.rs b/src/parse.rs
index 3d129ffa..48122d94 100644
--- a/src/parse.rs
+++ b/src/parse.rs
@@ -1,21 +1,6 @@
use std::path::Path;
/// Given input like
-/// "diff --git a/src/main.rs b/src/main.rs"
-/// Return "rs", i.e. a single file extension consistent with both files.
-pub fn get_file_extension_from_diff_line(line: &str) -> Option<&str> {
- match get_file_extensions_from_diff_line(line) {
- (Some(_ext1), Some(ext2)) => {
- // If they differ then it's a rename; use the new extension.
- Some(ext2)
- }
- (Some(ext1), None) => Some(ext1),
- (None, Some(ext2)) => Some(ext2),
- (None, None) => None,
- }
-}
-
-/// Given input like
/// "--- one.rs 2019-11-20 06:16:08.000000000 +0100"
/// Return "rs"
pub fn get_file_extension_from_marker_line(line: &str) -> Option<&str> {
@@ -51,6 +36,14 @@ pub fn get_file_path_from_file_meta_line(line: &str, git_diff_name: bool) -> Str
.to_string()
}
+pub fn get_file_extension_from_file_meta_line_file_path(path: &str) -> Option<&str> {
+ if path.is_empty() || path == "/dev/null" {
+ None
+ } else {
+ get_extension(&path).map(|ex| ex.trim())
+ }
+}
+
pub fn get_file_change_description_from_file_paths(
minus_file: &str,
plus_file: &str,
@@ -81,16 +74,6 @@ pub fn parse_hunk_metadata(line: &str) -> (&str, &str) {
(code_fragment, line_number)
}
-/// Given input like "diff --git a/src/main.rs b/src/main.rs"
-/// return ("rs", "rs").
-fn get_file_extensions_from_diff_line(line: &str) -> (Option<&str>, Option<&str>) {
- let mut iter = line.split(' ').skip(2);
- (
- iter.next().and_then(|s| get_extension(&s[2..])),
- iter.next().and_then(|s| get_extension(&s[2..])),
- )
-}
-
/// Attempt to parse input as a file path and return extension as a &str.
fn get_extension(s: &str) -> Option<&str> {
let path = Path::new(s);
@@ -105,21 +88,37 @@ mod tests {
use super::*;
#[test]
- fn test_get_file_extension_from_diff_line() {
+ fn test_get_file_extension_from_marker_line() {
assert_eq!(
- get_file_extension_from_diff_line("diff --git a/src/main.rs b/src/main.rs"),
+ get_file_extension_from_marker_line(
+ "--- src/one.rs 2019-11-20 06:47:56.000000000 +0100"
+ ),
Some("rs")
);
}
#[test]
- fn test_get_file_extension_from_marker_line() {
+ fn test_get_file_extension_from_file_meta_line() {
assert_eq!(
- get_file_extension_from_marker_line(
- "--- src/one.rs 2019-11-20 06:47:56.000000000 +0100"
- ),
+ get_file_extension_from_file_meta_line_file_path("a/src/parse.rs"),
Some("rs")
);
+ assert_eq!(
+ get_file_extension_from_file_meta_line_file_path("b/src/pa rse.rs"),
+ Some("rs")
+ );
+ assert_eq!(
+ get_file_extension_from_file_meta_line_file_path("src/pa rse.rs"),
+ Some("rs")
+ );
+ assert_eq!(
+ get_file_extension_from_file_meta_line_file_path("wat hello.rs"),
+ Some("rs")
+ );
+ assert_eq!(
+ get_file_extension_from_file_meta_line_file_path("/dev/null"),
+ None
+ );
}
// We should only strip the prefixes if they are "a/" or "b/". This will be correct except for