kriss
kriss

Reputation: 24187

Programmatically find if current state of git repository is tagged

I'm working on a simple packaging tool that checks that some internal version string (extracted from source code) match a git tag. The goal is to ensure that my program can show it's version number at run time and that this number will be identified by a tag in my repository.

Basically I need to check two things:

As it is to be included in a script I understand I should not use git porcelain commands, henceforth I should not parse git diff output or call git describe.

How can I do that only using git plumbing commands ?

Upvotes: 0

Views: 139

Answers (1)

Nevik Rehnel
Nevik Rehnel

Reputation: 51975

To find out if the working copy is clean (no uncommitted changes), use git status --porcelain, whose output is "remain stable across Git versions and regardless of user configuration." (See the linked man-page for details.)

To see if the currently checked-out revision is the same as the one of the tag you want, you can use git rev-parse: the same hash must be printed by git rev-parse HEAD and git rev-parse $TAGNAME^{}. The argument is the name of the tag, followed by a caret, then an opening and a closing curly brace; this refers to the commit that is tagged, and not the tag itself (which is also a git object with a hash); for more information on this notation, see the manpage for gitrevisions

Upvotes: 2

Related Questions