Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are some subtleties to this because of the nature of Git. The way I've done this is by copying what the Git developers themselves do. First, you'll want to use annotated tags which is probably a good idea anyway. To review, you can create a new tag like this:</p> <pre><code>$ git tag -a -m "Version 0.2" v0.2 HEAD </code></pre> <p>Then (as suggested in Otto's post) you can use <code>git describe</code> for a useful "version" string that will include the number of commits since the tag and the leading digits of the sha1 of the the current commit. Here's an example from one of my projects:</p> <pre><code>$ git describe v1.0-3-gee47184 </code></pre> <p>That is, this copy is 3 commits ahead of the "v1.0" tag and the commit sha1 begins with ee47184 (I'm not sure why they include that leading 'g').</p> <p>The Git developers take it one step further and also include an extra bit if the working copy is modified (uncommitted). This requires a few more steps so it's all wrapped up in a script they name <code>VERSION-GEN</code>. When run, it prints the version string to standard output and also creates a <code>VERSION-FILE</code> file (the script is careful to not re-touch that file if the version hasn't changed -- so it's build-tool friendly). Then, you can include that <code>VERSION-FILE</code> file in your source code, help files, etc.</p> <p>Using my example <code>VERSION-GEN</code> script (below), my version string for the above example is:</p> <pre><code>$ VERSION-GEN version: 1.0-3-gee47 </code></pre> <p>If I modify any of the tracked files it looks like this:</p> <pre><code>$ VERSION-GEN version: 1.0-3-gee47-mod </code></pre> <p>Here's my slightly tweaked version of <code>VERSION-GEN</code>. Note that it expects that the tags marking versions are of the form v[0-9]* (e.g., v1.0 or v0.2 or v12.3.4 or v12.2-4feb2009 etc.)</p> <pre class="lang-bsh prettyprint-override"><code>#!/bin/sh # Tag revisions like this: # $ git tag -a -m "Version 0.2" v0.2 HEAD VF=VERSION-FILE DEFAULT_VERSION=UKNOWN LF=' ' # First see if there is a version file (included in release tarballs), # then try git-describe, then default. if test -d .git -o -f .git &amp;&amp; VN=$(git describe --abbrev=4 HEAD 2&gt;/dev/null) &amp;&amp; case "$VN" in *$LF*) (exit 1) ;; v[0-9]*) git update-index -q --refresh test -z "$(git diff-index --name-only HEAD --)" || VN="$VN-mod" ;; esac then continue #VN=$(echo "$VN" | sed -e 's/-/./g'); else VN="$DEFAULT_VERSION" fi VN=$(expr "$VN" : v*'\(.*\)') # Show the version to the user via stderr echo &gt;&amp;2 "version: $VN" # Parse the existing VERSION-FILE if test -r $VF then VC=$(sed -e 's/^version: //' &lt;$VF) else VC=unset fi # If version has changed, update VERSION-FILE test "$VN" = "$VC" || { echo "version: $VN" &gt;$VF echo &gt;&amp;2 "($VF updated)" } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload