Add git tag and version number to an Arduino sketch.

The problem:

Version control and tracking is hard.
For sure in the Arduino IDE, which invites to rapid prototyping and has no built-in version tracking or version control mechanism.

A possible solution:

To make it somewhat more practical and less error prone I made some scripts to assist this process.
It works if you already use or start usingĀ git for your version control.

Using these scripts is as easy as adding them to your sketch folder and adding

#include "gitTagVersion.h"
...
Serial.println( sketchVersion );

to your Arduino sketch.
That is all that is needed.

The output in the example above will be something like v1.0.0-3-gab3fb04.
That breaks down to tag v1.0.0, 3 commits since that tag, at commit ab3fb04.

How to install and use?

There are 2 scripts involved, the first script is a replacement for the Arduino Verify button.
Save it as verify.sh:

echo "const char * sketchVersion = \"$(git describe --tags --always --dirty)\";" > gitTagVersion.h
~/arduino-1.8.5/arduino --verify test.ino
rm gitTagVersion.h

And the second script replaces the Upload button.
Save it as upload.sh:

echo "const char * sketchVersion = \"$(git describe --tags --always --dirty)\";" > gitTagVersion.h
~/arduino-1.8.5/arduino --upload test.ino --pref custom_DebugLevel=esp32_none
rm gitTagVersion.h

Some things to note about these scripts:

  1. The scripts REPLACE the buttons.
    You won’t be able to use the buttons anymore, as these buttons don’t generate the necessary version info from git.
    Using them will result in this error:
    fatal error: gitTagVersion.h: No such file or directory
  2. The scripts are hardcoded to use Arduino 1.8.5 and have to be updated if you update your Arduino IDE.
  3. These script only work in Linux with a vanilla Arduino install.
    If you use Windows, or are on a Mac the scripts will not work.
  4. To change the debug output (using the LOGx_ESP macros) on ESP32 builds, change theĀ custom_DebugLevel in the Upload script to esp32_xxxx, where xxxx is none, info, error, debug or verbose.