0

VERSION variable placed in a file

i.e.: VERSION='1.0.0'

I want that when a new tag (release) created, somehow git shall replace the value of this VERSION variable to the release/tag name.

Is it possible?

vpas
  • 513
  • 1
  • 5
  • 18

1 Answers1

1

Not in git itself, no.

You can't always get what you want

You need to make note of two things here:

  1. A git commit makes a new snapshot, by taking whatever is in the index / staging-area and turning that into a commit.1

    This means that what will be extracted to a work-tree, given some commit, is whatever was in the index at the time. To make a new, different, saved tree, you must alter the index and then make a new commit.

  2. A tag is not a commit.

    Tags come in two forms: "lightweight" tags, which are simply references whose names start with refs/tags/, and "annotated" tags, where git adds an annotated-tag object to the repository—this tag object necessarily points to an already-existing commit object2—and then a new lightweight tag using the name, pointing to the annotated-tag object.

    Given a tag name, i.e., a reference starting with refs/tags/, that tag is "lightweight" when it points to a commit, and "annotated" when it points to an annotated-tag object, which then points to a commit. Either way, a tag points to a commit—some commit that already existed at the time you made the tag.

But if you try sometimes, you just might find, you get what you need

Therefore, any file actually stored in git, has to be changed (and added to the index / staging-area) at the time the commit is made. You can thus write a little program that:

  • updates the version file;
  • commits the update; and
  • tags the commit

which will achieve the result you want.

Alternatively, you can design your system so that the version file is not maintained through git, and instead is created by the build system, which extracts the version information from git in whatever manner you like.

See these SO questions about using git describe for more information:


1If the new commit would be identical to the current one, and the new commit is not a merge commit, git commit refuses to make the commit unless you force it, with --allow-empty.

2Technically, a lightweight tag, or the hash inside an annotated tag, can point to something other than a commit object. I have used this for some special hacks in the past, and it works. It's not something git will normally do though.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775