views:

119

answers:

2

I'm currently using a collection of three commands to get the current tag, branch and the date and SHA1 of the most recent commit.

git describe --always --tag
git log -1 --format="%H%n%aD"
git rev-parse --abbrev-ref HEAD

Which will output something like:

1.2.3-5-gdeadbeef
deadbeef3b8d90071c24f51ac8f26ce97a72727b
Wed, 19 May 2010 09:12:34 +0200
master

To be honest, I'm totally fine with this. But I'm using these commands from Maven and anyone who'd used Maven before, knows how much things like external commands bloat the POM. I just want to slim down my pom.xml and maybe reduce execution time a bit.

+2  A: 

I don't use Maven, so I don't know how you are calling these commands, but adding custom commands to git is falrly trivial.

Create a script called git-tbc that looks like this:

#!/bin/bash

git describe --always --tag
git log -1 --format="%H%n%aD"
git rev-parse --abbrev-ref HEAD

Make sure git-tbc is in your PATH, you can now call "git tbc". Is this what you were looking for?

Peter Farmer
This is definitely a nice way to solve my problem. Didn't think about creating a custom Git command. But I'd be more interested in a single, built-in Git command. I don't want to have to distribute custom Git commands to other developers. :X
Koraktor
Fair comment, you could include the command in git repository and let git do the work for you, but again dev's would need to modify their PATH.
Peter Farmer
Another thing I'd like to avoid. Before including arbitrary commands in bash scripts inside my repositories, I'd rather stay with multiple executions for those commands. Just a matter of preference. Thanks though.
Koraktor
The best way to add something this simple as a git command is to use an alias. If you want to go one step farther, the easiest thing is to set `GIT_EXEC_PATH="/path/to/git/libexec/git-core:/path/to/my-git-commands"` so that you don't actually have to touch the installation.
Jefromi
@JefromiWhy mangle with GIT_EXEC_PATH when just putting your git commands within your PATH will be fine, I always run a ~/bin on my dev boxes for other custom commands.
Peter Farmer
@Peter Farmer: Well, if you want a batch script, throw it in your PATH, sure. If you want to run it as `git tbc`, though, it needs to be in the git exec path. This is where git looks to find its commands. (A few versions ago, git did indeed throw all of its commands in your PATH, but this has been changed. You should not expect to find any git commands in your path anymore.)
Jefromi
Guess I'd better upgrade my installation then.
Peter Farmer
@Jefromi - Are you sure?`$ which git``/usr/local/bin/git``$ git --version``git version 1.7.1.96.gc06ee``$ which git-pfarmer``/home/pfarmer/bin/git-pfarmer``$ echo $GIT_EXEC_PATH``$ git pfarmer``pfarmer`git-pfarmer simply contains `echo pfarmer`
Peter Farmer
Jefromi (and Peter),I have the latest version of Git. And it *still* finds any command on my PATH that is prefixed with "git-<NAME>", and allows me to invoke it as "git <NAME>". No need to mess with GIT_EXEC_PATH.
Jeet
@Peter, Jeet: Ah, I see. It does indeed still work through PATH. However, the entire reason that the git binaries were moved out of its `bin` installation directory was to avoid cluttering your PATH with things that will always be run as `git <command>` instead of `git-command`. By placing them in your PATH, you're just setting yourself to head back in that direction, but it's your choice if you want to do that.
Jefromi
+3  A: 

(1) git log is extremely flexible, with lots and lots of options. You might not be able to reproduce the exact output of the three commands above, but you might come close enough to achieve the effect you need.

For example:

$ git log --pretty=format:'%ad %h %d' --abbrev-commit --date=short -1

produces the date, SHA-1 and symbolic references (including tags) of the latest (HEAD) commit:

2010-05-20 45bd5e7  (HEAD, origin/master) 

After which, presumably, sed and/or awk or maybe Maven-native methods can do the fine-tuning/polishing. Note that a particular tag is associated with a particular commit, so if it was three commits prior to HEAD that was tagged with, for example, "v1.0.0", you are not going to see "v1.0.0" showing up with the above.

(2) A simpler single command to provide a succint description of a commit is:

$ git describe

which writes out the latest applicable tag, the number of commits since the tagged commit, and the SHA1:

v3.3.0-46-g71a77dc

(3) I am not at all familiar with Maven, and have no idea how easy/difficult it is to run external processes, so am unsure whether any of the following help in any way, but I thought I might mention it just in case ...

For the exact purpose that you describe, i.e. tagging builds, in an autoconf/automake framework, I actually use something like:

BUILDTAG="`git symbolic-ref HEAD 2> /dev/null | cut -b 12-`-`git log --pretty=format:\"%h\" -1`"

which produces something suitable for tacking onto the end of a program path:

master-c5282ff

A more extended description, suitable for including as a comment or a printed identifier:

BUILDDESC="$(git symbolic-ref HEAD 2> /dev/null | cut -b 12-)-$(git log --pretty=format:'%h, %ad' -1)"

produces something like:

master-c5282ff, Fri Mar 12 22:19:51 2010 -0600

I think playing around with git log, possibly in conjunction with text processing tools/methods will get you what you want.

Jeet
Before starting to use something like `sed` or `awk` to get almost (!) what I want, I'll most likely stay with my three separate commands. Thanks anyway.
Koraktor