<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/assets/atom.xsl" type="text/xsl"?><feed
	xmlns="http://www.w3.org/2005/Atom"
	xmlns:thr="http://purl.org/syndication/thread/1.0"
	xml:lang="en-US"
	><title>Sacha Chua - category - git</title>
	<subtitle>Emacs, sketches, and life</subtitle>
	<link rel="self" type="application/atom+xml" href="https://sachachua.com/blog/category/git/feed/atom/index.xml" />
  <link rel="alternate" type="text/html" href="https://sachachua.com/blog/category/git" />
  <id>https://sachachua.com/blog/category/git/feed/atom/index.xml</id>
  <generator uri="https://11ty.dev">11ty</generator>
	<updated>2024-10-23T12:53:44Z</updated>
<entry>
		<title type="html">A git post-commit hook for tagging my subed.el release version</title>
		<link rel="alternate" type="text/html" href="https://sachachua.com/blog/2024/10/a-git-post-commit-hook-for-tagging-my-subed-el-release-version/"/>
		<author><name><![CDATA[Sacha Chua]]></name></author>
		<updated>2024-12-09T17:47:35Z</updated>
    <published>2024-10-23T12:53:44Z</published>
    <category term="git" />
<category term="emacs" />
<category term="subed" />
		<id>https://sachachua.com/blog/2024/10/a-git-post-commit-hook-for-tagging-my-subed-el-release-version/</id>
		<content type="html"><![CDATA[<div class="update" id="org3c4ca53">
<p>
<span class="timestamp-wrapper"><span class="timestamp">[2024-12-09 Mon]</span></span>: It looks like I also need to use <code>P t</code> to push the tags from Magit, or call <code>git push &#45;&#45;tags</code> from the command-line. Since I'm using a postcommit hook, I'm not sure followTags will kick in for that. Hmm&#x2026;
</p>

</div>

<p>
Debian uses <a href="https://github.com/sachac/subed/issues/76">Git repository tags</a> to notice when to update packages. <a href="https://github.com/sachac/subed/issues/77">I kept forgetting to tag subed's versions</a>, so now I made a git post-commit hook which I think will do the trick.
I based it on <a href="https://gist.github.com/ajmirsky/1245103">https://gist.github.com/ajmirsky/1245103</a>, just updated for Python 3 and tweaked to work with how I do versions in subed.el. I've also added it to my <a href="https://github.com/sachac/subed/blob/main/subed/README.org">README.org</a>.
</p>


<div class="org-src-container">
<pre class="src src-python"><span class="org-comment-delimiter">#</span><span class="org-comment">!/usr/bin/python</span>

<span class="org-comment-delimiter"># </span><span class="org-comment">place in .git/hooks/post-commit</span>
<span class="org-comment-delimiter"># </span><span class="org-comment">Based on https://gist.github.com/ajmirsky/1245103</span>

<span class="org-keyword">import</span> subprocess
<span class="org-keyword">import</span> re

<span class="org-builtin">print</span>(<span class="org-string">"checking for version change..."</span>,)

<span class="org-variable-name">output</span> <span class="org-operator">=</span> subprocess.check_output([<span class="org-string">'git'</span>, <span class="org-string">'diff'</span>, <span class="org-string">'HEAD^'</span>, <span class="org-string">'HEAD'</span>, <span class="org-string">'-U0'</span>]).decode(<span class="org-string">"utf-8"</span>)

<span class="org-variable-name">version_info</span> <span class="org-operator">=</span> <span class="org-constant">None</span>
<span class="org-keyword">for</span> d <span class="org-keyword">in</span> output.split(<span class="org-string">"</span><span class="org-constant">\n</span><span class="org-string">"</span>):
<span class="org-highlight-indentation"> </span>   <span class="org-variable-name">rg</span> <span class="org-operator">=</span> re.<span class="org-builtin">compile</span>(r<span class="org-string">'\+(?:;;\s+)?Version:\s+(?P&lt;major&gt;[0-9]+)\.(?P&lt;minor&gt;[0-9]+)\.(?P&lt;rev&gt;[0-9]+)'</span>)
<span class="org-highlight-indentation"> </span>   <span class="org-variable-name">m</span> <span class="org-operator">=</span> rg.search(d)
<span class="org-highlight-indentation"> </span>   <span class="org-keyword">if</span> m:
<span class="org-highlight-indentation"> </span>   <span class="org-highlight-indentation"> </span>   <span class="org-variable-name">version_info</span> <span class="org-operator">=</span> m.groupdict()
<span class="org-highlight-indentation"> </span>   <span class="org-highlight-indentation"> </span>   <span class="org-keyword">break</span>

<span class="org-keyword">if</span> version_info:
<span class="org-highlight-indentation"> </span>   <span class="org-variable-name">tag</span> <span class="org-operator">=</span> <span class="org-string">"v%s.%s.%s"</span> <span class="org-operator">%</span> (version_info[<span class="org-string">'major'</span>], version_info[<span class="org-string">'minor'</span>], version_info[<span class="org-string">'rev'</span>])
<span class="org-highlight-indentation"> </span>   <span class="org-variable-name">existing</span> <span class="org-operator">=</span> subprocess.check_output([<span class="org-string">'git'</span>, <span class="org-string">'tag'</span>]).decode(<span class="org-string">"utf-8"</span>).split(<span class="org-string">"</span><span class="org-constant">\n</span><span class="org-string">"</span>)
<span class="org-highlight-indentation"> </span>   <span class="org-keyword">if</span> tag <span class="org-keyword">in</span> existing:
<span class="org-highlight-indentation"> </span>   <span class="org-highlight-indentation"> </span>   <span class="org-builtin">print</span>(<span class="org-string">"%s is already tagged, not updating"</span> <span class="org-operator">%</span> tag)
<span class="org-highlight-indentation"> </span>   <span class="org-keyword">else</span>:
<span class="org-highlight-indentation"> </span>   <span class="org-highlight-indentation"> </span>   <span class="org-variable-name">result</span> <span class="org-operator">=</span> subprocess.run([<span class="org-string">'git'</span>, <span class="org-string">'tag'</span>, <span class="org-string">'-f'</span>, tag])
<span class="org-highlight-indentation"> </span>   <span class="org-highlight-indentation"> </span>   <span class="org-keyword">if</span> result.returncode:
<span class="org-highlight-indentation"> </span>   <span class="org-highlight-indentation"> </span>   <span class="org-highlight-indentation"> </span>   <span class="org-keyword">raise</span> <span class="org-type">Exception</span>(<span class="org-string">'tagging not successful: %s %s'</span> <span class="org-operator">%</span> (result.stdout, result.returncode))
<span class="org-highlight-indentation"> </span>   <span class="org-highlight-indentation"> </span>   <span class="org-builtin">print</span>(<span class="org-string">"tagged revision: %s"</span> <span class="org-operator">%</span> tag)
<span class="org-keyword">else</span>:
<span class="org-highlight-indentation"> </span>   <span class="org-builtin">print</span>(<span class="org-string">"none found."</span>)
</pre>
</div>

<div><a href="https://sachachua.com/blog/2024/10/a-git-post-commit-hook-for-tagging-my-subed-el-release-version/index.org">View org source for this post</a></div><p>You can <a href="mailto:sacha@sachachua.com?subject=Comment%20on%20https%3A%2F%2Fsachachua.com%2Fblog%2F2024%2F10%2Fa-git-post-commit-hook-for-tagging-my-subed-el-release-version%2F&body=Name%20you%20want%20to%20be%20credited%20by%20(if%20any)%3A%20%0AMessage%3A%20%0ACan%20I%20share%20your%20comment%20so%20other%20people%20can%20learn%20from%20it%3F%20Yes%2FNo%0A">e-mail me at sacha@sachachua.com</a>.</p>]]></content>
		</entry>
</feed>