25

I'm using Github, and realized they have a nice little api for accessing repo information like commits, who did it, etc.

This would be a great way to show previous versions of the project on an external site, but I was wondering if there is a known way to add a Version Number to the master commit?

So the version number would either automatically increase with each master commit or I can manually set it.

I know I can add it in the notes, but I'm not familiar if there is a way to separate it.

Senica Gonzalez
  • 7,996
  • 16
  • 66
  • 108
  • Add it to the commit how? If it's just for display purposes, you could count up the number of commits in the master's ancestry (from the shell, `git rev-list HEAD | wc -l` or similar) and just put the number next to whatever part you're displaying. – jwodder Aug 03 '11 at 01:02
  • Fair enough. I thought about this. But was thinking there may be a time when I would want to bump the version somehow. In regards to how I was thinking....Github has an issue tracker that when I commit I can add text like "Closes #1" that github will seek out and close the correstponding issue. I didn't know if there was some secret way to do "Version 0.01" and have it seek that out and add a version number to the master commit that can later be retreived from the api. – Senica Gonzalez Aug 03 '11 at 01:06

5 Answers5

16

There are two kinds tags to consider, a build number and a version number. A version number can be applied as a tag by a person when the product ships. This tag is historical and identifies significant events (e.g. shipping the product).

The build number is useful for identifying which build you are on relative to some starting point. The combination of git-tag and git-describe provide a nice means of generating a build number that can be embedded into a build. git-describe can locate a specific previous tag with a glob pattern. The results of git describe will be formatted as:

tagname-[0-9]+-g[0-9a-f]+

Where the first pattern is the number of commits from the tag and the second pattern is the hash of the current commit. This can be nicely formatted into a build number. Including the hash (at least the first 7 characters) makes it simple to identify the specific commit associated with the build.

For example, git describe could return release-2.2-42-gd788e0e. This could be formatted to become release-2.2 build 42 (d788e0e).

Bill Door
  • 18,272
  • 3
  • 32
  • 37
12

You can use a tag to set a version number. You can read about the tag command on the git tag man page. At work I setup our build server to automatically increment a build version number which is then applied using a tag. I think this will meet your needs?

Nathan Fox
  • 4,023
  • 1
  • 23
  • 18
  • 10
    You should also consider using a well defined pattern for your version numbers. [Semantic Versioning](http://semver.org/) is a good one. – Tekkub Aug 03 '11 at 20:01
3

I'm using this:

function git_func {
    GITBIN=/usr/bin/git
    if [[ $1 == "commit" ]] && [[ "$#" -ne 1 ]]
    then
        GIT_VERSION=`$GITBIN rev-list HEAD | wc -l`
        let GIT_VERSION+=1
        perl -e "s/(\d+\.\d+\.)\d+/\${1}$GIT_VERSION/;" -pi.save config.json
        rm config.json.save
    fi

    GITCMD="$GITBIN "
    for var in "$@"
    do
        GITCMD="$GITCMD \"$var\""
    done
    eval $GITCMD
}


alias git='git_func'

config.json contains this:

"version": "0.1.44" 
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Sam Toliman
  • 123
  • 1
  • 5
3

As previously said, check the git-tag command and you can combine it with a hook so it automatically updates upon doing a certain thing for example pushing out master.

check out http://git-scm.com/book/en/Customizing-Git-Git-Hooks

Wissam Youssef
  • 800
  • 1
  • 10
  • 20
1

Most people use git-tag for this.

Check out Vagrant...scroll down the tags, you'll see all the versions.

citizen conn
  • 15,300
  • 3
  • 58
  • 80