<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Keep It With Mine &#187; Snippets</title>
	<atom:link href="http://bruno.defraine.net/techtips/category/snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://bruno.defraine.net/techtips</link>
	<description>Collection of assorted technology tips</description>
	<lastBuildDate>Wed, 03 May 2017 09:10:09 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.9.19</generator>
	<item>
		<title>Abridged git repository history</title>
		<link>http://bruno.defraine.net/techtips/abridged-git-repository-history/</link>
		<comments>http://bruno.defraine.net/techtips/abridged-git-repository-history/#comments</comments>
		<pubDate>Fri, 15 Mar 2013 11:56:27 +0000</pubDate>
		<dc:creator><![CDATA[bruno]]></dc:creator>
				<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://bruno.defraine.net/techtips/?p=106</guid>
		<description><![CDATA[It is sometimes useful to have a repository for a project that shows only the main points in history. It is possible to start from git repository with full history and create such an abridged history in a separate branch. You can do this efficiently in git by reusing the original tree objects. This post [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>It is sometimes useful to have a repository for a project that shows only the main points in history. It is possible to start from git repository with full history and create such an abridged history in a separate branch. You can do this efficiently in git by reusing the original tree objects. This post will do a walkthrough for the Mediawiki repository.<br />
<span id="more-106"></span></p>
<p>We start with a full repository:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git clone</span> git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>wikimedia<span style="color: #000000; font-weight: bold;">/</span>mediawiki-core.git
<span style="color: #7a0874; font-weight: bold;">cd</span> mediawiki-core
<span style="color: #c20cb9; font-weight: bold;">git config</span> user.name <span style="color: #ff0000;">&quot;Bruno De Fraine&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">git config</span> user.email <span style="color: #ff0000;">&quot;...&quot;</span></pre></td></tr></table></div>

<p>This is a bash function for the main import logic:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> import_mw <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #007800;">tag</span>=<span style="color: #007800;">$1</span>
        <span style="color: #007800;">tree</span>=
        <span style="color: #007800;">adate</span>=
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> key val; <span style="color: #000000; font-weight: bold;">do</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$key</span> <span style="color: #000000; font-weight: bold;">in</span>
                        <span style="color: #c20cb9; font-weight: bold;">tree</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">tree</span>=<span style="color: #007800;">$val</span> <span style="color: #000000; font-weight: bold;">;;</span>
                        author<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">adate</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${val#*&gt; }</span>&quot;</span> <span style="color: #000000; font-weight: bold;">;;</span>
                        <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">break</span> <span style="color: #000000; font-weight: bold;">;;</span>
                <span style="color: #000000; font-weight: bold;">esac</span>
        <span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">git cat-file</span> commit <span style="color: #007800;">$tag</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$tree</span>&quot;</span> <span style="color: #660033;">-o</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$adate</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Could not read tag commit info&quot;</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
                <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #007800;">head</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">git symbolic-ref</span> HEAD<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #007800;">parent</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">git rev-parse</span> <span style="color: #660033;">--verify</span> <span style="color: #007800;">$head</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null<span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #007800;">arg</span>=<span style="color: #ff0000;">&quot;-p <span style="color: #007800;">$parent</span>&quot;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #007800;">arg</span>=
        <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #007800;">commit</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">GIT_AUTHOR_NAME</span>=<span style="color: #ff0000;">&quot;Wikimedia Foundation&quot;</span> \
           <span style="color: #007800;">GIT_AUTHOR_EMAIL</span>=<span style="color: #ff0000;">&quot;&lt;wikimedia@github.com&gt;&quot;</span> \
           <span style="color: #007800;">GIT_AUTHOR_DATE</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$adate</span>&quot;</span> \
           <span style="color: #c20cb9; font-weight: bold;">git commit-tree</span> <span style="color: #007800;">$tree</span> <span style="color: #007800;">$arg</span> <span style="color: #000000; font-weight: bold;">&lt;&lt;&lt;</span><span style="color: #ff0000;">&quot;MediaWiki <span style="color: #007800;">$tag</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #c20cb9; font-weight: bold;">git update-ref</span> <span style="color: #007800;">$head</span> <span style="color: #007800;">$commit</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #c20cb9; font-weight: bold;">git tag</span> <span style="color: #007800;">$tag</span> <span style="color: #007800;">$commit</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> ~<span style="color: #000000; font-weight: bold;">/</span>mw-tags
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$commit</span>
        <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Commit failed&quot;</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
                <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>We start a completely independent branch for abridged history and create commit objects for relevant trees in the history:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git symbolic-ref</span> HEAD refs<span style="color: #000000; font-weight: bold;">/</span>heads<span style="color: #000000; font-weight: bold;">/</span>ABR1_17
<span style="color: #666666; font-style: italic;"># Note: head ABR1_17 does not exist yet</span>
import_mw 1.17.0
<span style="color: #000000; font-weight: bold;">for</span> tag <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000;">1.17</span>.<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">2</span>,<span style="color: #000000;">3</span>,<span style="color: #000000;">4</span>,<span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>; <span style="color: #000000; font-weight: bold;">do</span> import_mw <span style="color: #007800;">$tag</span>; <span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>To see the result so far:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git reset</span> <span style="color: #660033;">--hard</span>
<span style="color: #c20cb9; font-weight: bold;">git log</span> <span style="color: #660033;">--format</span>=fuller</pre></td></tr></table></div>

<p>We can fork another branch in our abridged history for a different major version:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Our commit object for 1.17.0 is 35d4f10</span>
<span style="color: #c20cb9; font-weight: bold;">git update-ref</span> refs<span style="color: #000000; font-weight: bold;">/</span>heads<span style="color: #000000; font-weight: bold;">/</span>ABR1_18 35d4f1055b9254b3b3d915ec782d0c16230516e5
<span style="color: #c20cb9; font-weight: bold;">git symbolic-ref</span> HEAD refs<span style="color: #000000; font-weight: bold;">/</span>heads<span style="color: #000000; font-weight: bold;">/</span>ABR1_18
<span style="color: #000000; font-weight: bold;">for</span> tag <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000;">1.18</span>.<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">0</span>,<span style="color: #000000;">1</span>,<span style="color: #000000;">2</span>,<span style="color: #000000;">3</span>,<span style="color: #000000;">4</span>,<span style="color: #000000;">5</span>,<span style="color: #000000;">6</span>,<span style="color: #000000;">7</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>; <span style="color: #000000; font-weight: bold;">do</span> import_mw <span style="color: #007800;">$tag</span>; <span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #c20cb9; font-weight: bold;">git reset</span> <span style="color: #660033;">--hard</span>
<span style="color: #c20cb9; font-weight: bold;">git</span> hist ABR1_17 ABR1_18</pre></td></tr></table></div>

<p>And so on for branches ABR1_19 and ABR1_20.</p>
<p>We can then export the abridged repository history to a separate repository:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git remote</span> add abr ..<span style="color: #000000; font-weight: bold;">/</span>mediawiki-core-abridged 
<span style="color: #c20cb9; font-weight: bold;">git push</span> abr ABR1_17
<span style="color: #c20cb9; font-weight: bold;">git push</span> abr ABR1_18
<span style="color: #c20cb9; font-weight: bold;">git push</span> abr ABR1_19
<span style="color: #c20cb9; font-weight: bold;">git push</span> abr ABR1_20
<span style="color: #7a0874; font-weight: bold;">cd</span> ..<span style="color: #000000; font-weight: bold;">/</span>mediawiki-core-abridged
<span style="color: #7a0874; font-weight: bold;">source</span> ~<span style="color: #000000; font-weight: bold;">/</span>mw-tags
<span style="color: #c20cb9; font-weight: bold;">git gc</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://bruno.defraine.net/techtips/abridged-git-repository-history/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Makefile for LaTeX documents</title>
		<link>http://bruno.defraine.net/techtips/makefile-for-latex/</link>
		<comments>http://bruno.defraine.net/techtips/makefile-for-latex/#comments</comments>
		<pubDate>Thu, 04 Oct 2012 07:53:52 +0000</pubDate>
		<dc:creator><![CDATA[bruno]]></dc:creator>
				<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://bruno.defraine.net/techtips/?p=68</guid>
		<description><![CDATA[This is a Makefile that I&#8217;ve perfected over the years for rendering typical LaTeX articles or presentations (e.g. for scientific papers). It handles almost all LaTeX documents, except for large documents that use the \include feature (this is for another tip). It assumes your document has a bibtex bibliography. The Makefile works out of the [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>This is a <code>Makefile</code> that I&#8217;ve perfected over the years for rendering typical LaTeX articles or presentations (e.g. for scientific papers). It handles almost all LaTeX documents, except for large documents that use the <a href="http://www.giss.nasa.gov/tools/latex/ltx-245.html"><tt>\include</tt> feature</a> (this is for another tip). It assumes your document has a <code>bibtex</code> bibliography.<br />
<span id="more-68"></span></p>
<p>The <code>Makefile</code> works out of the box, you only have to configure the jobname (=name of the main <tt>.tex</tt>-file). Most common prerequisites are automatically detected:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="make" style="font-family:monospace;">LATEXFLAGS?<span style="color: #004400;">=-</span>interaction<span style="color: #004400;">=</span>nonstopmode <span style="color: #004400;">-</span>file<span style="color: #004400;">-</span>line<span style="color: #004400;">-</span><span style="color: #666622; font-weight: bold;">error</span>
PDFLATEX<span style="color: #004400;">=</span>pdflatex
BIBTEX<span style="color: #004400;">=</span>bibtex
&nbsp;
JOB<span style="color: #004400;">=</span>paper
TEXS<span style="color: #004400;">=$</span><span style="color: #004400;">&#40;</span><span style="color: #0000CC; font-weight: bold;">wildcard</span> <span style="color: #004400;">*.</span>tex<span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #0000CC; font-weight: bold;">wildcard</span> <span style="color: #004400;">*.</span>sty<span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #0000CC; font-weight: bold;">wildcard</span> <span style="color: #004400;">*.</span>cls<span style="color: #004400;">&#41;</span>
PICS<span style="color: #004400;">=$</span><span style="color: #004400;">&#40;</span><span style="color: #0000CC; font-weight: bold;">wildcard</span> <span style="color: #004400;">*.</span>png<span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #0000CC; font-weight: bold;">filter-out</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>pdf<span style="color: #004400;">,$</span><span style="color: #004400;">&#40;</span><span style="color: #0000CC; font-weight: bold;">wildcard</span> <span style="color: #004400;">*.</span>pdf<span style="color: #004400;">&#41;</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #0000CC; font-weight: bold;">wildcard</span> <span style="color: #004400;">*.</span>jpg<span style="color: #004400;">&#41;</span>
BIBS<span style="color: #004400;">=$</span><span style="color: #004400;">&#40;</span><span style="color: #0000CC; font-weight: bold;">wildcard</span> <span style="color: #004400;">*.</span>bib<span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #0000CC; font-weight: bold;">wildcard</span> <span style="color: #004400;">*.</span>bst<span style="color: #004400;">&#41;</span>
&nbsp;
<span style="color: #990000;">.DEFAULT</span><span style="color: #004400;">:</span> all
<span style="color: #990000;">.PHONY</span><span style="color: #004400;">:</span> all clean
&nbsp;
all<span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>pdf
&nbsp;
<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>aux<span style="color: #004400;">:</span> <span style="color: #004400;">|</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TEXS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">PICS</span><span style="color: #004400;">&#41;</span>
        <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">PDFLATEX</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LATEXFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span>
&nbsp;
<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>bbl<span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>aux <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">BIBS</span><span style="color: #004400;">&#41;</span>
        <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">BIBTEX</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span>
&nbsp;
<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>pdf<span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TEXS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">PICS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>aux <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>bbl
        <span style="color: #004400;">@</span>cp <span style="color: #004400;">-</span>p <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>aux <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>aux<span style="color: #004400;">.</span>bak
        <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">PDFLATEX</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LATEXFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span>
        <span style="color: #004400;">@</span>if cmp <span style="color: #004400;">-</span>s <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>aux <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>aux<span style="color: #004400;">.</span>bak<span style="color: #004400;">;</span> \
        then touch <span style="color: #004400;">-</span>r <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>aux<span style="color: #004400;">.</span>bak <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>aux<span style="color: #004400;">;</span> \
        <span style="color: #666622; font-weight: bold;">else</span> NEWS<span style="color: #004400;">=</span><span style="color: #CC2200;">&quot;$$NEWS -W $(JOB).aux&quot;</span><span style="color: #004400;">;</span> fi<span style="color: #004400;">;</span> rm <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>aux<span style="color: #004400;">.</span>bak<span style="color: #004400;">;</span> \
        if <span style="color: #004400;">&#91;</span> <span style="color: #004400;">-</span>n <span style="color: #CC2200;">&quot;$$NEWS&quot;</span> <span style="color: #004400;">&#93;</span><span style="color: #004400;">;</span> then <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">MAKE</span><span style="color: #004400;">&#41;</span> <span style="color: #000088; font-weight: bold;">$$</span>NEWS <span style="color: #000088; font-weight: bold;">$@</span><span style="color: #004400;">;</span> fi
&nbsp;
clean<span style="color: #004400;">:</span>
        rm <span style="color: #004400;">-</span>f <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>aux <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>log <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>blg <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>bbl <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>out <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">JOB</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">.</span>pdf</pre></td></tr></table></div>

<p>LaTeX documents have a circular dependency: a <code>latex</code> run produces a <code>.aux</code>-file, which is read as the input for a following run. You have to keep running <code>latex</code> until this process reaches a <a href="http://en.wikipedia.org/wiki/Fixed-point_combinator" title="Fixed-point combinator">fixpoint</a>. This is handled as follows:</p>
<ol>
<li value="0">An <a href="http://www.gnu.org/software/make/manual/make.html#Prerequisite-Types" title="Types of Prerequisites">order-only prerequisite</a> is used to produce a <code>.aux</code>-file if none exists.</li>
<li>A backup copy of the <code>.aux</code>-file is preserved.</li>
<li><code>latex</code> is ran to produce a new <code>.aux</code>-file.</li>
<li>If the <code>.aux</code>-file differs from the backup copy, <code>make</code> is restarted to go back to step #1.</li>
</ol>
<p>The commands take special care to ensure that <code>make</code> considers the <code>.aux</code>-file as new if (and only if) it has changed.</p>
]]></content:encoded>
			<wfw:commentRss>http://bruno.defraine.net/techtips/makefile-for-latex/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Decoding UTF-8 streams</title>
		<link>http://bruno.defraine.net/techtips/decoding-utf-8-streams/</link>
		<comments>http://bruno.defraine.net/techtips/decoding-utf-8-streams/#comments</comments>
		<pubDate>Thu, 06 Sep 2012 15:10:43 +0000</pubDate>
		<dc:creator><![CDATA[bruno]]></dc:creator>
				<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://bruno.defraine.net/techtips/?p=56</guid>
		<description><![CDATA[UTF-8 is one of the most commonly used methods to encode unicode characters into byte values. It has some interesting properties, for example, characters from the ASCII codeset retain their encoding. This is an implementation of UTF-8 decoding in OCaml. The interface of the decoder is: val utf8_decode : char Stream.t -&#62; int Stream.t The [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> is one of the most commonly used methods to encode unicode characters into byte values. It has some interesting properties, for example, characters from the ASCII codeset retain their encoding. This is an implementation of UTF-8 decoding in OCaml.<br />
<span id="more-56"></span></p>
<p>The interface of the decoder is:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">val</span> utf8_decode <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">char</span> <span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>t <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">int</span> <span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>t</pre></td></tr></table></div>

<p>The decoding works lazily on streams, i.e. the resulting stream will decode characters as they are requested. In case of illegal input, the code will raise stream errors.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> byte <span style="color: #a52a2a;">=</span> Single <span style="color: #06c; font-weight: bold;">of</span> <span style="color: #06c; font-weight: bold;">int</span> <span style="color: #a52a2a;">|</span> Cont <span style="color: #06c; font-weight: bold;">of</span> <span style="color: #06c; font-weight: bold;">int</span> <span style="color: #a52a2a;">|</span> Leading <span style="color: #06c; font-weight: bold;">of</span> <span style="color: #06c; font-weight: bold;">int</span> <span style="color: #a52a2a;">*</span> <span style="color: #06c; font-weight: bold;">int</span> <span style="color: #a52a2a;">|</span> Invalid
&nbsp;
<span style="color: #5d478b; font-style: italic;">(** [classify chr] returns the {!byte} corresponding to [chr] *)</span>
<span style="color: #06c; font-weight: bold;">let</span> classify chr <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> c <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">int_of_char</span> chr <span style="color: #06c; font-weight: bold;">in</span>
    <span style="color: #5d478b; font-style: italic;">(* Classify byte according to leftmost 0 bit *)</span>
    <span style="color: #06c; font-weight: bold;">if</span> c <span style="color: #06c; font-weight: bold;">land</span> 0x80 <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">then</span> Single c
    <span style="color: #06c; font-weight: bold;">else</span> <span style="color: #06c; font-weight: bold;">if</span> c <span style="color: #06c; font-weight: bold;">land</span> 0x40 <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">then</span> Cont <span style="color: #a52a2a;">&#40;</span>c <span style="color: #06c; font-weight: bold;">land</span> 0x3f<span style="color: #a52a2a;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">else</span> <span style="color: #06c; font-weight: bold;">if</span> c <span style="color: #06c; font-weight: bold;">land</span> 0x20 <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">then</span> Leading <span style="color: #a52a2a;">&#40;</span><span style="color: #c6c;">1</span>, c <span style="color: #06c; font-weight: bold;">land</span> 0x1f<span style="color: #a52a2a;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">else</span> <span style="color: #06c; font-weight: bold;">if</span> c <span style="color: #06c; font-weight: bold;">land</span> 0x10 <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">then</span> Leading <span style="color: #a52a2a;">&#40;</span><span style="color: #c6c;">2</span>, c <span style="color: #06c; font-weight: bold;">land</span> 0x0f<span style="color: #a52a2a;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">else</span> <span style="color: #06c; font-weight: bold;">if</span> c <span style="color: #06c; font-weight: bold;">land</span> 0x08 <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">then</span> Leading <span style="color: #a52a2a;">&#40;</span><span style="color: #c6c;">3</span>, c <span style="color: #06c; font-weight: bold;">land</span> 0x07<span style="color: #a52a2a;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">else</span> <span style="color: #06c; font-weight: bold;">if</span> c <span style="color: #06c; font-weight: bold;">land</span> 0x04 <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">then</span> Leading <span style="color: #a52a2a;">&#40;</span><span style="color: #c6c;">4</span>, c <span style="color: #06c; font-weight: bold;">land</span> 0x03<span style="color: #a52a2a;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">else</span> <span style="color: #06c; font-weight: bold;">if</span> c <span style="color: #06c; font-weight: bold;">land</span> 0x02 <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">then</span> Leading <span style="color: #a52a2a;">&#40;</span><span style="color: #c6c;">5</span>, c <span style="color: #06c; font-weight: bold;">land</span> 0x01<span style="color: #a52a2a;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">else</span> Invalid
&nbsp;
<span style="color: #5d478b; font-style: italic;">(** [follow strm n c] returns the code point based on [c] plus [n] continuation
    bytes taken from [strm] *)</span>
<span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> follow strm n c <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">if</span> n <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">then</span> c
    <span style="color: #06c; font-weight: bold;">else</span>
        <span style="color: #a52a2a;">&#40;</span><span style="color: #06c; font-weight: bold;">match</span> classify <span style="color: #a52a2a;">&#40;</span><span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>next strm<span style="color: #a52a2a;">&#41;</span> <span style="color: #06c; font-weight: bold;">with</span>
        <span style="color: #a52a2a;">|</span> Cont cc <span style="color: #a52a2a;">-&gt;</span> follow strm <span style="color: #a52a2a;">&#40;</span>n<span style="color: #a52a2a;">-</span><span style="color: #c6c;">1</span><span style="color: #a52a2a;">&#41;</span> <span style="color: #a52a2a;">&#40;</span><span style="color: #a52a2a;">&#40;</span>c <span style="color: #06c; font-weight: bold;">lsl</span> <span style="color: #c6c;">6</span><span style="color: #a52a2a;">&#41;</span> lor <span style="color: #a52a2a;">&#40;</span>cc <span style="color: #06c; font-weight: bold;">land</span> 0x3f<span style="color: #a52a2a;">&#41;</span><span style="color: #a52a2a;">&#41;</span>
        <span style="color: #a52a2a;">|</span> _ <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">raise</span> <span style="color: #a52a2a;">&#40;</span><span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>Error <span style="color: #3cb371;">&quot;Continuation byte expected&quot;</span><span style="color: #a52a2a;">&#41;</span><span style="color: #a52a2a;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">(** [utf8_decode strm] returns a code point stream that will lazily decode
    the byte stream [strm] *)</span>
<span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> utf8_decode strm <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>slazy <span style="color: #a52a2a;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> <span style="color: #a52a2a;">&#40;</span><span style="color: #a52a2a;">&#41;</span> <span style="color: #a52a2a;">-&gt;</span>
        <span style="color: #06c; font-weight: bold;">match</span> <span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>peek strm <span style="color: #06c; font-weight: bold;">with</span>
        <span style="color: #a52a2a;">|</span> Some chr <span style="color: #a52a2a;">-&gt;</span>
            <span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>junk strm<span style="color: #a52a2a;">;</span>
            <span style="color: #a52a2a;">&#40;</span><span style="color: #06c; font-weight: bold;">match</span> classify chr <span style="color: #06c; font-weight: bold;">with</span>
            <span style="color: #a52a2a;">|</span> Single c <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>icons c <span style="color: #a52a2a;">&#40;</span>utf8_decode strm<span style="color: #a52a2a;">&#41;</span>
            <span style="color: #a52a2a;">|</span> Cont _ <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">raise</span> <span style="color: #a52a2a;">&#40;</span><span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>Error <span style="color: #3cb371;">&quot;Unexpected continuation byte&quot;</span><span style="color: #a52a2a;">&#41;</span>
            <span style="color: #a52a2a;">|</span> Leading <span style="color: #a52a2a;">&#40;</span>n, c<span style="color: #a52a2a;">&#41;</span> <span style="color: #a52a2a;">-&gt;</span>
                <span style="color: #06c; font-weight: bold;">let</span> c <span style="color: #a52a2a;">=</span> follow strm n c <span style="color: #06c; font-weight: bold;">in</span>
                <span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>icons c <span style="color: #a52a2a;">&#40;</span>utf8_decode strm<span style="color: #a52a2a;">&#41;</span>
            <span style="color: #a52a2a;">|</span> Invalid <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">raise</span> <span style="color: #a52a2a;">&#40;</span><span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>Error <span style="color: #3cb371;">&quot;Invalid byte&quot;</span><span style="color: #a52a2a;">&#41;</span><span style="color: #a52a2a;">&#41;</span>
        <span style="color: #a52a2a;">|</span> None <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">Stream</span><span style="color: #a52a2a;">.</span>sempty<span style="color: #a52a2a;">&#41;</span></pre></td></tr></table></div>

<p>Posted at the occassion of <a href="https://plus.google.com/u/0/101960720994009339267/posts/Rz1udTvtiMg">20 years of UTF-8</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://bruno.defraine.net/techtips/decoding-utf-8-streams/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
