( Solved - help wanted ) Want to include GitHub tag/version into project.

Cellie
Posts: 45
Joined: Sat Feb 27, 2016 9:47 pm

( Solved - help wanted ) Want to include GitHub tag/version into project.

Postby Cellie » Sun Apr 01, 2018 2:49 pm

EDIT: - The scripts I made and a proper explanation and how-to can be found here.
The scripts can also be found in the last post of this thread.
The scripts work for Linux only.
If anyone could port these scripts to Windows and/or OSX and post them in this thread it would be greatly appreciated.
/EDIT

I want to auto include a version number in my project that tells me exactly which version is running on a device.
Preferably a string value like something like the output from:

Code: Select all

git --no-pager describe --tags --always --dirty > version.txt
.

Above is very basic example and a somewhat nicer way would be to generate a file that looks like:

Code: Select all

const char * sketchVersion "3.1.001-11-g2f50fc1-dirty";
so it can be included as a .h file in the sketch.

I could run a script like the above manually every time I make a commit, but that is too error prone and not the lazy easy way.

Can I edit the build process from the Arduino IDE so that it produces this data every time I compile a project?

Or is it possible to let git somehow produce this data every time I make a commit?

I found this thread on this forum, and others elsewhere on the net, but these all were targeted at makefiles and/or ESP-IDF.

I was also thinking/dreaming along the lines of a bash script file that executes the above script and then starts the Arduino IDE and auto compiles/uploads my sketch.

I made this very simple poc. Put it in the sketch folder and execute it.

Code: Select all

git --no-pager describe --tags --always --dirty > versiontest.txt
~/arduino-1.8.5/arduino --upload test.ino
If anyone is interested I could use some help with formatting the output from git so that it can be included as a header file.

Or am I re-inventing the wheel and have others done this already?
Last edited by Cellie on Wed Apr 04, 2018 10:48 am, edited 4 times in total.

Cellie
Posts: 45
Joined: Sat Feb 27, 2016 9:47 pm

Re: Want to include GitHub tag/version into project.

Postby Cellie » Sun Apr 01, 2018 4:23 pm

After consulting a well known search engine I got this thrown together:

Code: Select all

rm version.h
echo -n 'const char * sketchVersion "' > version.h
git --no-pager describe --tags --always --dirty>>version.h
echo  -n '";' >> version.h
which is basically what I want, but produces this output:

Code: Select all

const char * sketchVersion "v1.0.0
";
An newline too much, probably produced by git.
How to get rid of that newline?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Want to include GitHub tag/version into project.

Postby kolban » Sun Apr 01, 2018 4:57 pm

Maybe the following:

Code: Select all

rm version.h
echo -n 'const char * sketchVersion "' > version.h
git --no-pager describe --tags --always --dirty | tr --delete '\n' >>version.h
echo  -n '";' >> version.h
Recipe from here:

https://stackoverflow.com/questions/125 ... -using-sed
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Cellie
Posts: 45
Joined: Sat Feb 27, 2016 9:47 pm

Re: Want to include GitHub tag/version into project.

Postby Cellie » Sun Apr 01, 2018 6:24 pm

Thanks Neil!

EDIT: These scripts work for Linux only.
If anyone could port these scripts to Windows and/or OSX and post them in this thread it would be greatly appreciated.
/EDIT

I now got it working like I want it.
The script has become 2 scripts.
One for flashing, let's call that one flash.sh:

Code: Select all

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
Note that the 'custom_DebugLevel' has to be set from the script. It is just esp32_xxxx where xxxx is the usual none, info, error, warning and debug.

And a script for verifying called verify.sh:

Code: Select all

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

Code: Select all

#include "gitTagVersion.h"
...
Serial.printl( sketchVersion );
...
Which will output something like v1.0.0-3-gab3fb04.

That breaks down to tag v1.0.0, 3 commits after that tag, at commit ab3fb04.

Pretty cool.

Compiling in the Arduino IDE does not work anymore (because the version file is not generated), but with a terminal opened in the sketch folder I can start the scripts pretty easy.
Actually it is a bonus that the sketch can't be compiled from the IDE, this way the version info can never be stale.
I could save the generated file, but I think this is more foolproof.
No Windows or Mac pc to port the scripts to, but I am sure it can't be that hard.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 141 guests