patchy icon indicating copy to clipboard operation
patchy copied to clipboard

Windows line endings under Mingw as installed by Git

Open tolomea opened this issue 7 years ago • 5 comments

Sooo, I'm using Python 3.6.1 on a Win10 machine, inside the Mingw Bash environment that comes with the Windows Git client and I get:

ValueError: Could not apply the patch to 'get_meta_options'. The message from `patch` was:
patching file 'C:\Users\tolomea\AppData\Local\Temp\patchy5nv4mk4f\get_meta_options.py'
Hunk #1 FAILED at 13 (different line endings).

Using hexdump I can confirm that the original source file for the target function has \n line endings and the Python file invoking Patchy and everything in Patchy's temp directory have \r\n line endings.

Hacking the Patchy source to pass --binary to patch seems to fix the issue although I don't really understand why.

tolomea avatar Jul 21 '17 14:07 tolomea

Sounds to me like we should normalize the source and the patch to \n line endings before saving and calling patch.

adamchainz avatar Jul 21 '17 14:07 adamchainz

The --binary flag caused problems with blank lines in the patches, patch in general seems to have some kind of issue with blank lines and windows line endings, so instead I did this:

@@ -90,8 +90,8 @@
             source_file.write(source)

         patch_path = os.path.join(tempdir, name + '.patch')
-        with open(patch_path, 'w') as patch_file:
-            patch_file.write(patch_text)
+        with open(patch_path, 'wb') as patch_file:
+            patch_file.write(patch_text.encode('utf-8'))
             if not patch_text.endswith('\n'):
                 patch_file.write('\n')

which forces the patch file to be written with unix line endings.

tolomea avatar Jul 26 '17 09:07 tolomea

Of course that might break things for people running on windows without mingw, although will patchy work at all for them?

tolomea avatar Jul 26 '17 09:07 tolomea

¯\_(ツ)_/¯

adamchainz avatar Jul 26 '17 14:07 adamchainz

I have been using that patch both in the windows environment and Ubuntu for over a year with no issues

tolomea avatar Feb 15 '19 06:02 tolomea