312 lines
16 KiB
HTML
312 lines
16 KiB
HTML
<!DOCTYPE html>
|
||
<html class="no-js" lang="en-GB">
|
||
<head>
|
||
<title>git format-patch is your Friend | Oliver Davies</title>
|
||
|
||
<meta charset="UTF-8">
|
||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
|
||
|
||
<meta property="og:url" content="https://opdavies.github.io/oliverdavies.uk/blog/2014/05/21/git-format-patch">
|
||
<meta property="og:title" content="git format-patch is your Friend"/>
|
||
|
||
|
||
|
||
<meta property="og:image" content="https://opdavies.github.io/oliverdavies.uk/assets/images/me-precedent.jpg"/>
|
||
<meta property="og:image:height" content="327"/>
|
||
<meta property="og:image:type" content="image/jpg">
|
||
<meta property="og:image:width" content="327"/>
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="https://opdavies.github.io/oliverdavies.uk/assets/css/main.css">
|
||
<link rel="stylesheet" href="https://opdavies.github.io/oliverdavies.uk/assets/css/blog-post.css">
|
||
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=57" sizes="57x57">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=114" sizes="114x114">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=72" sizes="72x72">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=144" sizes="144x144">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=60" sizes="60x60">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=120" sizes="120x120">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=76" sizes="76x76">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=152" sizes="152x152">
|
||
|
||
<link rel="icon" href="/assets/images/me-precedent.jpg?s=160" sizes="160x160">
|
||
<link rel="icon" href="/assets/images/me-precedent.jpg?s=96" sizes="96x96">
|
||
<link rel="icon" href="/assets/images/me-precedent.jpg?s=32" sizes="32x32">
|
||
<link rel="icon" href="/assets/images/me-precedent.jpg?s=16" sizes="16x16">
|
||
</head>
|
||
<body class="">
|
||
<nav class="navbar navbar-inverse navbar-fixed-top">
|
||
<div class="container">
|
||
<div class="navbar-header">
|
||
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
|
||
<span class="sr-only">Toggle navigation</span>
|
||
<span class="icon-bar"></span>
|
||
<span class="icon-bar"></span>
|
||
<span class="icon-bar"></span>
|
||
</button>
|
||
<a class="navbar-brand" href="https://opdavies.github.io/oliverdavies.uk/">Oliver Davies</a>
|
||
</div>
|
||
|
||
<div id="navbar" class="collapse navbar-collapse" role="navigation">
|
||
<ul class="nav navbar-nav">
|
||
<li class="">
|
||
<a href="/">About</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
<a href="/experience">Experience</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
<a href="/testimonials">Testimonials</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
<a href="/talks">Talks</a>
|
||
</li>
|
||
|
||
<li class="active">
|
||
<a href="/blog">Blog</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
<a href="/contact">Contact</a>
|
||
</li>
|
||
</ul>
|
||
</div> </div>
|
||
</nav>
|
||
|
||
<div class="container">
|
||
<div class="row">
|
||
<main class="col-md-9">
|
||
<h1>git format-patch is your Friend</h1>
|
||
|
||
<p class="posted">21st May 2014</p>
|
||
|
||
<h2 id="the-problem">The Problem</h2>
|
||
|
||
<p>As an active contributor to the <a href="http://drupal.org">Drupal</a> project, I spend a lot of time working with other peoples’ modules and themes, and occassionally have to fix a bug or add some new functionality.</p>
|
||
|
||
<p>In the Drupal community, we use a patch based workflow where any changes that I make get exported to a file detailing the differences. The patch file (*.patch) is attached to an item in an issue queue on Drupal.org, applied by the maintainer to their local copy of the code and reviewed, and hopefully committed.</p>
|
||
|
||
<p>There is an option that the maintainer can add to the end of their commit message.</p>
|
||
|
||
<p>For example:</p>
|
||
|
||
<pre><code class="language-bash">--author="opdavies <opdavies@381388.no-reply.drupal.org>"
|
||
</code></pre>
|
||
|
||
<p>This differs slightly different for each Drupal user, and the code can be found on their Drupal.org profile page.</p>
|
||
|
||
<p>If this is added to the end of the commit message, the resulting commit will show that it was committed by the maintainer but authored by a different user. This will then display on Drupal.org that you’ve made a commit to that project.</p>
|
||
|
||
<p><img src="/assets/images/blog/git-format-patch.png" alt="A screenshot of a commit that was authored by rli but committed by opdavies" /></p>
|
||
|
||
<p>The problem is that some project maintainers either don’t know about this option or occasionally forget to add it. <a href="http://dreditor.org">Dreditor</a> can suggest a commit message and assign an author, but it is optional and, of course, not all maintainers use Dreditor (although they probably should).</p>
|
||
|
||
<p>The <code>git format-patch</code> command seems to be the answer, and will be my preferred method for generating patch files in the future rather than <code>git diff</code>.</p>
|
||
|
||
<h2 id="what-does-it-do-differently%3F">What does it do Differently?</h2>
|
||
|
||
<p>From the <a href="http://git-scm.com/docs/git-format-patch">manual page</a>:</p>
|
||
|
||
<blockquote>
|
||
<p>Prepare each commit with its patch in one file per commit, formatted to resemble UNIX mailbox format. The output of this command is convenient for e-mail submission or for use with git am.</p>
|
||
</blockquote>
|
||
|
||
<p>Here is a section of a patch that I created for the <a href="http://drupal.org/project/metatag">Metatag module</a> using <code>git format-patch</code>:</p>
|
||
|
||
<pre><code class="language-bash">From 80c8fa14de7f4a83c2e70367aab0aedcadf4f3b0 Mon Sep 17 00:00:00 2001
|
||
From: Oliver Davies &lt;oliver@oliverdavies.co.uk&gt;
|
||
Date: Mon, 12 May 2014 14:53:55 +0100
|
||
Subject: [PATCH] Exclude comment entities when checking if this is the page,
|
||
otherwise comment_fragment.module will break metatag
|
||
|
||
---
|
||
</code></pre>
|
||
|
||
<p>As mentioned above, the patch is structured in an email format. The commit message is used as the subject line, and the date that the commit was made locally is used for the date. What we’re interested in is the “From” value. This contains your name and email address from your <code>~/.gitconfig</code> file and is used to author the patch automatically.</p>
|
||
|
||
<p>Everything below this is the same as a standard patch file, the same as if was generated with <code>git diff</code>.</p>
|
||
|
||
<p>The full patch file can be found at <a href="https://drupal.org/files/issues/metatag-comment-fragment-conflict-2265447-4.patch">https://drupal.org/files/issues/metatag-comment-fragment-conflict-2265447-4.patch</a>.</p>
|
||
|
||
<h2 id="the-process">The Process</h2>
|
||
|
||
<p>How did I create this patch? Here are the steps that I took:</p>
|
||
|
||
<ol>
|
||
<li>Clone the source repository using <code>$ git clone --branch 7.x-1.x http://git.drupal.org/project/metatag.git</code> and move into that directory.</li>
|
||
<li>Create a branch for this patch using <code>$ git checkout -b 2265447-comment-fragment-conflict</code>.</li>
|
||
<li>Add and commit any changes as normal.</li>
|
||
<li>Generate the patch file using <code>$ git format-patch 7.x-1.x --stdout > metatag-comment-fragment-conflict-2265447-4.patch</code>.</li>
|
||
</ol>
|
||
|
||
<p><em>Note:</em> I am defining 7.x-1.x in the last step as the original branch to compare (i.e. the original branch that we forked to make our issue branch). This will change depending on the project that you are patching, and it’s version number. Also, commits should always be made against the development branch and not the stable release.</p>
|
||
|
||
<p>By default, a separate patch file will be created for each commit that we’ve made. This is overridden by the <code>--stdout</code> option which combines all of the patches into a single file. This is the recommended approach when uploading to Drupal.org.</p>
|
||
|
||
<p>The resulting patch file can be uploaded onto a Drupal.org issue queue, reviewed by the Testbot and applied by a module maintainer, and you automatically get the commit attributed. Problem solved.</p>
|
||
|
||
<h2 id="committing-the-patch">Committing the Patch</h2>
|
||
|
||
<p>If you need to commit a patch that was created using <code>git format-patch</code>, the best command to do this with is the <code>git am</code> command.</p>
|
||
|
||
<p>For example, within your repository, run:</p>
|
||
|
||
<pre><code class="language-bash">$ git am /path/to/file
|
||
$ git am ~/Code/metatag-comment-fragment-conflict-2265447-4.patch
|
||
</code></pre>
|
||
|
||
<p>You should end up with some output similar to the following:</p>
|
||
|
||
<pre><code class="language-bash">Applying: #2272799 Added supporters section
|
||
Applying: #2272799 Added navigation tabs
|
||
Applying: #2272799 Fixed indentation
|
||
Applying: #2272799 Replaced URL
|
||
</code></pre>
|
||
|
||
<p>Each line is the commit message associated with that patch.</p>
|
||
|
||
<p>Assuming that there are no errors, you can go ahead and push your updated code into your remote repository.</p>
|
||
|
||
<p class="tags">
|
||
Tags:
|
||
<a href="https://opdavies.github.io/oliverdavies.uk/blog/tags/patches">patches</a>, <a href="https://opdavies.github.io/oliverdavies.uk/blog/tags/drupal">drupal</a>, <a href="https://opdavies.github.io/oliverdavies.uk/blog/tags/drupal-planet">drupal-planet</a>, <a href="https://opdavies.github.io/oliverdavies.uk/blog/tags/git">git</a> </p>
|
||
<div class="post-pager is-flex">
|
||
<div class="is-half">
|
||
<a href="/blog/2014/05/06/thanks">
|
||
« Thanks
|
||
</a>
|
||
</div>
|
||
|
||
<div class="is-half text-right">
|
||
<a href="/blog/2014/07/02/drush-make-drupalbristol">
|
||
drush make drupalbristol »
|
||
</a>
|
||
</div>
|
||
</div>
|
||
<div class="about-author">
|
||
<h2>About the Author</h2>
|
||
|
||
<img src="https://opdavies.github.io/oliverdavies.uk/assets/images/me-precedent.jpg" alt="Picture of Oliver" class="img-circle">
|
||
|
||
<p>Oliver Davies is a Web Developer, System Administrator and Drupal specialist based in the UK. He is a Senior Developer at <a href="https://microserve.io">Microserve</a> and also provides freelance consultancy services for Drupal websites, PHP applications and Linux servers.</p>
|
||
</div>
|
||
</main>
|
||
|
||
<div class="col-md-3">
|
||
<div class="panel badges text-center">
|
||
<a class="badge--da-member" href="https://assoc.drupal.org/membership" title="I’m a Drupal Association member.">
|
||
<img
|
||
src="https://opdavies.github.io/oliverdavies.uk/assets/images/da-individual-member.png"
|
||
alt="Drupal Association Individual Member"
|
||
width="152"
|
||
>
|
||
</a>
|
||
|
||
<a href="http://drupalcores.com/#opdavies">
|
||
<img
|
||
alt="I built Drupal 8 with hand holding a wrench on blue background"
|
||
src="https://opdavies.github.io/oliverdavies.uk/assets/images/drupal-8.jpg"
|
||
/>
|
||
</a>
|
||
|
||
<img
|
||
src="https://opdavies.github.io/oliverdavies.uk/assets/images/badges/acquia-certified-developer-drupal-8.png"
|
||
alt="Acquia Certified Developer - Drupal 8 Exam Badge"
|
||
height="147" width="147"
|
||
/>
|
||
|
||
<a href="http://conference.phpnw.org.uk/phpnw17">
|
||
<img src="https://opdavies.github.io/oliverdavies.uk/assets/images/badges/phpnw17.png" alt="">
|
||
</a>
|
||
</div>
|
||
<div class="availability panel panel-default">
|
||
<div class="panel-heading">Availability</div>
|
||
|
||
<div class="panel-body">
|
||
<p>
|
||
<i class="fa fa-thumbs-o-up text-warning"></i>
|
||
|
||
|
||
Currently have limited part-time capacity
|
||
</p>
|
||
<p>
|
||
<i class="fa fa-thumbs-o-down text-danger"></i>
|
||
|
||
Currently no spare full-time capacity.
|
||
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<div class="latest-posts panel panel-default">
|
||
<div class="latest-posts__heading panel-heading">Latest blog posts</div>
|
||
|
||
<ul class="list-group">
|
||
<li class="post list-group-item">
|
||
<span class="post__title">
|
||
<a href="/blog/2017/06/09/introducing-the-drupal-meetups-twitterbot">
|
||
Introducing the Drupal Meetups Twitterbot
|
||
</a>
|
||
</span> -
|
||
<span class="post__date">9th June, 2017</span>
|
||
</li>
|
||
<li class="post list-group-item">
|
||
<span class="post__title">
|
||
<a href="/blog/2017/05/20/turning-drupal-module-into-feature">
|
||
Turning Your Custom Drupal Module into a Feature
|
||
</a>
|
||
</span> -
|
||
<span class="post__date">20th May, 2017</span>
|
||
</li>
|
||
<li class="post list-group-item">
|
||
<span class="post__title">
|
||
<a href="/blog/2017/05/15/drupalcamp-bristol-early-bird-tickets-sessions-sponsors">
|
||
DrupalCamp Bristol 2017 - Early Bird Tickets, Call for Sessions, Sponsors
|
||
</a>
|
||
</span> -
|
||
<span class="post__date">15th May, 2017</span>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
</div> </div>
|
||
<footer class="container">
|
||
<p class="copyright">
|
||
© 2010-2017 Oliver Davies. Built with <a href="https://sculpin.io">Sculpin</a>.
|
||
</p>
|
||
|
||
<div class="meetups">
|
||
<h2>Things that I organise</h2>
|
||
<ul>
|
||
<li class="meetups--drupal-bristol">
|
||
<a href="http://www.drupalbristol.org.uk" title="Drupal Bristol">
|
||
<img src="https://opdavies.github.io/oliverdavies.uk/assets/images/meetups/drupal-bristol.jpeg" alt="Drupal Bristol">
|
||
</a>
|
||
</li>
|
||
<li class="meetups--drupalcamp-bristol">
|
||
<a href="http://www.drupalcampbristol.co.uk" title="DrupalCamp Bristol">
|
||
<img src="https://opdavies.github.io/oliverdavies.uk/assets/images/meetups/drupalcamp-bristol.png" alt="DrupalCamp Bristol">
|
||
</a>
|
||
</li>
|
||
<li class="meetups--phpsw">
|
||
<a href="http://phpsw.uk" title="PHPSW">
|
||
<img src="https://opdavies.github.io/oliverdavies.uk/assets/images/meetups/phpsw.jpeg" alt="PHPSW">
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</footer>
|
||
|
||
<script src="https://opdavies.github.io/oliverdavies.uk/assets/js/site.js"></script>
|
||
|
||
<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-11967257-1', 'auto'); ga('send', 'pageview');</script>
|
||
|
||
</body>
|
||
</html>
|