Git keyword expansion tip

A place to discuss software and code that isn't automotive related. Free and open source preferred but not compulsory.
Post Reply
User avatar
QFP80 - Contributor
Posts: 45
Joined: Tue Jul 06, 2010 1:03 pm
Location: Espoo, Finland

Git keyword expansion tip

Post by MrOnion »

Bumped into a simple problem when designing a PCB with eagle. Wanted to have a Git commit id in the boards silkscreen that would update automatically. As Eagle files are in XML it should not be a big problem.

You can use the clean/smudge filter in git attributes to do this:

You can specify a custom keyword that you "clean" when you add or commit files. When you take the file out of the repo it will "smudge" the keyword with the info you want. I'll explain my approach here.

Here is the part of the XML file holding the Git hash. This is what it looks like after smudge.

Code: Select all

<text x="95.885" y="10.16" size="1.4224" layer="21" rot="R180">GIT 73d710e</text>
And this is what it looks like when you create the file and in repo after clean:

Code: Select all

<text x="95.885" y="10.16" size="1.4224" layer="21" rot="R180">%GIT_COMMIT_ID%</text>
You can use git attributes file to add a custom command like this:

Code: Select all

my_ultimate_board.brd filter=hasher 

I call it the "hasher". This command is then specified either in the global git config or under the project config:

Code: Select all

[filter "hasher"]
        smudge = sed \"s/GIT %GIT_COMMIT_ID%/GIT `git rev-parse --verify HEAD --short`/\"
        clean = sed \"s/GIT [0-9a-z]\\{7\\}/GIT %GIT_COMMIT_ID%/\"

I just used simple sed regexp replace commands to find and replace the hash/keyword. You can use whatever command you like/have or script. You can also alter the git command as you please. Maybe use git describe etc.

One problem with this approach is that after you commit the file the new commit id won't be available until after you checkout the file again. So remember that.

To get the id into eagle I save the file, commit, delete the file and checkout the file again. Then I can make the gerbers or whatever I publish with the current commit id.

Thats it. Hope this comes in handy and if you find a better approach or enhance this, let me/us know.
User avatar
Posts: 15407
Joined: Tue Jan 15, 2008 2:31 pm
Location: Home sweet home!

Re: Git keyword expansion tip

Post by Fred »

That problem with the having to check it out again, is that normal? Or something to do with your setup? I hadn't heard of that before. If it is normal, you could likely use a hook to do a checkout of the file post commit. Just FYI.

Thanks for posting it up! :-)

Fred. - where Open Source means Open Source, and Free means Freedom - the open source engine management system
FreeEMS dev diary and its comments thread and my turbo truck!
n00bs, do NOT PM or email tech questions! Use the forum!
The ever growing list of FreeEMS success stories!
Post Reply