Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 900
Matthew Avatar asked Mar 28 '26 03:03

Matthew


2 Answers

you want

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

head is optional. It is inferred if not specified.

like image 195
Adam Dymitruk Avatar answered Mar 29 '26 19:03

Adam Dymitruk


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
like image 39
Julien Oster Avatar answered Mar 29 '26 17:03

Julien Oster