Matthew
Matthew

Reputation: 5222

Git and incremental commit dates/number/something

I am trying to set up a build server using NAnt tasks. I have a few Git repos that I want to build, but I am having a problem with versioning of the results.

How do version a library (dll) so that each build will use a number for each version? I know that Git has no Revision Number like SVN, but rather a Hash of some sort. I thought of using the Commit date. I will only be building the master on the central server so all commits will be increasing (the merge date).

If I could get the integer representation of the date, I could use this (YYMMDD).

Now I will be using NAnt to do all the cool stuff :), does Git provide a way to get the date of the latest commit?

Upvotes: 3

Views: 3361

Answers (3)

David M. Syzdek
David M. Syzdek

Reputation: 15788

I know that this is not what you asked for, however if you do want to use version numbers then git-describe is an option.

I use it to provide version information for my libraries and programs. Since I use the version format X.Y.Z, git-describe works perfectly for me. It has the added benefit of also providing me the commit ID of the latest commit as well so I always know exactly which point in the source history the library/program was compiled.

To use git-describe you must used signed tags. When creating a tag, I use the format of vX.Y. git-describe will then append the patch and commit information using the format vX.Y-Z-gC where X is the major version, Y is the minor version, Z is the patch level, and C is the git commit ID.

For example, I create a commit:

$ git tag -s v0.2 -m "Creating release 0.2"
$ git tag
v0.0
v0.1
v0.2
$ git describe --long --abbrev=7
0.2.0.gbb871fd

I can then derive the version with patch level using the following:

GIT_DESCRIBE=`git describe --long --abbrev=7 |sed -e 's/-/./g' -e 's/^v//g'`
GIT_MAJOR=`echo ${GIT_DESCRIBE}  |cut -d. -f1`
GIT_MINOR=`echo ${GIT_DESCRIBE}  |cut -d. -f2`
GIT_PATCH=`echo ${GIT_DESCRIBE}  |cut -d. -f3`
GIT_COMMIT=`echo ${GIT_DESCRIBE} |cut -d. -f4`

echo "${GIT_DESCRIBE}"
0.2.53.g00e0e11

echo "${GIT_MAJOR}.${GIT_MINOR}.${GIT_PATCH}.${GIT_COMMIT}"
0.2.53.g00e0e11

In the above output, there have been 53 commits (patched) since the last tag was created and 00e0e11 is the partial commit ID of the latest commit in the revision history.

Once I have the parsed version information I can create a C header file to define the version numbers for use within the source code:

cat << EOF > lib_version.h
#undef MYLIB_VER_MAJOR
#undef MYLIB_VER_MINOR
#undef MYLIB_VER_PATCH
#undef MYLIB_VER_COMMIT
#undef MYLIB_VERSION
#define MYLIB_VER_MAJOR  ${GIT_MAJOR}
#define MYLIB_VER_MINOR  ${GIT_MINOR}
#define MYLIB_VER_PATCH  ${GIT_PATCH}
#define MYLIB_VER_COMMIT "${GIT_COMMIT}"
#define MYLIB_VERSION    "${GIT_DESCRIBE}"
EOF

I've used similar techniques as the one above to generate version information in packages using autoconf (and friends) and Xcode's external build target (it is actually how all of my iOS apps are versioned).

Here is a real world example (with source code) implementing the above: http://bindle.github.com/LdapKit/ Notice that the version information appears in the documentation and it is updated every time I regenerate the docs.

Upvotes: 3

Julien Oster
Julien Oster

Reputation: 2322

This gives you the commit time of the latest commit (i.e. HEAD) as a UNIX timestamp (seconds since epoch, i.e. 00:00:00 UTC on 1 January 1970), which should be pretty much what you want:

% git show -s --format='format:%ct' HEAD
1334298121

You can then use date to convert it into the YYMMDD format you mentioned, and also add other stuff:

% date -r `git show -s --format='format:%ct' HEAD` +"foobar-%C%y%m%d.zip"
foobar-20120413.zip

(I added the century in front with %C because that seems the sane thing to do, but you can omit that of course. The foobar and .zip is just an example on how you would directly generate a filename.)

There are also some other formats you can choose from, see git show --help for details:

% git show -s --format='format:%cd' HEAD
Fri Apr 13 14:22:01 2012 +0800
% git show -s --format='format:%cD' HEAD
Fri, 13 Apr 2012 14:22:01 +0800
% git show -s --format='format:%cr' HEAD
3 months ago
% git show -s --format='format:%ct' HEAD
1334298121
% git show -s --format='format:%ci' HEAD
2012-04-13 14:22:01 +0800

Upvotes: 3

Adam Dymitruk
Adam Dymitruk

Reputation: 129654

you want

git log -1 --pretty=format:%cd HEAD

head is optional. It is inferred if not specified.

Upvotes: 4

Related Questions