Creating patches

In this recipe, we'll learn how to make patches out of commits. Patches can be sent via e-mails for quick sharing or copied to sneakernet devices (USB sticks, memory cards, external hard disk drives, and so on) if they need to be applied on an offline computer or the like. Patches can be useful methods to review code as the reviewer can apply the patch on his repository, investigate the diff, and check the program. If the reviewer decides the patch is good, he can publish (push) it to a public repository, given the reviewer is the maintainer of the repository. If the reviewer rejects the patch, he can simply reset his branch to the original state and inform the author of the patch that more work is needed before the patch can be accepted.

Getting ready

In this example, we'll clone and use a new repository. The repository is just an example repository for the Git commands and only contains some example commits:

$ git clone https://github.com/dvaske/offline-sharing.git
$ cd offline-sharing
$ git checkout master

How to do it...

Let's see the history of the repository as shown by Gitk:

$ gitk –-all

The history of the repository can be seen in the following screenshot:

How to do it...

There are three branches in the repository: master, develop, and doc. All of them differ by one or more commits to the others. On the master branch, we can now create a patch file for the latest commit on the master branch and store it in the latest-commit folder, as shown in the following command:

$ git format-patch -1 -o latest-commit
latest-commit/0001-Calculate-pi-with-more-digits.patch

If we look at the file created by the patch command, we will see the following command:

$ cat latest-commit/0001-Calculate-pi-with-more-digits.patch

From 1e42a2dfa3a377d412efd27a77b973c75935c62a Mon Sep 17 00:00:00 2001
From: Aske Olsson <[email protected]>
Date: Thu, 10 Apr 2014 09:19:29 +0200
Subject: [PATCH] Calculate pi with more digits

Dik T. Winter style

Build: gcc -Wall another_pi.c -o pi
Run: ./pi
---
 another_pi.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 another_pi.c

diff --git a/another_pi.c b/another_pi.c
new file mode 100644
index 0000000..86df41b
--- /dev/null
+++ b/another_pi.c
@@ -0,0 +1,21 @@
+/* Pi with 800 digits
+ * Dik T. Winter style, but modified sligthly
+ * https://crypto.stanford.edu/pbc/notes/pi/code.html
+ */
+ #include <stdio.h>
+
+void another_pi (void) {
+    printf("800 digits of pi:
");
+    int a=10000, b=0, c=2800, d=0, e=0, f[2801], g=0;
+    for ( ;b-c; )f[b++]=a/5;
+        for (;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
+            for (b=c; d+=f[b]*a, f[b]=d%--g,d/=g--,--b; d*=b);
+
+    printf("
");
+}
+
+int main (void){
+    another_pi();
+
+    return 0;
+}
--
1.9.1

The previous snippet is the content of the patch file produced. It contains a header much like an e-mail with the From, Date, and Subject fields, a body with the commit message and after the 3 dashes (---), the actual patch, and finally ending with 2 dashes (--), and the Git version used to generate the patch. The patch generated by git format-patch is in the UNIX mailbox format but with a magic fixed timestamp to identify that it comes from git format-patch rather than a real mailbox. You can see the time stamp in the first line after the sha-1 ID Mon Sep 17 00:00:00 2001.

How it works…

When generating the patch, Git will diff the commit at HEAD with its parent commit and use this diff as the patch. The -1 option tells Git only to generate patches for the last commit and -o latest-commit tells Git to store the patch in the folder latest-commit. The folder will be created if it does not exist.

There's more…

If you want to create patches for several commits, say the last 3 commits, you just pass on -3 to git format-patch instead of the -1.

Format the latest three commits as patches in the latest-commits folder:

$ git format-patch -3 -o latest-commits
latest-commits/0001-Adds-math-pi-calculation.patch
latest-commits/0002-Adds-checker-for-prime-number.patch
latest-commits/0003-Calculate-pi-with-more-digits.patch
$ ls -la latest-commits
total 24
drwxr-xr-x  5 aske  staff   170 Apr 11 21:55 .
drwxr-xr-x  8 aske  staff   272 Apr 11 21:55 ..
-rw-r--r--  1 aske  staff   676 Apr 11 21:55 0001-Adds-math-pi-calculation.patch
-rw-r--r--  1 aske  staff  1062 Apr 11 21:55 0002-Adds-checker-for-prime-number.patch
-rw-r--r--  1 aske  staff  1041 Apr 11 21:55 0003-Calculate-pi-with-more-digits.patch
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset