Search code examples
githashcommitkeyword-expansion

Git keyword-expansion or alternative


Having read some postings here about this topic, I realize that there are quite some cons that speak against using keyword-substitution. Nevertheless, I need a way to solve following problem and hope that somebody might have an idea on how to solve this with git (whether or not with keyword-substitution):

I have a main application for which I use git as VCS. Parts of this main application (a certain set of XML-Files) are being used by a utility for which I also use git (separate repo).

Now, the main app and the utility have different release cycles, and whenever I release a new version of the utility, I copy over the most current set of XML-Files from the main application. Within the utility, this set of XML-Files is non-versioned content in git, so it's not even checked in to the repo.

As of now, whenever I look at different releases of this utility (which is managed by git and has tagged releases) I have no way of knowing which "version" (better: commit hash) the set of contained XML-Files is referring to.

So I was thinking: if I can have the latest commit SHA within those XML-Files (as a comment), I'd always know to which set of XML-Files a certain version of my utility refers to. Is there anything wrong in this approach or is this the way to go? If yes, how do I get the latest commit-SHA into my XML-Files? (I read that $Id:$ won't take the commit-SHA, but some "blob"-SHA?)


Solution

  • Ok guys, based on your input, this is what i did:

    1. Insert placeholders in my XML-Files: <!-- @GIT_VERSION@ -->
    2. Have ant pick up git version information, note that this is Windows:
    3. Have ant insert this info into my XML-Files while copying them over.

    Code:

    <target name="getGitDetails">
        <exec executable="cmd" outputproperty="git.revision">
             <arg value="/c" />
             <arg value="git.cmd --git-dir=<path-to-repo> describe --long --dirty --always" />
        </exec>
         <exec executable="cmd" outputproperty="git.currentBranchRef">
             <arg value="/c" />
             <arg value="git.cmd --git-dir=<path-to-repo> describe --all" />
         </exec>
     </target>
    
     <target name="build" depends="getGitDetails">
         <copy todir="<dest-dir>">  
             <filterset>
                 <filter token="GIT_VERSION" value="${git.currentBranchRef} ${git.revision}" />
             </filterset>
         </copy>
     </target>
    

    Thanks for your help!

    See also:

    How to lookup the latest git commit hash from an ant build script

    Deriving application build version from `git describe` - how to get a relatively straightforward string?