2

Warning: I'm not a git wizard, so I may not have the right terminology...

Before I push a release to github, I create a version file that reflects the current commit tag, sort of like this:

git commit -m <insert pithy comment here>
MAJOR=1
MINOR=2
BUILD=`git describe --all --tags`
echo VERSION = [${MAJOR}, ${MINOR}, #{BUILD}] > version.rb
git push origin master

This works, but for the obvious flaw that version.rb is modified after the commit has happened. I can add verion.rb to .gitignore, but is there a way to sneak verion.rb into the config after the commit without creating a new tag? Or is there another approach I'm not thinking of?

fearless_fool
  • 33,645
  • 23
  • 135
  • 217
  • 1
    Use the `pre-commit` hook? – Rawkode Mar 19 '13 at 13:44
  • I just tried that (thanks!), but that gives me access to the previous tag, not the tag about to be created. – fearless_fool Mar 19 '13 at 14:15
  • If the file just contains the version, why do you require it in the repository? This file could be automatically generated with a `post-commit` hook? – Rawkode Mar 19 '13 at 14:43
  • 1
    what do you want to achieve with that? if you want the BUILD-version to show up at runtime, why don't you run the script in the deployment? – umläute Mar 19 '13 at 15:27
  • 1
    Yah, after sleeping on it, the better approach is to stop trying to get git to do something it doesn't want to do. Instead, just keep a file with "build number" in it, bump that number each time I run my `rake deploy` script. – fearless_fool Mar 20 '13 at 14:59

2 Answers2

1

update

This is a Ruby specific answer, but you can probably implement something equivalent in your environment of choice...

original answer

After looking at the comments and digging deeper into git documentation, it seems imprudent to try to use the git tag as part of the version number, if only because the git tag isn't available until after the commit.

So I wrote a simple rake task to bump the build number directly in my config/version.rb file. I run this script before doing the commit and deployment:

# Read config/version.rb file containing
#   VERSION = [a, b, c]
# Overwrite config/version.rb file to contain:
#   VERSION = [a, b, c+1]
task :bump_version do
  desc "increment build number in config/version.rb"
  file = "config/version.rb"
  unless (File.exist?(file))
    $stderr.puts("cannot locate version file #{file}")
  else
    s = File.open(file) {|f| f.read}
    if (s =~ /(\d+)\D+(\d+)\D+(\d+)/)
      s1 = "VERSION = [#{$1}, #{$2}, #{$3.to_i + 1}]"
      $stderr.puts(s1)
      File.open(file, "w") {|f| f.puts(s1) }
    else
      $stderr.puts("cannot parse version file")
    end
  end
end

Works fine for me.

fearless_fool
  • 33,645
  • 23
  • 135
  • 217
-1

Use git commit --ammend -m <pithy comment> to add changes to the last commit. This would go just before the push command in your example above.

David Culp
  • 5,354
  • 3
  • 24
  • 32