Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

How to create a patch: Difference between revisions

From MoodleDocs
(15 intermediate revisions by 13 users not shown)
Line 1: Line 1:
If you have made some changes to the code that you would like to share with the community, particularly if you want to send them to one of the core developers for possible inclusion in Moodle Core, it is very helpful if you can provide them as a patch file. Sometimes also called a diff file.
If you have made some changes to the code that you would like to share with the community, particularly if you want to send them to one of the core developers for possible inclusion in Moodle Core, it is very helpful if you can provide them as a [[Patch|patch file]]. Sometimes also called a diff file.


This page explains how you can make a patch file. Patch is a standard format, and there are many options for how to create one. Pick the one that is easiest for you.
This page explains how you can make a patch file. Patch is a standard format, and there are many options for how to create one. Pick the one that is easiest for you.
'''
One of the most critical aspects of patch usage is identifying for your audience the location for the patch file for application relative to the -p flag! When you create patches note your location and provide that when you share patches. '''


''Note, I have not been able to test most of these instructions. They are about right, but I am hoping that as people use them, they will fill in any gaps, correct any details, and so on.[[User:Tim Hunt|Tim Hunt]] 08:40, 25 June 2007 (CDT)''
==Creating a patch using diff==


==Introduction: what does a patch file look like==
diff is the a linux command line program, and is where patch files originated. It requires that you have two copies of the code, one with your changes, and one without. Suppose these two copies are in folders called 'standard_moodle' and 'my_moodle' which are subdirectories of the current folder. Then to create the patch, type:


This is a simple example:
diff -Naur standard_moodle my_moodle > patch.txt


<pre><nowiki>
==Creating a patch using Eclipse==
Index: lang/en_utf8/quiz.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lang/en_utf8/quiz.php,v
retrieving revision 1.57.2.10
diff -u -r1.57.2.10 quiz.php
--- lang/en_utf8/quiz.php 29 May 2007 17:47:25 -0000 1.57.2.10
+++ lang/en_utf8/quiz.php 25 Jun 2007 12:58:34 -0000
@@ -252,7 +255,6 @@
$string['indivresp'] = 'Responses of Individuals to Each Item';
$string['info'] = 'Info';
$string['introduction'] = 'Introduction';
-$string['invalidcategory'] = 'Category ID is invalid';
$string['invalidnumericanswer'] = 'One of the answers you entered was not a valid number.';
$string['invalidnumerictolerance'] = 'One of the tolerances you entered was not a valid number.';
$string['invalidsource'] = 'The source is not accepted as valid.';
@@ -375,8 +377,10 @@
$string['questiontypesetupoptions'] = 'Setup options for question types:';
$string['quiz:attempt'] = 'Attempt quizzes';
$string['quiz:deleteattempts'] = 'Delete quiz attempts';
+$string['quiz:emailconfirmsubmission'] = 'Receive own quiz submission notification';
+$string['quiz:emailnotifysubmission'] = 'Receive student quiz submission notifications';
$string['quiz:grade'] = 'Grade quizzes manually';
-$string['quiz:ignoretimelimits'] = 'Ignores time limit on quizzes';
+$string['quiz:ignoretimelimits'] = 'Ignores time limit on quizs';
$string['quiz:manage'] = 'Manage quizzes';
$string['quiz:preview'] = 'Preview quizzes';
$string['quiz:view'] = 'View quiz information';
</nowiki></pre>


At the top it says which file is being affected. Changes to several files can be included in one patch. Lines added are shown with a '+', lines removed are shown with a '-', lines changed are shown as the old line being removed and the new one added.
See [[Setting_up_Eclipse#Creating_a_patch]]. Eclipse makes creating patches really easy, once you have got it set up correctly.


Patch files are good because they only show the changed parts of the file. This has two advantages: it easy to understand the change; and if other parts of the same files change between the patch being made an being used, there is no problem, the patch will still apply.
==Creating a patch using WinMerge==


==Creating a patch using diff==
[http://winmerge.org/ WinMerge] is a nice windows GUI for comparing folders. In this sense it is like the original command-line 'diff' program. You need a copy of 'standard_moodle' and 'my_moodle'. Use '''File -> Open...''' to open the two versions for comparison. This will give you a nice view of what you have changed. Then do '''Tools -> Generate patch ...'''. In the dialogue box, make sure you select Style: Unified in the Format box.


diff is the a linux command line program, and is where patch files originated. It requires that you have two copies of the code, one with your changes, and one without. Suppose these two copies are in folders called 'standard_moodle' and 'my_moodle' which are subdirectories of the current folder. Then to create the patch, type:
==Creating a patch using Git==


diff -ur standard_moodle my_moodle > patch.txt
Creating a patch if you're using Git for version control is similar to CVS, and similarly you don't need an unchanged copy of moodle to diff against.  


==Creating a patch using CVS (command line)==
There are several ways for creating a patch, the recommended one is using git format-patch (as mentioned in MDL-43119)


It is easier if you are using CVS to manage your development, because you don't need to keep the copy of 'standard_moodle'. CVS takes care of that for you. In you workspace (sandbox) type:
The easiest way to create a patch for the last commit is


  cvs diff -uN > patch.txt
  git show > patch.txt


==Creating a patch using Tortoise CVS==
or if you want to create a patch between 2 specific commits you can use git diff


[http://www.tortoisecvs.org/ Tortoise CVS] is a Windows GUI front end for CVS. To create a patch, right-click on a folder in CVS, and choose '''CVS -> Make patch ...''' from the context menu.
git diff ''commitid1'' ''commitid2'' > patch.txt


==Creating a patch using Eclipse==
There's also a tool, format-patch, for formatting a patch to send as an e-mail. You can create patches for the last ''n'' revisions like this:


See [[Setting_up_Eclipse#Creating_a_patch]]. Eclipse makes creating patches really easy, once you have got it set up correctly.
git format-patch -''n''


==Creating a patch using WinMerge==
which will create the patch in the current directory. The -o parameter allows you to specify a different output directory.


[http://winmerge.org/ WinMerge] is a nice windows GUI for comparing folders. It this sense it is like the original command-line 'diff' program. You need a copy of 'standard_moodle' and 'my_moodle'. Use '''File -> Open...''' to open the two versions for comparison. This will give you a nice view of what you have changed. Then do '''Tools -> Generate patch ...'''. In the dialogue box, make sure you select Style: Unified in the Format box.
==See Also==
* [http://drupal.org/node/1054616 Detailed instructions for creating patches using Git]:  [http://moodle.org/mod/forum/discuss.php?d=181953#p792050 shared by Frank Ralf]
* [[How_to_apply_a_patch]]
* [[Patch]]
* [https://www.youtube.com/watch?v=05BXj_Jeuzo Fixing a bug - Video tutorial by Tim Hunt]

Revision as of 20:23, 25 January 2015

If you have made some changes to the code that you would like to share with the community, particularly if you want to send them to one of the core developers for possible inclusion in Moodle Core, it is very helpful if you can provide them as a patch file. Sometimes also called a diff file.

This page explains how you can make a patch file. Patch is a standard format, and there are many options for how to create one. Pick the one that is easiest for you. One of the most critical aspects of patch usage is identifying for your audience the location for the patch file for application relative to the -p flag! When you create patches note your location and provide that when you share patches.

Creating a patch using diff

diff is the a linux command line program, and is where patch files originated. It requires that you have two copies of the code, one with your changes, and one without. Suppose these two copies are in folders called 'standard_moodle' and 'my_moodle' which are subdirectories of the current folder. Then to create the patch, type:

diff -Naur standard_moodle my_moodle > patch.txt

Creating a patch using Eclipse

See Setting_up_Eclipse#Creating_a_patch. Eclipse makes creating patches really easy, once you have got it set up correctly.

Creating a patch using WinMerge

WinMerge is a nice windows GUI for comparing folders. In this sense it is like the original command-line 'diff' program. You need a copy of 'standard_moodle' and 'my_moodle'. Use File -> Open... to open the two versions for comparison. This will give you a nice view of what you have changed. Then do Tools -> Generate patch .... In the dialogue box, make sure you select Style: Unified in the Format box.

Creating a patch using Git

Creating a patch if you're using Git for version control is similar to CVS, and similarly you don't need an unchanged copy of moodle to diff against.

There are several ways for creating a patch, the recommended one is using git format-patch (as mentioned in MDL-43119)

The easiest way to create a patch for the last commit is

git show > patch.txt

or if you want to create a patch between 2 specific commits you can use git diff

git diff commitid1 commitid2 > patch.txt

There's also a tool, format-patch, for formatting a patch to send as an e-mail. You can create patches for the last n revisions like this:

git format-patch -n

which will create the patch in the current directory. The -o parameter allows you to specify a different output directory.

See Also