Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Git commit hash and/or branch name in C/C++ source

I would like to know how you can get a Git commit hash and/or other information into the contents of a C++ variable in the compiled binary without having it be part of the source that's tracked by Git.

I need to keep track of firmware release information in a compiled executable running on an embedded processor. Means to identify the particular release of the firmware binary such as meaningful filenames, MD5 checksums or even date/time stamps are not available in this closed environment (i.e., there is no filesystem).

One approach is to have the device's console output produce identifying text, such as 'Release 1.2.3', 'commit hash 1bc123...', or similar. The firmware release information is only of interest to maintenance personnel, so a trained operator could examine the console output. To implement this it could potentially involve manual editing of a version string, which is then compiled into the code and output to the console at program startup.

This is acceptable for major releases where a signoff workflow is used to double-check that the version information is correct. However, this being a manual process it is inherently unreliable. For example, what if the developer forgets to update the version information? - There is now a disconnect between the compiled code and its reported version string.

A workflow where the code is freshly compiled and downloaded each time the user wants to test the hardware is not practical in the situation in question, ie., it is quite onerous to update the firmware.

An automatic way of identifying the version of the code is thus required. In the situation in question, Git is used, and the developers regularly commit their work to feature branches. Displaying the Git commit hash, and perhaps also whether or not there are unstaged changes, would be a way of identifying the status of the source code used to compile the firmware.

The requirement is that I would like my application to have information available to it so that it is able to display: "Git commit:[01abcdef...etc], branch: experimentalStuffDoNotRelease"

Thus, I would like to automatically include Git information, such as commit hash and branch, in the compiled C and/or C++ code.

The development environment has developers using both Windows and Linux, and uses Eclipse CDT with a relatively unsophisticated workflow of: check out; compile; download to the hardware.

like image 740
JoeAC Avatar asked May 18 '17 04:05

JoeAC


People also ask

How get all commit ID of git branch?

To get a list of all commit ids of a branch, you can use git rev-list branchname . Of course, this will also include commits that were merged ino from other branches. If you always merge in the same direction, you might say that only the first parent of each commit is important and relevant to you.

What is commit hash in git?

Commit hashesThe long string following the word commit is called the commit hash. It's unique identifier generated by Git. Every commit has one, and I'll show you what they're used for shortly. Note: The “commit hash” is sometimes called a Git commit “reference” or “SHA”.

How git commit hash is generated?

Git relies on the collision-resistance of SHA-1. (In practice, if you encounter such a collision, which I believe has never occurred outside of intentional attempts to create collisions), you can just add a space to the end of the commit message, which will generate a new hash.

How many characters is a commit hash?

A commit in git always has a hash that contains 40 characters. But to make the id:s easier to handle it also supports using a short version of the id. The short commit id can actually be any number of characters as long as it's unique for a commit within the same repo.


2 Answers

I use a makefile, like so:

GIT_HASH=`git rev-parse HEAD`
COMPILE_TIME=`date -u +'%Y-%m-%d %H:%M:%S UTC'`
GIT_BRANCH=`git branch | grep "^\*" | sed 's/^..//'`
export VERSION_FLAGS=-DGIT_HASH="\"$(GIT_HASH)\"" -DCOMPILE_TIME="\"$(COMPILE_TIME)\"" -DGIT_BRANCH="\"$(GIT_BRANCH)\""

all:
    g++ main.cpp $(VERSION_FLAGS)

When the makefile is run, the git hash and the time of compilation are both loaded into macros accessible within the source, like so:

#include <iostream>

int main(){
  std::cerr<<"hash="<<GIT_HASH<<", time="<<COMPILE_TIME<<", branch="<<GIT_BRANCH<<std::endl; 
}

Which gives output like:

hash=35f531bf1c959626e1b95f2d3e1a7d1e4c58e5ec, time=2017-05-18 04:17:25 UTC, branch=master
like image 131
Richard Avatar answered Oct 06 '22 01:10

Richard


In Eclipse CDT, use a pre-build step to generate an include file containing the relevant information, and a conditional inclusion to check that the file was created:

  1. Right-click the project

  2. Select Properties

  3. Expand the C/C++ Build

  4. Select Settings In the Build Steps tab

  5. Enter the following in the Command text box:

      git log --pretty=format:'#define GIT_INFO_PRESENT%n static const char* GIT_INFO = "Version Information=[%H,%d]\r\n";' -n 1 > ../src/gitcommit.h
    

    This will, upon build, create a file gitcommit.h that will be included in the source code. To customise it, adjust the string to your needs. (See https://git-scm.com/docs/pretty-formats)

As an example, I produce a debug output at the beginning of the main routine to inform the user of the commit and branch (not strictly needed knowing the commit, but certainly helpful):

  1. Put this in the file, probably at the top

     #if __has_include("gitcommit.h")
     #include "gitcommit.h"
     #else
     static const char* GIT_INFO = "Git version information not present.\r\n";
     #endif
    
  2. To display the information somewhere in your code, do similar to this:

     printf(GIT_INFO);
    

Note that I haven't, in this case, made the pre-build step a shell script or Windows/DOS .bat file, as I work often in Linux or Windows.

Note that this isn't tested in Windows.

In both cases, 'git' must be executable from the standard command line.

There is a dependency on provision of __has_include. This was intended to provide simplicity so that a default include file need not be provided.

Note that the gitcommit.h file's path should be discoverable by the compiler.

like image 21
JoeAC Avatar answered Oct 06 '22 00:10

JoeAC