<h1 id="a-gentle-introduction-to-quilt">A gentle introduction to quilt</h1>
<p>Or, patch management for software.</p>
<h2 id="speaker-intro">Speaker Intro</h2>
<p>Hi, I'm bjb. I'm a programmer.</p>
<h2 id="motivation-and-topic-intro">Motivation and topic intro</h2>
<p>I needed to learn how to use the software tool &quot;quilt&quot;, so you get to listen to my podcast about an introduction to quilt.</p>
<p>People collaborating on a project must edit the same set of source files. After one person commits some changes, then the other people must rebase their own changes on the new version of the shared files before they can push their changes.</p>
<p>A minor fix for some old typo should not be in the same patch as a new feature; a comment correction should also be in its own patch. Essentially, two new features and some bug fixes should not all be smushed together in one patch. Each feature should be in its own patch (or patch series), and each bug fix should be in its own patch. This allows others to be able to review the proposed changes easily, and even lets them pick and choose which patches they want to apply. It becomes a chore to manage all these patches. That's where quilt comes in.</p>
<p>Sadly, I hadn't learned quilt till this weekend ... well one way to ensure I learn it fairly well is to write a HPR episode about it! Here goes.</p>
<p>I have written this episode to be understandable by anyone - you don't have to be a coder. You could use this tool to keep track of any plain-text files - recipes, todo lists, html, hpr show notes, poetry, what-have-you.</p>
<h2 id="introduction">Introduction</h2>
<p>First let's describe what a patch is. No, first let's describe what source code looks like. Source code is a plain text file full of computer instructions. It is a plain text file, as opposed to a word processing file. Plain text files do not have any formatting codes or styles in them (such as which font should be used, or what colour, and so on). They just contain the characters that make up words of the content.</p>
<p>A key feature of these source code files is that a new section of the file starts on a new line. The source code is almost never &quot;reflowed&quot; like prose might be. It is sort of like poetry - the more formal poetry, not prose poetry. There are a lot of really small sections in source code files (called &quot;statements&quot; and &quot;expressions&quot;). Most of these sections fit on one line. This is useful for the tools we're going to discuss because when one line changes it does not affect the following lines, as it might when text is reflowed after a change.</p>
<p>People have been coding with plain text files in various languages for decades. Thus a large set of tooling has grown around this format. One of those tools is called &quot;diff&quot; and another one is called &quot;patch&quot;.</p>
<p>Diff is a way to compare two text files. Typically it would be used to compare the &quot;before&quot; and &quot;after&quot; of a source code file undergoing changes. So you could find out what was done to the source code file by running diff on the before and after versions of that file.</p>
<p>A diff file is a series of excerpts from the original and changed files. There are various kinds of diffs. Some of them show only the changed lines. Some of them show a few lines before and a few lines after in addition to the changed lines themselves. That second kind is called a &quot;context diff&quot; and helps the automated machinery (and humans too) find the correct part of the file to which the change must be applied.</p>
<p>By default there are 3 lines of context before and after the changed lines.</p>
<p>The changed part is represented by including the old AND new line. In order to distinguish which lines are old and which are the replacements, all the lines (context lines, removed lines and added lines) are shifted over to the right by one character. The context lines start with a space in the extra left-most character, the original removed lines have a minus sign in the left-most character and the new added lines have a plus sign.</p>
<p>Thus if any character on a line in the source file has changed, been added or removed, then the whole line will be replaced with a new line in the new file. The diff will show both the removed line and the new one.</p>
<p>The patch utility takes the &quot;diff&quot; output and applies it to the original file to produce the later version of that file. You can apply it in reverse mode to the later version to get the original version. So patch is also a really useful program, and these two tools, diff and patch, are the basis of most of the version control systems out there. It is the existence of these text-based diff and patch tools that makes revision control systems work really well on plain-text files that are naturally structured in a line-by-line format.</p>
<p>A note about terminology: the diff program produces a diff. This diff is also called a patch. The patch program takes the diff (aka patch) and applies it to the original file to produce the changed file.</p>
<p>So if you have a timeline of adding a few features and making a few fixes on a code-base, it can be fully described by the original file plus a set of patches that had been produced with diff. You can get the final source code by taking the original file, applying the patches one by one, and voila, the final version of the file has been recreated.</p>
<p>Now we know enough to give a concise description of quilt:</p>
<p>Quilt lets you work with patches, creating them, applying them, un-applying them, and moving some things from one patch to another with a minimum of effort.</p>
<h2 id="how-to-use-quilt">How to use quilt</h2>
<p>Now a tutorial on how to get started using quilt.</p>
<p>This tutorial will start with a buggy program, create a few bad patches, and fix them up into good patches. I make no claims as to the quality of the final code though. The reason for starting with bad code and patches is to illustrate how to use quilt.</p>
<h3 id="starting-to-use-quilt-on-a-project">Starting to use quilt on a project</h3>
<p>To start using quilt, create a directory called &quot;patches&quot; at the top of your code or just above.</p>
<pre class="sourceCode "><code>$ mkdir patches</code></pre>
<p>If you don't do this, quilt will create it for you. However, first it will look for a directory called &quot;patches&quot; in the current working directory, its parent, and all the way up ... if it finds one, it will use it. If not, it will create one in the current directory.</p>
<p>So, to keep it from finding some unrelated directory with the name &quot;patches&quot;, just create a patches directory yourself in the right place.</p>
<h3 id="quilt-first-patch-including-a-new-file">Quilt first patch, including a new file!</h3>
<p>You must tell quilt before you make any changes to your source code. Then it can store the original versions of the files that will change, so it can produce the diffs that will become that patch once you change the files.</p>
<p>Create a directory called example, and create a file in it like this, called hello.c (don't fix the errors):</p>
<pre class="sourceCode "><code>#include &quot;stdio.h&quot;

int main (int argc, char *argv[], char *env[])
{
    print (&quot;Hello, world!n&quot;)
    return 0;
}</code></pre>
<p>Now create a new patch - that is, give it a name - before you change any code. This will create (or find) a couple of directories, &quot;patches&quot; and &quot;.pc&quot;, and populate them with some files to start.</p>
<pre class="sourceCode "><code>$ quilt new fix-typo</code></pre>
<p>And now you can fix the typo and generate the patch. First start by telling quilt that you want hello.c to be in the patch. Quilt saves a copy of it aside for comparing with the later versions:</p>
<pre class="sourceCode "><code>$ quilt add hello.c</code></pre>
<p>You can get quilt to tell you what files it knows about:</p>
<pre class="sourceCode "><code>$ quilt files</code></pre>
<p>Edit the file - add a semicolon at the end of the print line, and change the double-quotes on the #include line to angle brackets:</p>
<pre class="sourceCode "><code>#include &lt;stdio.h&gt;
print (&quot;Hello, world!n&quot;);</code></pre>
<p>Save the file and exit the editor. Next generate the patch:</p>
<pre class="sourceCode "><code>$ quilt refresh</code></pre>
<p>The oddly named &quot;refresh&quot; command creates the patch itself. It is called &quot;refresh&quot; because it can also be used to update the patch.</p>
<p>Now you can see the current set of patches by giving the command:</p>
<pre class="sourceCode "><code>$ quilt series</code></pre>
<p>The single patch is called fix-typo, and its name in the list is coloured brownish. That is because it is the &quot;current&quot; patch, and it is the one that will be updated if you &quot;quilt refresh&quot; again with more changes.</p>
<p>One thing I did not find in the quilt documentation is how to add a new file. When adding a new file, there is no existing file that you can name in the quilt add command. Of course, the very first patch I wanted to manage with quilt, I had introduced a new file. It turns out that the quilt edit command can be used to add a file to the patch, even if the file does not yet exist:</p>
<pre class="sourceCode "><code>$ quilt edit header.h</code></pre>
<p>Add content to header.h (see below) using the plain-text editor that quilt has started up for you. Save the file.</p>
<pre class="sourceCode "><code>#ifndef HEADER_HH__
#define HEADER_HH__

#define NAME &quot;bjb&quot;

#endif</code></pre>
<p>Regenerate the patch with the new changes:</p>
<pre class="sourceCode "><code>$ quilt refresh</code></pre>
<p>Now you can list the patch series again with quilt series. So far there is one patch. You can see what the patch consists of with the</p>
<pre class="sourceCode "><code>$ quilt diff</code></pre>
<p>command.</p>
<pre class="sourceCode "><code>$ quilt diff
Index: hello/hello.c
===================================================================
--- hello.orig/hello.c
+++ hello/hello.c
@@ -1,8 +1,8 @@
-#include &quot;stdio.h&quot;
+#include &lt;stdio.h&gt;

 int main (int argc, char *argv[], *env[])
 {
-    print (&quot;Hello, world!n&quot;)
+    print (&quot;Hello, world!n&quot;);
     return 0;
 }

Index: hello/header.h
===================================================================
--- /dev/null
+++ hello/header.h
@@ -0,0 +1,7 @@
+#ifndef HEADER_HH__
+#define HEADER_HH__
+
+#define NAME &quot;bjb
+
+#endif
+
$</code></pre>
<h3 id="quilt-second-patch">Quilt second patch</h3>
<p>Now it is time to make a second patch. First we tell quilt we are moving to a new patch:</p>
<pre class="sourceCode "><code>$ quilt new prototype
$ quilt edit header.h</code></pre>
<p>Edit this file again - add a function prototype.</p>
<pre class="sourceCode "><code>int do_output(const char *name);</code></pre>
<p>Create the patch and look at the list of patches:</p>
<pre class="sourceCode "><code>$ quilt refresh
$ quilt series</code></pre>
<p>Now when we give the quilt series command, we see two patches. The first one is green, meaning it has been applied, and the second one is brown, meaning this is the one that quilt refresh will change if you call it.</p>
<p>Again you can see what latest diff looks like by giving the quilt diff command.</p>
<pre class="sourceCode "><code>$ quilt diff</code></pre>
<p>Now let's unapply the latest diff:</p>
<pre class="sourceCode "><code>$ quilt pop
$ quilt series</code></pre>
<p>We see that the list of patches has the same patches in it, but now the second patch is white (meaning unapplied) and the first patch is brown (meaning it is the one that would change if we edited a file and typed quilt refresh.</p>
<pre class="sourceCode "><code>$ quilt files</code></pre>
<p>That first patch has two files in it, hello.c and header.h.</p>
<p>Now unapply the first diff:</p>
<pre class="sourceCode "><code>$ quilt pop
$ quilt series</code></pre>
<p>Both patches are listed, and both are shown as white.</p>
<p>We can see what files quilt knows about before any patches are applied:</p>
<pre class="sourceCode "><code>$ quilt files</code></pre>
<p>No files.</p>
<p>Apply all the patches at once:</p>
<pre class="sourceCode "><code>$ quilt push -a
$ quilt series</code></pre>
<p>And look at what files quilt knows about:</p>
<pre class="sourceCode "><code>$ quilt files</code></pre>
<p>Now quilt reports on only one file, while in the first patch it knew about two files. You must be careful to &quot;add&quot; each file to each patch, or it will not put the changes in those files into the patch. Luckily, quilt edit will put the files in the patch for you, so if you always start your editor with quilt edit fname, then you will have your changed files added to your patches without having to take any other action. But, if you are adding an existing file to the patch, you can add it without having to open your editor with the quilt add command:</p>
<pre class="sourceCode "><code>$ quilt add fname</code></pre>
<p>In order to avoid forgetting to add a file in a patch as I was editing, I just added all the files in the directory each time I created a new patch, whether I edited them or not.</p>
<h3 id="split-a-patch-in-two-parts">Split a patch in two parts</h3>
<p>We are going to split the first patch in two parts. We had fixed a typo and added a new file in one patch. They should be two separate patches.</p>
<p>First make the first patch current:</p>
<pre class="sourceCode "><code>$ quilt pop</code></pre>
<p>Then make a copy of that patch:</p>
<pre class="sourceCode "><code>$ quilt fork</code></pre>
<p>This makes a copy of the first patch called fix-typo-2. But, it removes the first patch fix-typo and puts fix-typo-2 in the series. We need to put the first patch back, and then edit each of the two fix-typo patches so each one contains one part of the original patch.</p>
<pre class="sourceCode "><code># edit patches/series file and put the first patch back
# The file should contain:

fix-typo
fix-typo-2
prototype</code></pre>
<p>Now edit the first patch using a plain-text editor. It is in patches/fix-typo. Remove the part about the new file, header.h. It should now look like:</p>
<pre class="sourceCode "><code>Index: hello/hello.c
===================================================================
--- hello.orig/hello.c
+++ hello/hello.c
@@ -1,4 +1,4 @@
-#include &quot;stdio.h&quot;
+#include &lt;stdio.h&gt;

 int main (int argc, char *argv[], *env[])
 {</code></pre>
<p>Save this file. Now edit the second patch patches/fix-typo-2 using a plain-text editor. Remove the part about the file hello.c. It should now look like:</p>
<pre class="sourceCode "><code>Index: hello/header.h
===================================================================
--- /dev/null
+++ hello/header.h
@@ -0,0 +1,7 @@
+#ifndef HEADER_HH__
+#define HEADER_HH__
+
+#define NAME &quot;bjb
+
+#endif
+</code></pre>
<p>If you give a quilt series command now, you will see that fix-typo-2 is the current patch and quilt thinks fix-typo has been applied.</p>
<p>We have to fix up quilts idea of reality.</p>
<p>Pop the current patch. Things have changed under quilts feet so we have to force this with the -f option:</p>
<pre class="sourceCode "><code>$ quilt pop -f</code></pre>
<p>Now, because quilt thought the original state of fix-typo-2 is the unchanged file, quilt shows the series as being completely un-applied.</p>
<pre class="sourceCode "><code>$ quilt series
patches/fix-typo
patches/fix-typo-2
patches/prototype</code></pre>
<p>Now we can push the patches:</p>
<pre class="sourceCode "><code>$ quilt push -a</code></pre>
<h3 id="rename-a-patch">Rename a patch</h3>
<p>Here we rename a patch from fix-typo-2 to add-header. The quilt rename command acts on the current patch, so make fix-typo-2 current first:</p>
<pre class="sourceCode "><code>$ quilt pop fix-typo-2
$ quilt rename add-header
$ quilt series
$ quilt push -a</code></pre>
<h3 id="reorder-the-patch-series">Reorder the patch series</h3>
<p>We will make a new patch, then move it earlier in the series:</p>
<p>First make the new patch:</p>
<pre class="sourceCode "><code>$ quilt new printf
$ quilt edit hello.c</code></pre>
<p>And change the print statement to:</p>
<pre class="sourceCode "><code>printf(&quot;Hello, world!n&quot;);</code></pre>
<p>Save the patch:</p>
<pre class="sourceCode "><code>$ quilt refresh</code></pre>
<p>Now to demonstrate the reordering.</p>
<p>Unapply all the patches, edit the patches series file patches/series so the patches are in the order you like, and then re-apply the patches. If you are lucky, they will re-apply with no conflicts.</p>
<pre class="sourceCode "><code>$ quilt pop -a
$ vi patches/series
# move &quot;printf&quot; between fix-typo and add-header.
# now all the bug-fixes are at the beginning of the series
$ quilt push -a</code></pre>
<h3 id="merge-two-patches-into-one">Merge two patches into one</h3>
<p>Make another new patch:</p>
<pre class="sourceCode "><code>$ quilt new output-function
$ quilt edit hello.c</code></pre>
<p>Change the c file to this:</p>
<pre class="sourceCode "><code>#include &lt;stdio.h&gt;

int do_output(const char *name)
{
    return printf(&quot;Hello, %s!n&quot;, name);
}

int main (int argc, char *argv[], char *env[])
{
    /* ignoring the return code for do_output */
    do_output(NAME);
    return 0;
}</code></pre>
<pre class="sourceCode "><code>$ quilt refresh</code></pre>
<p>Now, to merge two patches into one:</p>
<pre class="sourceCode "><code>$ quilt pop prototype
$ quilt fold &lt; patches/output-function</code></pre>
<p>We have merged the prototype and output-function patches, because they describe a related change.</p>
<p>Save the patch.</p>
<pre class="sourceCode "><code>$ quilt refresh</code></pre>
<h3 id="throw-away-a-patch">Throw away a patch</h3>
<p>Now we no longer need the last patch, output-function, as it has been included into the prototype patch. But we might want to rename the prototype patch.</p>
<pre class="sourceCode "><code>$ quilt delete output-function
# we have to clean up a bit for quilt or the rename won&#39;t work
$ rm patches/output-function
$ quilt rename output-function</code></pre>
<p>Deleting will not work on a patch that has been applied before the current patch.</p>
<p>You are ready to contribute your patches ... go forth and code.</p>
<h2 id="summary">Summary</h2>
<p>We have seen that quilt can help you manage your contributions to any project that is written in plain-text files. It can generate patch files (usually needed for contributions to open source projects) and can help you manage and update them as the tip of the development branch moves forward with other peoples' contributions.</p>
<p>To use quilt successfully, you need to remember to add files to each patch with quilt add/or quilt edit before editing, and to generate the patch with quilt refresh once all the editing of each patch is done. The rest is easy.</p>
<p>Commands that edit the patches:</p>
<pre class="sourceCode "><code>$ quilt new patch-name
$ quilt add fname
$ quilt edit fname
$ quilt refresh
$ quilt pop [-a]
$ quilt push [-a]
$ quilt rename [-P oldname] newname
$ quilt delete [-P patchname]
$ quilt fold &lt; patch_to_merge</code></pre>
<p>Commands that view the state of the patches:</p>
<pre class="sourceCode "><code>$ quilt series
$ quilt files
$ quilt diff [-P patchname]
$ quilt graph [--all]
$ quilt patches fname
$ quilt annotate fname
$ quilt applied
$ quilt unapplied</code></pre>
<h2 id="hpr-exhortation">HPR exhortation</h2>
<p>You've been listening to Hacker Public Radio. Anyone can make a show -if I can do it, so can you.</p>
