<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Git on despatches</title><link>https://icle.es/tags/git/</link><description>Recent content in Git on despatches</description><generator>Hugo</generator><language>en</language><lastBuildDate>Tue, 08 Jul 2025 17:18:26 +0100</lastBuildDate><atom:link href="https://icle.es/tags/git/index.xml" rel="self" type="application/rss+xml"/><item><title>Automatically link to repo at current commit</title><link>https://icle.es/2025/07/08/automatically-link-to-repo-at-current-commit/</link><pubDate>Tue, 08 Jul 2025 15:39:14 +0100</pubDate><guid>https://icle.es/2025/07/08/automatically-link-to-repo-at-current-commit/</guid><description>&lt;p>I like writing blog posts, particularly about code, and I like to link to code
on my repo from my blog post. I do this a lot. Until now, I&amp;rsquo;ve just been copying
and pasting the full link to github. However, I ran into a problem today.&lt;/p>
&lt;p>I moved a file that was referenced in a blog post. I then had to go to that blog
post and update the links - this is fine if I remember the linked blog posts -
but that&amp;rsquo;s not scalable.&lt;/p></description><content:encoded><![CDATA[<p>I like writing blog posts, particularly about code, and I like to link to code
on my repo from my blog post. I do this a lot. Until now, I&rsquo;ve just been copying
and pasting the full link to github. However, I ran into a problem today.</p>
<p>I moved a file that was referenced in a blog post. I then had to go to that blog
post and update the links - this is fine if I remember the linked blog posts -
but that&rsquo;s not scalable.</p>
<p>Also:</p>
<ul>
<li>Finding the link on GitHub, then copying and pasting is annoying</li>
<li>It worries me a little that the version they are linked to could be vastly
different from what I mention on the post.</li>
</ul>
<p>I was already trying to create permalinks using tags, but that is laborious and
error prone.</p>
<p>What if I could get Hugo to:</p>
<ul>
<li>Automatically link to GitHub if the relative link is not within <code>blog/content</code></li>
<li>What if I could get it to link it to the file at the last commit of the post.</li>
</ul>
<p>The last one is something to bear in mind. If I update a blog post, I&rsquo;ll have to
ensure that the links are still relevant.</p>
<p>Alternatively, let&rsquo;s allow an override at the page level where you can provide a
specific commit to link to:</p>
<h2 id="step-1-enable-git-info">Step 1: Enable git info</h2>
<p>To be able to get the commit of the post, we need to enable
<a href="https://gohugo.io/methods/page/gitinfo/">git info</a></p>
<p><a href="https://icle.es/hugo.toml">hugo.toml</a></p>
```toml
enableGitInfo = true
```
<h2 id="step-2-update-rendering-of-link">Step 2: Update rendering of link</h2>
<p><a href="https://icle.es/layouts/_default/_markup/render-link.html">layout/_default/_markup/render-link.html</a></p>
```gotmpl
{{- $linkPath := .Destination -}}                           {{/* e.g. "../scripts/tool.sh" */}}
{{- $currentPath := .Page.File.Path -}}                     {{/* e.g. "posts/foo.md" */}}
{{- $currentDir := path.Dir $currentPath -}}                {{/* e.g. "posts" */}}

{{- $combined := path.Join $currentDir $linkPath -}}        {{/* e.g. "posts/../scripts/tool.sh" */}}
{{- $resolved := path.Clean $combined -}}                   {{/* e.g. "scripts/tool.sh" */}}

{{- $fullRepoPath := path.Join "blog/content" $resolved -}} {{/* e.g. "blog/content/scripts/tool.sh" */}}

{{- $isInContent := strings.HasPrefix $fullRepoPath "blog/content/" -}}

{{- if $isInContent -}}
    <span class="unpublished">{{ $text }}</span>
{{- else -}}
    {{- $commit := or .Page.Params.link_commit .Page.GitInfo.Hash -}}
    <a href="https://github.com/drone-ah/wordsonsand/blob/{{ $commit }}/{{ $fullRepoPath }}" {{ with .Title }}title="{{ . }}"{{ end }}>{{ $text }}</a>
```
<h2 id="bonus-allow-per-post-commit-override">Bonus: Allow per-post commit override</h2>
<p>In the above code:</p>
```gotmpl
{{- $commit := or .Page.Params.link_commit .Page.GitInfo.Hash -}}
```
<p>The commit id is picked up from the page parameter <code>link_commit</code>, or if that
doesn&rsquo;t exist, from the last commit of the page.</p>
<p>You can therefore, set the commit to use for a post with:</p>
```yaml
link_commit: <custom-commit-to-link-to>
```
<h2 id="conclusion">Conclusion</h2>
<p>Necessity might be the mother of invention, but sometimes it takes a fine-tuned
sense of frustration to detect minor needs.</p>
]]></content:encoded></item></channel></rss>