6

I am working on a Magento module that requires a quick fix. I assume I can fix it by creating Magento 2 "patch" way.

I have never created a patch file before, can anyone help me to create a patch file for a custom module?

22

I use this method for creating patch, hope it helps . Let suppose you will modify this file

vendor/magento/module-customer/Block/CustomerData.php

Create a copy of that file with your changes on it:

vendor/magento/module-customer/Block/CustomerDataModified.php

What you need to do is run this command:

diff -u CustomerData.php CustomerDataModified.php > diff.patch

Move diff.patch in your root under a directory, example Mypatches dir . Delete the file added CustomerDataModified.php since the patch is generated with the changes.

Here comes the tricky part , needs some manual modification now : When you open the diff.patch you will get something like this on the top :

--- CustomerData.php    2018-02-21 01:26:16.000000000 -0500
+++ CustomerDataModified.php    2019-01-03 03:57:47.326011737 -0500

Replace those line with this one:

diff --git a/Block/CustomerData.php b/Block/CustomerData.php
index 3ee2rd..8349152 111644
--- a/Block/CustomerData.php
+++ b/Block/CustomerData.php

The index is needed (the numbers are generated by me random, by default are generated from git but in most cases the vendor is in .gitignore)

Next Step is modification of the composer.json in the root of your magento : Add the extra section (if you dont have one already)

"extra": {
        "magento-force": "override",
        "patches": {
            "magento/module-customer": {
                "some description abt issue applying this patch": "Mypatches/diff.patch"
            }
        }
    }

And there you go . Your patch is diff.patch (you can call whatever you like ) . Run composer install to apply that

| improve this answer | |
  • 1
    Let me know if it works for you . – Ylgen Guxholli Jan 3 '19 at 12:35
  • 2
    This works exactly what I am looking for. Thanks dude. – Charlie Jan 7 '19 at 10:34
  • 1
    Glad it helped :). – Ylgen Guxholli Jan 7 '19 at 10:37
  • 1
    Easily Understanable @YlgenGuxholli, All type of patches generated using this formula ? – Hardik Makwana Aug 10 '19 at 6:36
  • 2
    Works as expected. Many thanks, Ylgen! – Sergey Uskov Feb 6 at 10:39
3

Here is my answer, i hope it will help someone:

First make sure that this package is installed via composer: cweagans/composer-patches

Lets say you want to apply a patch from a third-party module installed via composer for a controller. Please make sure that you have this part in the composer.json file:

"extra": {
    "magento-force": true,
    "composer-exit-on-patch-failure": true,
    "patches": {
        "vendorname/module-somerandomname": {
            "Description here": "patches/my_patch_name.patch"
        }
    }
},

Assuming that your vendor folder is in the .gitignore file, you can still do:

  1. git add -f vendor/vendorname/module-somerandomname/Controller/Myfile.php
  2. Do your changes to the Myfile.php
  3. Do a git diff vendor/vendorname/module-somerandomname/Controller/Myfile.php > patches/my_patch_name.patch
  4. git reset HEAD vendor/vendorname/module-somerandomname/Controller/Myfile.php
  5. Do a composer install
| improve this answer | |
1

For creating a patch, most of the people are using Github

At git, it is easy to create a patch for a commit. If you want to create the patch for a commit 452 then use git format-patch -1 {commitId}

Checkout below blogs:

https://coderwall.com/p/6aw72a/creating-patch-from-github-pull-request https://stackoverflow.com/questions/6658313/generate-a-git-patch-for-a-specific-commit https://gist.github.com/emmanueltissera/19d0a8852f000cde13768dd7420a0906

For magento, you can use below steps describe by magento

https://support.magento.com/hc/en-us/articles/360005484154-Create-a-patch-for-a-Magento-2-Composer-installation-from-a-GitHub-commit

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.