Skip to content

Instantly share code, notes, and snippets.

@tilgovi
Last active December 22, 2015 16:58
Show Gist options
  • Select an option

  • Save tilgovi/6502556 to your computer and use it in GitHub Desktop.

Select an option

Save tilgovi/6502556 to your computer and use it in GitHub Desktop.
Getting back to a commit after stripping the git hash from `git describe --always` output.

Background

For Hypothes.is, I've been using git describes --always option for generating version numbers. However, Chrome apps can only have digits (and '.') in their version identifiers. For that reason, instead of ending up with 0.0.6-1371-g93b5d9d (which is what versioneer generates for me), I've been publishing extensions that strip off the git hash.

Problem

When something goes wrong in production, the clear question is "what git hash was this extension built from?"

Solution

Here's my bash script to recover a git hash from a version like "0.0.6-1371" (in our case this means 1371 commits since v0.0.6).

It's easy: use git rev-list to count the commits since the tag. Then, subtract from that the trailing count in our version number to get the difference. Finally, skip back that amount from HEAD (important: in topological order).

#!/bin/bash
IFS='.'
read -a V <<< "$1"
M=${V[3]}
T=$(echo "${V[*]:0:3}")
N=$(git rev-list v"$T"..HEAD | wc -l)
SKIP=$((N-M))
git rev-list --skip=${SKIP} --topo-order HEAD | head -n1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment