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?
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?
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
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:
git add -f vendor/vendorname/module-somerandomname/Controller/Myfile.php
git diff vendor/vendorname/module-somerandomname/Controller/Myfile.php > patches/my_patch_name.patch
git reset HEAD vendor/vendorname/module-somerandomname/Controller/Myfile.php
composer install
Add the file you want to modify::
git add vendor/magento/module-customer/Block/CustomerData.php -f
make changes to the original file
vendor/magento/module-customer/Block/CustomerData.php
git diff
vendor/magento/module-customer/Block/CustomerData.php > yourPatchName.patch
you will get the patch file (yourPatchName.patch) in the root directory
remove the changes you added to the original file manually by using ctrl+z
vendor/magento/module-customer/Block/CustomerData.php
apply the patch using::
git apply yourPatchName.patch
you will notice the changes in the original file
Here is the simpliest way to get what you need.
Create a copy of file(f5) and change lines you need, then git diff
this is option you need --no-index
Example:
git diff --no-index vendor/magento/module-cms/Model/PageRepository.php vendor/magento/module-cms/Model/PageRepositoryNew.php
will show you exactly what you need for patch. After that change PageRepositoryNew.php to PageRepository.php in patch.
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
If you are using Magento Cloud, you can create your patch following @Ylgen method, then move the patch to the m2-hotfixes/ folder and run:
php vendor/bin/ece-patches apply
php bin/magento cache:clean
Documentation:
https://devdocs.magento.com/cloud/project/project-patch.html#apply-a-patch-in-a-local-environment
Cloud is not accepting to move vendor files to git.
So i created file in app/code folder like
app/code/vendor/magento/module-customer/Block/CustomerData.php
added to git then committed. Not pushed the changes to any branch.
Then updated the modified code then executed the git diff command. git diff app/code/vendor/magento/module-customer/Block/CustomerData.php
It showed the below code. diff --git a/app/code/vendor/magento/module-customer/Block/CustomerData.php b/app/code/vendor/magento/module-customer/Block/CustomerData.php index 3ee2rd..8349152 111644 --- a/app/code/vendor/magento/module-customer/Block/CustomerData.php +++ b/app/code/vendor/magento/module-customer/Block/CustomerData.php
I copied that above code and added in customer.patch file then removed the app/code/ code.
uploaded the customer.patch file from hotfixes folder then moved to the git.
Then remove the file from app/code/vendor/magento/module-customer/Block/CustomerData.php location then commit again.
The two answers with the most upvotes work like a charm.
In case somebody else is banging his head why it does not apply the patches, you might want to install the composer package that actually applies the patches.
composer require cweagans/composer-patches
After this composer will apply the patches
More information can be found here: https://experienceleague.adobe.com/docs/commerce-operations/upgrade-guide/patches/apply.html?lang=en#composer
if you're using phpstorm you can simply right click on a file from local changes and select "Create patch from local changes"
Try this code to create a custom patch
Step-1 Run the below command to install the patch package.
composer require cweagans/composer-patches
Step-2 Add the below lines in your main Magento's composer.json file
"extra": {
"magento-force": "override",
"patches-file": "composer-patches.json",
"composer-exit-on-patch-failure": true
}
Step-3 Create patch file using this command
diff -u path/originalFile.php path/updateFile.php > your_patch_file_name.patch
I copy the original file (system.js) and paste it at the same location with a different name (systemUpdate.js). I add a new modification in a new file (systemUpdate.js). then I run the below command
diff -u vendor/paypal/module-braintree-core/view/adminhtml/web/js/system.js vendor/paypal/module-braintree-core/view/adminhtml/web/js/systemUpdate.js > diff.patch
You got diff.patch file which is your patch now updates the file path and file name. Because the file name and path must be the same. also add a and b bfore the file path.
Example: Fix_Store_Config_Issue.patch
--- a/vendor/paypal/module-braintree-core/view/adminhtml/web/js/system.js 2023-01-25 22:36:53.170211591 +0530
+++ b/vendor/paypal/module-braintree-core/view/adminhtml/web/js/system.js 2022-09-12 14:45:06.000000000 +0530
@@ -125,7 +125,7 @@
let messagingLogoPosition = $('.' + location + '-messaging-logo-position').val();
let messagingTextColor = $('.' + location + '-messaging-text-color').val();
- locations.each(function (loc) {
+ $.each(locations, function (loc) {
buttonTypes.each(function (type) {
$('[data-ui-id="select-groups-braintree-section-groups-braintree-groups-braintree-paypal-groups-styling-groups-button-' + loc + '-groups-button-location-' + loc + '-type-' + type + '-fields-button-location-' + loc + '-type-' + type + '-show-value"]').val(buttonShowStatus).click();
$('[data-ui-id="select-groups-braintree-section-groups-braintree-groups-braintree-paypal-groups-styling-groups-button-' + loc + '-groups-button-location-' + loc + '-type-' + type + '-fields-button-location-' + loc + '-type-' + type + '-layout-value"]').val(buttonLayout).click();
@@ -150,7 +150,7 @@
let locations = ['checkout', 'productpage', 'cart'], buttonTypes = ['paypal', 'paylater', 'credit'];
let buttonShowStatus = 1, buttonLayout = 'horizontal', buttonTagline = 0, buttonLabel = 'paypal', buttonColor = 'gold', buttonShape = 'rect', buttonSize = 'responsive';
- locations.each(function (loc) {
+ $.each(locations, function (loc) {
buttonTypes.each(function (type) {
$('[data-ui-id="select-groups-braintree-section-groups-braintree-groups-braintree-paypal-groups-styling-groups-button-' + loc + '-groups-button-location-' + loc + '-type-' + type + '-fields-button-location-' + loc + '-type-' + type + '-show-value"]').val(buttonShowStatus).click();
$('[data-ui-id="select-groups-braintree-section-groups-braintree-groups-braintree-paypal-groups-styling-groups-button-' + loc + '-groups-button-location-' + loc + '-type-' + type + '-fields-button-location-' + loc + '-type-' + type + '-layout-value"]').val(buttonLayout).click();
@@ -174,7 +174,7 @@
window.applyButton = function () {
let locations = ['checkout', 'productpage', 'cart'], buttonTypes = ['paypal', 'paylater', 'credit'];
- locations.each(function (loc) {
+ $.each(locations, function (loc) {
buttonTypes.each(function (type) {
$('[data-ui-id="select-groups-braintree-section-groups-braintree-groups-braintree-paypal-groups-styling-groups-button-' + loc + '-groups-button-location-' + loc + '-type-' + type + '-fields-button-location-' + loc + '-type-' + type + '-show-value"]').click();
$('[data-ui-id="select-groups-braintree-section-groups-braintree-groups-braintree-paypal-groups-styling-groups-button-' + loc + '-groups-button-location-' + loc + '-type-' + type + '-fields-button-location-' + loc + '-type-' + type + '-layout-value"]').click();
@@ -200,7 +200,7 @@
$('[data-ui-id="select-groups-braintree-section-groups-braintree-groups-braintree-paypal-groups-styling-fields-payment-location-value"]').change(function () {
hidePaypalSections();
});
- locations.each(function (loc) {
+ $.each(locations, function (loc) {
$('[data-ui-id="select-groups-braintree-section-groups-braintree-groups-braintree-paypal-groups-styling-groups-button-'+loc+'-fields-paypal-location-'+loc+'-button-type-value"]').change(function () {
hidePaypalSections();
});
@@ -215,7 +215,7 @@
}
merchantCountryIndex = mainLocation.attr('id').split('_')[1];
mainType = $('[data-ui-id="select-groups-braintree-section-groups-braintree-groups-braintree-paypal-groups-styling-groups-button-'+mainLocation.val()+'-fields-paypal-location-'+mainLocation.val()+'-button-type-value"]');
- locations.each(function (loc) {
+ $.each(locations, function (loc) {
$('#row_payment_' + merchantCountryIndex + '_braintree_section_braintree_braintree_paypal_styling_button_' + loc).hide();
buttonTypes.each(function (type) {
$('#row_payment_'+merchantCountryIndex+'_braintree_section_braintree_braintree_paypal_styling_button_'+loc+'_button_location_'+loc+'_type_' + type).hide();
Step-4 Create new folder at magentoRoot -> patches And add your path file here. (magentoRoot/patches/yourfile)
Step-5 Create a new file at the root of magento -> composer-patches.json
{
"patches": {
"paypal/module-braintree-core": {
"Fix Store Configuration Tab Issue": "patches/Fix_Store_Config_Issue.patch"
}
}
}
here you can add your module packages name. In vendor path. Examples are paypal/module-braintree-core, magento/module-catalog, etc. You can find your package name in the module's composer.json file
Step-6 Run the below command
composer install
composer update