Sending patches

In the previous example, you saw how to create and apply patches. You can, of course, attach these patch files directly to an e-mail, but Git provides a way to send the patches directly as e-mails with the git send-email command. The command requires some setting up, but how you do that is heavily dependent on your general mail and SMTP configuration. A general guide can be found in the Git help pages or visit http://git-scm.com/docs/git-send-email.

Getting ready

We'll set up the same repository as in the previous example:

$ git clone https://github.com/dvaske/offline-sharing.git
Cloning into 'offline-sharing'...
remote: Counting objects: 32, done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 32 (delta 7), reused 30 (delta 6)
Unpacking objects: 100% (32/32), done.
Checking connectivity... done.
$ cd offline-sharing

How to do it...

First, we'll send the same patch as the one we created in the first example. We'll send it to ourselves using the e-mail address we specified in our Git configuration. Let's create the patch again with git format-patch and send it with git send-email:

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

Save the e-mail address from the Git configuration to a variable as follows:

$ emailaddr=$(git config user.email)

Send the patch using the e-mail address in both the to and from fields:

$ git send-email --to $emailaddr --from $emailaddr latest-commit/0001-Calculate-pi-with-more-digits.patch
latest-commit/0001-Calculate-pi-with-more-digits.patch
(mbox) Adding cc: Aske Olsson <[email protected]> from line 'From: Aske Olsson <[email protected]>'
OK. Log says:
Server: smtp.gmail.com
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
From: [email protected]
To: [email protected]
Subject: [PATCH] Calculate pi with more digits
Date: Mon, 14 Apr 2014 09:00:11 +0200
Message-Id: <[email protected]>
X-Mailer: git-send-email 1.9.1

An e-mail check will reveal an e-mail in the inbox, as shown in the following screenshot:

How to do it...

How it works…

As we saw in the previous examples, git format-patch creates the patch files in the Unix mbox format, so only a little extra effort is required to allow Git to send the patch as an e-mail. When sending e-mails with git send-email, make sure your MUA (Mail User Agent) does not break the lines in the patch files, replace tabs with spaces, and so on. You can test this easily by sending a patch to yourself and checking whether it can be applied cleanly to your repository.

There's more…

The send-email command can of course be used to send more than one patch at a time. If a directory is specified instead of a single patch file, all the patches in that directory will be sent. We don't even have to generate the patch files before sending them; we can just specify the same range of revisions we want to send as we would have specified for the format-patch command. Then, Git will create the patches on the fly and send them. When we send a series of patches this way, it is good practice to create a cover letter with a bit of explanation about the patch series that follows. The cover letter can be created by passing --cover-letter to the send-email command. We'll try sending patches for the commits on develop since it is branched from master (the same patches as in the second example) as follows:

$ git checkout develop
Switched to branch 'develop'
Your branch is up-to-date with 'origin/develop'.
$ git send-email --to [email protected] --from  [email protected] --cover-letter --annotate origin/master
/tmp/path/for/patches/0000-cover-letter.patch
/tmp/path/for/patches/0001-Move-print-functionality-of-is_prime.patch
/tmp/path/for/patches/0002-Adds-Makefile-for-easy-building.patch
/tmp/path/for/patches/0003-Adds-functionality-to-prime-test-a-range-of-numbers.patch
(mbox) Adding cc: Aske Olsson <[email protected]> from line 'From: Aske Olsson <[email protected]>'
OK. Log says:
Server: smtp.gmail.com
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
From: [email protected]
To: [email protected]
Subject: [PATCH 0/3] Cover Letter describing the patch series
Date: Sat, 14 Jun 2014 23:35:14 +0200
Message-Id: <[email protected]>
X-Mailer: git-send-email 1.9.1
...

We can check our e-mail inbox and see the four mails we sent: the cover letter and the 3 patches, as shown in the following screenshot:

There's more…

Before sending the patches, the cover letter is filled out and by default has [PATCH 0/3] (if sending 3 patches) in the subject line. A cover letter with only the default template subject and body won't be sent as default. In the scripts that come with this chapter, the git send-email command invokes the --force and --confirm=never options. This was done for script automation to force Git to send the mails even though the cover letter has not been changed from the default. You can try to remove these options, put in the --annotate option, and run the scripts again. You should then be able to edit the cover letter and e-mails that contain the patches before sending.

..................Content has been hidden....................

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