Contributing simple patches to Drupal.org using Git

Contributing simple patches to Drupal.org using Git

Posted 06/03/2011 - 21:20 by State68

The Drupal community thrives on being a "do-ocracy": if you encounter a problem with some aspect of Drupal - a module, a theme, or Drupal core itself, for example - you can, and should, try to fix the problem yourself.

This concept can be daunting if you're new to Drupal and lack - or think you lack - the technical expertise or confidence to work in this way. If you think this describes you, then check out this recent article by Karen Stevenson of Lullabot which shows you how to effectively report bugs or other issues to the Drupal community.

This post is about something a bit more technical: contributing a piece of code, or "patch", to the Drupal community, which actually fixes a bug or issue you or someone else has experienced. Drupal uses a piece of software called Git to create these patches: if you've not already got Git installed on your computer, then take a look at my earlier blog post if you're using a Mac, or this page on Drupal.org if you're working in a Windows or Linux environment.

The first thing you'll need to do is to find a bug or issue to fix. We'll use this issue as an example. When I was glancing through the code for the Drupal 7 version of the Wysiwyg module, I noticed that the README file that showed how to install the module hadn't been updated from its Drupal 6 version. I've attached a version of the file that includes the error to this post. Note that line 21 reads

Install as usual, see http://drupal.org/node/70151 for further information.

Rather than

Install as usual, see http://drupal.org/documentation/install/modules-themes/modules-7 for further information.

To go about fixing this, you'll need a copy of the code for the Wysiwyg module. You can get this by running the following command from the command line:

git clone --branch master http://git.drupal.org/project/wysiwyg.git

What this means is, 'make a copy - or clone - of the git repository for the wysiwyg module that is hosted on Drupal.org, using the "master" branch'. A git repository is where code is stored, and a branch is a sub-division of a given repository: for example, the repository for wysiwyg.module has branches named 'master', where the code for the most recent version of the Drupal 7 version of the module is stored, '6.x-3.x', '5.x-3.x', and a few others. Git branches can be little tricky to understand, so if you'd like a tutorial covering this topic then post a comment and I'll write one up.

Once the above command has been executed, a copy of the Wysiwyg module will be downloaded in a folder called "wysiwyg". Change into that folder by running

cd wysiwyg

from the command line.

Next, you'll need to run the following command to create a separate, local branch of the repository that you've downloaded.

git checkout -b 1173476-Error-in-README.txt

Now make the changes to the code: in other words, change line 21 so that it reads

Install as usual, see http://drupal.org/documentation/install/modules-themes/modules-7 for further information.

Save your changes, then go back to the command line and run this command:

git commit -a -m "Issue #1173476: Corrected line 21 of README.txt."

This tells Git that you're happy with the changes you've made to your local copy of the code. Next, grab a new copy of the Git repository that is stored on Drupal.org, just in case it's changed since the last time you downloaded it:

git fetch origin

Then "rebase" your local code so that it applies to the latest version of the code from Drupal.org (for more on what rebasing does, have a look at this article by Nick Quaranto of gitready.com):

git rebase origin/master

You're now ready to create your patch by running the following command:

git format-patch origin/master --stdout > Error-in-README.txt-1173476.patch

Now you can upload your patch to the Drupal.org issue queue!

If you made more than one

If you made more than one commit, why not still use format-patch? That way authorship is preserved and it's easier to review the work done in smaller incremental chunks.

--deviantintegral

Posted by Anonymous on Sat, 06/04/2011 - 02:16
Thanks for pointing this out

Thanks for pointing this out - I lifted that section of the tutorial wholesale from the documentation on Drupal.org, but having reviewed it you are, of course, correct; I've changed the tutorial to account for this.

I also found http://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git/ which seems useful.

Posted by State68 on Sat, 06/11/2011 - 22:58