Skip to content

Conversation

pt2121
Copy link
Contributor

@pt2121 pt2121 commented May 2, 2020

Fixes #139

@pt2121 pt2121 changed the title Fix panicking when diffing filenames with space Fix panicking when diffing filenames with space enclosed dashes May 2, 2020
@pt2121 pt2121 force-pushed the pt/139-diff branch 2 times, most recently from 406a716 to f709c2c Compare May 2, 2020 19:31
@dandavison
Copy link
Owner

dandavison commented May 3, 2020

Hi @pt2121 thanks very much for this!

Did you see #120 where we are trying to handle the diff.noprefix flag? For a user with that flag set, the strings won't have a/ and b/ prefixes. Do you have any thoughts about handling that? Does it look like we need to instead start using the lines starting --- and +++ (what delta calls "file meta" lines) as the source of the file extension?

Some example input is (git --no-pager diff --no-prefix)

diff --git src/pa rse.rs src/pa rse.rs
index dcbea72..a642b6b 100644
--- src/pa rse.rs
+++ src/pa rse.rs
diff --git src/parse.rs src/pa rse.rs
similarity index 100%
rename from src/parse.rs
rename to src/pa rse.rs
diff --git src/pa rse.rs src/pa rse.rs                                                                                                                                                                          
new file mode 100644                                                                                                                                                                                            
index 0000000..dcbea72                                                                                                                                                                                          
--- /dev/null                                                                                                                                                                                                   
+++ src/pa rse.rs

@pt2121
Copy link
Contributor Author

pt2121 commented May 3, 2020

Yes, sure. Thanks for the pointer. I'll look into it.

@dandavison
Copy link
Owner

Thanks!

* Fix panicking when diffing filenames with space enclosed dashes (dandavison#139).
* Handle the diff.noprefix flag.
@pt2121
Copy link
Contributor Author

pt2121 commented May 3, 2020

Hi @dandavison,
I updated my code as you suggested. The diff.noprefix flag should be now handled.
Please let me know what you think. Thanks!

Test get_file_extension_from_file_meta_line_file_path
assert_eq!(
get_file_extension_from_file_meta_line_file_path("Makefile"),
Some("Makefile")
);
Copy link
Owner

@dandavison dandavison May 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before I write anything else I want to make it clear that you've already done more than enough for your PR to be merged and it's very helpful, so thank you!

I don't have time right now to do further work on these edge cases myself so I'm just going to make some additional comments on the off chance that you want to double-check my thinking, since it's very helpful to have someone else familiar with this code.

Would you agree that this test case demonstrates a bug? I wonder if what we should do (in a different PR) is determine (programatically?) the cases where the sublime syntax definition is keyed off the entire filename (like Makefile, Dockerfile) and otherwise refuse to identify a file extension for random filenames that do not look like *.foo.

diff --git a/src/parse.rs b/src/parse.rs
index e1a0860..78cea85 100644
--- a/src/parse.rs
+++ b/src/parse.rs
@@ -123,6 +123,11 @@ mod tests {
             get_file_extension_from_file_meta_line_file_path("Makefile"),
             Some("Makefile")
         );
+        // TODO: The fact that the following test passes is perhaps a bug.
+        assert_eq!(
+            get_file_extension_from_file_meta_line_file_path("rs"),
+            Some("rs")
+        );
         assert_eq!(
             get_file_extension_from_file_meta_line_file_path("a/src/Makefile"),
             Some("Makefile")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. Nice catch.
I'm not sure if there is a good way to differentiate them unless we have a list of all file types that don't have an extension e.g. Makefile, Dockerfile.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping that the sublime syntax definitions might distinguish between genuine extensions, that involve a ., like foo.py, vs whole-file-name extensions like Makefile, Dockerfile. However, it looks like they don't. So I don't think this is delta's problem, seeing as bat also suffers from it:

image

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"
),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I believe that there's another bug remaining (not your fault or responsibility!), in that unified diff is still broken for file names with spaces:

--- "src/pa r se.rs"	2020-05-03 16:40:13.851935846 -0400
+++ "src/pa rse.rs"	2020-05-03 16:41:49.798457052 -0400
@@ -231,4 +231,7 @@
         );
     }
 }
-vjhvjj
+
+pub fn test(s: &str) {
+    println!("hello {}", s);
+}
image

If I remove the double quotes and spaces from those filenames, then delta syntax-highlights the added function as Rust:

--- src/parse.rs	2020-05-03 16:40:13.851935846 -0400
+++ src/parse.rs	2020-05-03 16:41:49.798457052 -0400
@@ -231,4 +231,7 @@
         );
     }
 }
-vjhvjj
+
+pub fn test(s: &str) {
+    println!("hello {}", s);
+}
image

@dandavison
Copy link
Owner

Thanks a lot for this work. I'll do a final review/test and merge soon.

@pt2121
Copy link
Contributor Author

pt2121 commented May 9, 2020

Have you had a chance to look at this? Anything else I should address in this PR?

@dandavison
Copy link
Owner

Anything else I should address in this PR?

No, it looks great, thanks very much!

@dandavison dandavison merged commit bd30692 into dandavison:master May 11, 2020
@pt2121 pt2121 deleted the pt/139-diff branch May 11, 2020 14:25
@pt2121
Copy link
Contributor Author

pt2121 commented May 11, 2020

No problem. I will try fixing the remaining bug when having time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Diff for filenames with space enclosed dashes panic

2 participants