Search code examples
eclipsegiteclipse-cdt

Git: How to include working tree status in git log --pretty=format...?


I'm using the following command in an Eclipse CDT pre-build step, to generate a header file containing my current short Git hash as a string macro:

git log --pretty=format:'#define GIT_HASH_STRING "%h"' -n 1 > ../Inc/gitcommit.h

Works great, but it doesn't indicate the status of the working tree. Like when running git submodule status, if there are working tree changes, I'd like it to spit out something like

a289542-dirty

Is this possible? I checked the man page for git-log formats, but didn't see anything that looked pertinent.

Context: The GIT_HASH_STRING macro is displayed when issuing a version command via the CLI of an embedded device. If I can include a -dirty flag in the string, it can serve as a warning that the device is running an unreleased version of firmware that doesn't align with a specific commit.


Solution

  • The git log command does not inspect the work-tree, so it cannot do this.

    There are many commands that do inspect the work-tree. One simple one is git describe:

    git describe --always --dirty
    

    will print out a string that will end with -dirty if the work-tree or the index is modified with respect to the current commit (i.e., in the same situations where git status would say something is staged for commit or not staged for commit).

    If you want to check submodules as well, you will need more.