3

We are using Git with a central server, and our code needs to include a version number in a file. The way this is currently done is like this:

  1. A new developer does a "git clone"
  2. In his local copy, he edits .git/hooks/pre-commit to call version.sh

version.sh (which is included in the project root) takes a version number from "git describe" and stores it in a file.

While this works, I would like to make sure that the version number is updated even if a developer forgot to edit his pre-commit hook.

Since the server has no working copy, simply calling the (pre|post)-receive hooks there does not work, so I am wondering if there is a way to do this.

knipknap
  • 5,934
  • 7
  • 39
  • 43
  • If you could enlighten us as to why you need the version number in that file, we might be able to help you solve the actual problem as well (I'm thinking, you need it as a part of the binary? Why not have make pull it out of the git-store and set it as a macro?) – falstro Dec 05 '09 at 16:41
  • It should be in the binary - so yeah, if we had a build procedure, we could do the numbering there. However, I wouldn't be too happy to release something that differs from our repositories. (Currently, we just create an archive of the working tree - no build procedure there.) – knipknap Dec 05 '09 at 16:56

2 Answers2

4

How about a hooks/update on the central repo that rejects commits without changes to the version file (named VERSION in the example below)?

#! /bin/bash

refname="$1"
oldrev="$2"
newrev="$3"

if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
  echo "Usage: $0 <ref> <oldrev> <newrev>" >&2
  exit 1
fi

if [ "$refname" != "refs/heads/master" ]; then
  exit 0
fi

if diff -bq <(git show $oldrev:VERSION) \
            <(git show $newrev:VERSION) >/dev/null
then
  echo "$0: VERSION unchanged; rejecting update" >&2
  exit 1
fi
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
1

The x264 project automatically generates a version number by counting git commits in the history. It migrated from svn, where versions were actually called r678, for example, but someone cooked up a script to generate numbers from git.

http://git.videolan.org/?p=x264.git;a=blob;f=version.sh;hb=HEAD

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847