<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="rss-style.xsl"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Corions Musings</title>
        <link>https://corion.net/blog/tag/git/</link>
        <atom:link href="https://corion.net/blog/tag/git.rss" rel="self" type="application/rss+xml" />
        <description>Blog feed of Corions Musings</description>
        <generator>Statocles 0.098</generator>
        <item>
            <title>Tooling: git absorb</title>
            <link>https://corion.net/blog/2025/12/28/tooling-git-absorb/</link>
            <guid>https://corion.net/blog/2025/12/28/tooling-git-absorb/</guid>
            <description><![CDATA[
                <p>My development workflow looks something like this</p>

<ul>
<li>Implement feature 1</li>
<li>Implement another feature 2</li>
<li>Bug fixes for something in feature 2</li>
<li>Bug fixes for feature 1</li>
<li>More bug fixes for something in feature 2</li>
</ul>

<p>This results in a git history like the above, which I then interactively rebase into</p>

<p><code>
add Implement feature 1
squash Bug fixes for feature 1
add Implement another feature 2
squash Bug fixes for something in feature 2
squash More bug fixes for something in feature 2
</code></p>

<p>The <code>git absorb</code> command automates part of the rebase by looking at the currently staged hunks and finding the commit that most recently changed lines in that hunk, and squashing that hunk in that commit:</p>

<p><code>
git add app.pl -p # add the parts for feature 1 and feature 2 that don&#39;t overlap
git absorb
</code></p>


                    <p>Tags:
                        <a href="https://corion.net/blog/tag/programming/">programming</a>
                        <a href="https://corion.net/blog/tag/git/">git</a>
                    </p>

            ]]></description>
            <pubDate>
                Sun, 28 Dec 2025 00:00:00 +0000
            </pubDate>
        </item>
        <item>
            <title>Simple Deployments using Git as Transport</title>
            <link>https://corion.net/blog/2023/01/25/simple-deployments-using-git-as-transport/</link>
            <guid>https://corion.net/blog/2023/01/25/simple-deployments-using-git-as-transport/</guid>
            <description><![CDATA[
                <p><em>This is largely an elaboration on <a href="https://corion.net/blog/2023/01/25/simple-deployments-using-git-as-transport/../../../../2021/09/30/my-perl-deployment-setup/">my other post on using Git for deployment</a></em></p>

<p>I like to write small toy programs as web apps, like the
<a href="https://corion.net/curl2lwp.psgi">Curl-to-Perl converter</a> or
my <a href="https://corion.net/weather.psgi/index.html">weather forecast app</a>.
My current tool of choice for writing such web apps is <a href="https://mojolicious.org/">Mojolicious</a>,
and the local development is quite nice as it comes with a local web server
built in.</p>

<p>XXX screenshot of the weather app</p>

<h1>The internet</h1>

<p>But obviously a web application is no fun if you can&#39;t put it online and use
it from wherever, or show it around. While I have a server on the internet
and it has a webserver, updating my software while it is in development is
inconvenient. When it is inconvenient, I don&#39;t do it often, so I want to remove
that inconvenience as far as possible.</p>

<h1>Approaches</h1>

<h2>Copying files to the target machine</h2>

<p>It&#39;s easy to copy files using <code>scp</code> or <code>rsync</code>. My uplink is pretty fast
nowadays so I can conveniently copy 10MB within a second.</p>

<p>I program in Perl, and I would need to copy the needed Perl modules
as well. This fails when I need to use Perl modules with a binary component
like a C library. So copying files alone will not work.</p>

<h2>Running programs on the target machine</h2>

<p>If I&#39;m only copying the data that can be easily created locally, I need a way
to run programs on the remote machine. This is possible for example through
<code>ssh -c</code>.</p>

<p>But as most of my webapps are writen with Perl as the backend, I need to run at
least <code>cpanm --installdeps</code> to install the modules needed. Often I also
want to regenerate other files like <code>manifest.json</code> and/or compress assets.
Usually, these other jobs are done through a <code>Makefile</code>.</p>

<h2>Using Git as transport and runner</h2>

<p>I use Git as my version control system. In Git, I usually check in almost
everything of interest to a project. Instead of writing a shell script that I
run locally which uploads the files and then kicks off a remote build, I
(ab)use the Git <code>post-receive</code> hook to work as my program runner and the
Git transport mechanism for transferring the data.</p>

<h1>Git as transport</h1>

<p>Git can download and upload changes from other git Repositories. It can
use a variety of transport mechanisms:</p>

<ul>
<li>file copy from/to a local directory</li>
<li>file copy via the <code>git</code> protocol</li>
<li>file copy via the <code>ssh</code> / <code>scp</code> protocol</li>
</ul>

<p>The last protocol is of convenient interest to me, as it means I can simply
have a Git repository on the webserver machine and <code>git push</code> will upload
my local changes to the remote webserver.</p>

<h1>Anatomy of the Git <code>post-receive</code> hook</h1>

<p>Whenever Git receives a complete set of changes in a repository, it will
then kick off the <code>post-receive</code> hook. The <code>post-receive</code> hook is a program
intended to be customized by the user to perform tasks whenever the event
arrives.</p>

<p>In my case, I use that <code>post-receive</code> hook to perform all tasks that I want
to be done on the webserver:</p>

<ul>
<li>check out the latest state of my webapp into a directory</li>
<li>install modules needed by my webapp</li>
<li>perform other tasks as specified by a Makefile</li>
</ul>

<h1>Setup</h1>

<p>Setting up the <code>post-receive</code> hook is fairly simple:</p>

<ol>
<li>Create a remote directory for the repository</li>
</ol>

<p><code>
mkdir my-webapp.git
</code></p>

<ol>
<li>Initialize the directory as Git repository</li>
</ol>

<p><code>
cd my-webapp.git &amp;&amp; git init --bare
</code></p>

<ol>
<li>Add the <code>post-receive</code> hook</li>
</ol>

<p>Don&#39;t forget to make the file executable.</p>

<ol>
<li>Add the machine as a remote on your local repository</li>
</ol>

<p><code>
git remote add demo corion@that.machine.example:my-webapp.git
</code></p>

<h1>Deployment</h1>

<p>Deployment now looks like</p>

<p><code>
git push demo
</code></p>

<h1>The steps of the <code>post-receive</code> hook</h1>

<p>The steps performed by the hook in detail are:</p>

<h2>Check out the latest state of my webapp into a directory</h2>

<p><code>
git &quot;--work-tree=${CHECKOUT_DIR}&quot; &quot;--git-dir=${REPO}&quot; checkout -f
</code></p>

<p>From the Git repository, we checkout the current state into a target directory.</p>

<h2>Install modules needed by my webapp</h2>

<p>I like to install all modules needed by a webapp into a directory local to that
webapp. This means more maintenance, but it also means that changes to one
webapp don&#39;t break other webapps. For additional safety, I also reset the
<code>PERL5LIB</code> environment variable so even if the hook is run manually it won&#39;t
install or use modules outside of the app-specific directory.</p>

<p><code>
PERL5LIB=${BASE}/${DIST}/lib /home/corion/perl/bin/cpanm --installdeps &quot;${CHECKOUT_DIR}&quot;  -l &quot;${CHECKOUT_DIR}/extlib&quot; --notest
</code></p>

<h2>Run post-install steps</h2>

<p>Some assets of the webapp might need to be (re)compressed. <code>make</code> is a
convenient tool to update files based on the date of other files:</p>

<p><code>
cd &quot;${CHECKOUT_DIR}/public&quot; &amp;&amp; make deploy
</code></p>

<h1>The <code>post-receive</code> hook in its full glory</h1>

<pre><code>#!/bin/sh
unset PERL_MB_OPT PERL_MM_OPT PERL_LOCAL_LIB_ROOT PERL5LIB

REPO=$( cd &quot;$GIT_DIR&quot; || exit; pwd)
BASE=$(cd &quot;${REPO}/..&quot; || exit; pwd)
CHECKOUT_DIR=${REPO%.git}
DIST=$(basename &quot;${CHECKOUT_DIR}&quot;)

git &quot;--work-tree=${CHECKOUT_DIR}&quot; &quot;--git-dir=${REPO}&quot; checkout -f
PERL5LIB=${BASE}/${DIST}/lib /home/corion/perl/bin/cpanm --installdeps &quot;${CHECKOUT_DIR}&quot;  -l &quot;${CHECKOUT_DIR}/extlib&quot; --notest

cd &quot;${CHECKOUT_DIR}/public&quot; &amp;&amp; make deploy
</code></pre>

<h1>See Also</h1>

<p><a href="https://metacpan.org/dist/Git-Hooks/view/lib/Git/Hooks/Tutorial.pod">Git::Hooks</a> -
a Perl program for Git hooks</p>

<p><a href="https://git-scm.com/docs/git-init">git-init</a> - create or edit your default Git hooks</p>

<h1>Other approaches</h1>

<h2><a href="https://metacpan.org/pod/Carton">Carton</a></h2>

<p>A bundler for Perl. This requires you to have the same Perl compiler and compiler
flags locally as you have on the remote machine as it compiles all artifacts
locally.</p>


                    <p>Tags:
                        <a href="https://corion.net/blog/tag/perl/">perl</a>
                        <a href="https://corion.net/blog/tag/git/">git</a>
                    </p>

            ]]></description>
            <pubDate>
                Wed, 25 Jan 2023 00:00:00 +0000
            </pubDate>
        </item>
        <item>
            <title>My Perl deployment setup</title>
            <link>https://corion.net/blog/2021/09/30/my-perl-deployment-setup/</link>
            <guid>https://corion.net/blog/2021/09/30/my-perl-deployment-setup/</guid>
            <description><![CDATA[
                <h1>How I make deployments convenient</h1>

<p>For some of my modules, I also run a demo website that shows off the features
and use of each module. Case in point, <a href="https://metacpan.org/pod/HTTP::Request::FromCurl">HTTP::Request::FromCurl</a>
has an <a href="https://corion.net/curl2lwp.psgi">online converter on my website</a>.</p>

<p>I like to use <code>git</code> as my deployment tool, becuase it conveniently abstracts
the packaging and transport. Basically, a deployment becomes</p>

<pre><code>git push demo
</code></pre>

                    <p><a href="https://corion.net/blog/2021/09/30/my-perl-deployment-setup/#section-2">Continue reading...</a></p>

                    <p>Tags:
                        <a href="https://corion.net/blog/tag/perl/">perl</a>
                        <a href="https://corion.net/blog/tag/cpan/">cpan</a>
                        <a href="https://corion.net/blog/tag/git/">git</a>
                    </p>

            ]]></description>
            <pubDate>
                Thu, 30 Sep 2021 00:00:00 +0000
            </pubDate>
        </item>
    </channel>
</rss>

