Tuesday, September 13, 2011

Create a Drupal Module Patch With Git

I thought I'd written my last bit of code over 10 years ago, but here I am struggling with all this new-fangled technology and inevitably tweaking module code that makes up the great Drupal open source empire.
A quick way to generate a patch for a module you want to tweak is using Drush and Git.

1. Download the module to your test environment with Drush. FFS don't do code development on a live site - kittens will die and you might (quite rightly) lose your job.
drush dl [module-name]

2. Navigate to directory and initialize an empty repository  
cd [module-name] 
git init

3. Add the files to new repository
git add *

4. Commit the files to the repository
git commit -m 'Initial commit'

5. Make changes to the file(s) you want to change within the module's directory.

6. Create patch - do this BEFORE committing changes  
git diff —no-prefix > patchfile.patch

7. Optionally commit your latest changes to Git  
git commit -m 'Description of what you've changed' 

Save the patch, submit it on an issue queue, set the issue status to 'Needs review' and hope that some kind person reviews it so it can be committed as a permanent feature. Fame and glory awaits.

This is taken from a post by the awesome Swedish Chef dudes at Node One. http://nodeone.se/blogg/how-to-create-a-patch-for-a-drupal-module. They talk about commits against core/contrib HEAD: not really my bag, but ignore the comments about CVS (CVS must die, etc).