April 2013

Using git format-patch to generate better patches for your Drupal projects

One thing that bothers me about how the developer documentation is how it suggests that you should use git diff to generate your patches. That's all well and good if you have a maintainer of a project who comments the commit properly, but most of the time that doesn't happen.

When you run git diff on a repo you get output that looks like this:

$ git diff 6e04143743 --color=never
diff --git a/drupal-org.make b/drupal-org.make
index 1fda437..9429e81 100644
--- a/drupal-org.make
+++ b/drupal-org.make
@@ -1,7 +1,7 @@
 api = 2
 core = 7.x

-projects[ctools][version] = "1.2"
+projects[ctools][version] = "1.3"
 projects[ctools][subdir] = contrib

 projects[date][version] = "2.6"

That certainly is a patch, but it tells you nothing about authored the code or when it was committed.

If you use git format-patch to generate the same diff you end up with something like this:

$ git format-patch 6e04143743 --stdout
From 15936028741df0038d0f2065f0c2b09bdf2a5bd3 Mon Sep 17 00:00:00 2001
From: Zach Seifts 
Date: Wed, 3 Apr 2013 15:41:01 -0400
Subject: [PATCH] Fixes issue #1960476, Update ctools module to 7.x-1.3

---
 drupal-org.make | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drupal-org.make b/drupal-org.make
index 1fda437..9429e81 100644
--- a/drupal-org.make
+++ b/drupal-org.make
@@ -1,7 +1,7 @@
 api = 2
 core = 7.x

-projects[ctools][version] = "1.2"
+projects[ctools][version] = "1.3"
 projects[ctools][subdir] = contrib

 projects[date][version] = "2.6"
--
1.8.2

You end up with a whole lot more information about the commit in your patch. This way when you use git am to apply the patch you get the actual commit with the proper person attributed for their work.