Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue if trying to patch file with CRLF line ending #299

Closed
mamsincl opened this issue Feb 7, 2020 · 16 comments
Closed

Issue if trying to patch file with CRLF line ending #299

mamsincl opened this issue Feb 7, 2020 · 16 comments

Comments

@mamsincl
Copy link

mamsincl commented Feb 7, 2020

Hi cweagans,

Recently just faced with issue stated in the title.
The file I wanted to patch is using Windows standard new line characters (\r\n or CRLS). The main problem, that patch command itself stripping trailing CRs from patch so I am continuously getting different line endings error messages.
To prevent this we can use --binary switch on patch command, but as automated deployment, if I am patching your Patches class, that only will be available of the next run only ... and have to re-patch your patcher every time so the patching will failing over and over again.

Could be possible to add a settings/switch to composer.patch.json to use --binary option during patch ... something like

{
  "patches": {
    "magento/module-email": {
      "https://github.com/magento/magento2/pull/22469": "patches/vendor/magento/module-email/Model/Template.php.patch",
      "settings": [ 
          "binary": true/false(default if not present)
      ]
    }
  }
}

Thanks,
Robert Szeker
aka
Mammouth

@ilnytskyi
Copy link

Faced the same issue with LF CRLF

@heran
Copy link

heran commented Jun 29, 2021

same issue

@terra-yi
Copy link

same issue.

@yaroslav-qlicks
Copy link

same issue

@elvis-zunde
Copy link

same issue!

@artissegrums
Copy link

same issue

@swnikita
Copy link

same issue!

@agata-maksymiuk
Copy link

The problem is in your patch file and your editor stripping line endings.
I managed to solve this issue by creating a patch via command line, omitting PHP Storm, for example:
git diff 27706c 6fc369 > destination-dir/your-patch.patch
And it worked for me.
I'm using the latest composer patches module (1.7.2)

@mamsincl
Copy link
Author

hi @agata-maksymiuk

The problem is in your patch file and your editor stripping line endings. I managed to solve this issue by creating a patch via command line, omitting PHP Storm, for example: git diff 27706c 6fc369 > destination-dir/your-patch.patch And it worked for me. I'm using the latest composer patches module (1.7.2)

the issue is not with the patch file, but with the original file: if it has other than LF line endings, the patch command cannot apply the patch as the command itself strips trailing CRs from line endings causing the different line ending issue - not sure about command git apply: I am about 99% sure that for integrity point of view that also checking line ending, but the CRs might not getting stripped - in additional, git apply also having a [binary option|https://git-scm.com/docs/git-apply#Documentation/git-apply.txt---allow-binary-replacement] available which is always on for latest git

as per the 1.x the code only uses git apply if the install path of the package is a git repo, so for private repositories patching is falling back to the more general patch command where the line ending is getting flagged and failing to patch (understandable we are want to the patches be applied, so using "composer-exit-on-patch-failure": true flag is mandatory) - I am assuming you patched a module installed from a git repository directly this why you patch get applied with no issue

@mrrobotisback
Copy link

@mamsincl Hi i have a similar problem. File in vendor have crlf line ending and my patch have lf unix style file ending. I have put --binary to vendor/cweagans/composer-patches/src/Patches.php:410 like this:

    if ($patched = $this->executeCommand("patch %s --no-backup-if-mismatch --binary -d %s < %s", $patch_level, $install_path, $filename)) {

Whit this mod when run composer install i have another problem:

patch: **** malformed patch at line 10:

But my patch it's ok.

any idea ?

thanks in advance

cweagans/composer-patches version 1.7.2

@agata-maksymiuk
Copy link

@mamsincl My original file has windows style line endings (\n\r), and I've created patch file with exactly the same line endings. And it works for me. Patch is being applied with no error. Original file still has windows line endings and content is patched properly.

The error I got before was:

  - Applying patches for mageplaza/module-rma
    patches/composer/2.4.2-p1/mageplaza/module-rma/WP-982-webapi.patch (Webapi fix)
patch '-p1' --no-backup-if-mismatch -d '/var/www/page/vendor/mageplaza/module-rma' < '/var/www/page/patches/composer/2.4.2-p1/mageplaza/module-rma/WP-982-webapi.patch'
Executing command (CWD): patch '-p1' --no-backup-if-mismatch -d '/var/www/page/vendor/mageplaza/module-rma' < '/var/www/page/patches/composer/2.4.2-p1/mageplaza/module-rma/WP-982-webapi.patch'
patching file etc/webapi.xml

Hunk #1 FAILED at 147 (different line endings).
1 out of 1 hunk FAILED -- saving rejects to file etc/webapi.xml.rej

Or maybe I just don't understand your issue.

@mrrobotisback
Copy link

mrrobotisback commented May 19, 2022

Hi @agata-maksymiuk, but how do you convert ?

I try convert my patch with unix2dos command, but not work.

@agata-maksymiuk
Copy link

agata-maksymiuk commented May 19, 2022

@mrrobotisback I did not convert. I keep the same line endings in original file and in patch. And it works.
I've created patch by command (listed above) and it keeps the same line delimiter in patch file.
First of all I've created git repository in vendor module

git init
git add .
git commit -m "init"

then I did some changes in module (I used vim editor), commited them, and created patch from diff between commits, e.g.

git diff 27706c 6fc369 > destination-dir/name-your-patch.patch

@cweagans
Copy link
Owner

cweagans commented Feb 7, 2023

Binary patching will be supported in main soon. I recommend fixing your patch though.

@cweagans cweagans closed this as completed Feb 7, 2023
@cweagans cweagans mentioned this issue Feb 7, 2023
31 tasks
@aholovan
Copy link

aholovan commented Mar 1, 2023

Hi @agata-maksymiuk, but how do you convert ?

I try convert my patch with unix2dos command, but not work.

I was able to get working patch after convert by unix2dos - it's require edit the patch (by mcedit for example, etc..) and remove ^M from any patch structure line that don't related to code.

@Mercurieus
Copy link

I was able to get working patch after convert by unix2dos - it's require edit the patch (by mcedit for example, etc..) and remove ^M from any patch structure line that don't related to code.

You can simplify that to this one command to make only "unified diff" header using the LF line endings:

unix2dos your-patch.patch && awk '/^diff|^index|^---|^\+\+\+|^@@/{sub(/\r$/,"");}1' your-patch.patch > temp.patch && mv temp.patch your-patch.patch

Where your-patch.patch is your patch that should modify a file with the CRLF line endings.

I had a headache with this problem today. Hopefully, the solution will help you.

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

No branches or pull requests