Simvik

Parse a git describe version

Split a git describe string into the release, the commits since it, the commit hash and the dirty flag. A medium embedded C problem (Git and releases for…

medium · Git and releases for firmware · versioning · parsing · strings · robustness

Builds embed the output of git describe --tags --dirty, and a factory tool reads it back from each device. Write parse_describe(s, d), which fills *d and returns true for a valid string, or false for anything else. A valid string is:

vMAJOR.MINOR.PATCH                       v1.4.2
vMAJOR.MINOR.PATCH-AHEAD-gHASH           v1.4.2-17-gabc1234
either of those followed by -dirty       v1.4.2-dirty, v1.4.2-17-gabc1234-dirty
  • MAJOR, MINOR, PATCH and AHEAD are decimal numbers from 0 to 65535, with at least one digit.
  • HASH is 4 to 12 characters, each 0-9 or a-f (git prints lowercase).
  • Nothing may follow, and nothing may be missing: v1.4, v1.4.2-17 and 1.4.2 are all invalid.

Set fields that are not present to 0, "" and false.

Starter code

bool parse_describe(const char *s, describe_t *d) {
  memset(d, 0, sizeof *d);
  if (*s == 'v') s++;
  while (*s >= '0' && *s <= '9') d->major = d->major * 10 + (*s++ - '0');
  return true;
}

Example tests

A release and a development build

show("v1.4.2");
show("v1.4.2-17-gabc1234-dirty");

Expected output:

v1.4.2: 1.4.2 ahead=0 hash=-
v1.4.2-17-gabc1234-dirty: 1.4.2 ahead=17 hash=abc1234 dirty

Missing parts

show("v1.4");
show("1.4.2");
show("v1.4.2-17");

Expected output:

v1.4: invalid
1.4.2: invalid
v1.4.2-17: invalid

Learn it first

More Git and releases for firmware problems