How can I include a version file in a commit?

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:

  • Gitlab key format issue
  • Why am I getting “Commit failed with error: pathspec … did not match any file(s)”?
  • jenkins git plugin using --local when authenticating with HTTPS
  • GIT: remove from 2nd to 5th commits in the remote branch
  • Ignore git directory
  • Merging IntelliJ tasks with no-ff
  • 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?

  • Dockerfile versioning best practice
  • Copying an untracked folder to another branch
  • Nested git repositories without remotes (a.k.a. git submodule without remotes)
  • Gitstats Eclipse
  • What is the correct way to merge upstream without losing changes?
  • Allow other developers to QA my code before pushing into main trunk?
  • 2 Solutions collect form web for “How can I include a version file in a commit?”

    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.

    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.

    Git Baby is a git and github fan, let's start git clone.