2

On CI server we git clone --depth 1 <repo> and I would like to create a version string for the artifact that is being built. I want my version string to look like this:

0.1.0-15-0bef345-master

Where:

  • 0.1.0 is the version obtained from the latest tag, this is easy, using git ls-remote.
  • 15 is the number of commits after the 0.1.0 tag
  • 0bef345 is the short hash of the git commit
  • master is the branch

Now the question is, how can I get the 15 (amount of commits between a tag and the HEAD).

Normally this is easy git rev-list --count v0.1.0 HEAD, when you have the full history.

How can I do this without having to clone the entire history?

mirosval
  • 6,671
  • 3
  • 32
  • 46
  • http://stackoverflow.com/a/6643792 – Tatsuyuki Ishi Mar 31 '17 at 15:19
  • @TatsuyukiIshi thanks, but as I understand this you have to have the full history to perform this – mirosval Mar 31 '17 at 15:42
  • shallow clone is identical to snapshot, so the answer is no. – Tatsuyuki Ishi Mar 31 '17 at 15:43
  • Why not have the full history for the master branch. Initial clone will be one time cost. Next time it should be fast download and fast forwards. Wondering how tags work with shallow clone. – Vishwanath Mar 31 '17 at 15:46
  • You can't do that: as you noted yourself, you have to *have* the commits, in order to count them. There is no in-Git way to ask a remote repository to count some subset of its own commits. (You could abuse the fetch protocol but this would be tricky and there's no clear way to deal with merge commits without actually receiving the objects anyway.) If you are dead set against cloning the commits, you could write yourself an agent that takes requests: "on the remote, do the counting, and deliver me the string I want." Since the remote has the commits, you're good to go there. – torek Mar 31 '17 at 21:31

0 Answers0