7

Say I have two branches: develop and feature.

Assume I also have a file called VersionNumber which has the following content:

BUILD_NUMBER 1

I want to use Git hooks so that, when I merge feature into develop, the BUILD_NUMBER field gets incremented automatically.

I thought about the following process using the post-merge hook:

  1. Check that branch being merged into is develop
  2. Update the VersionNumber file by incrementing BUILD_NUMBER by 1
  3. Add the updated file: git add VersionNumber
  4. Amend the commit: git commit --amend -C HEAD --no-verify

It all works fine until the final command. Git says I can't amend the commit in the middle of a merge (which is surprising to me, since I thought this was post-merge).

Any advice on how I can do this (using post-merge or any other hook for that matter)?

alguru
  • 404
  • 6
  • 16

1 Answers1

0

Unfortunately your question lacks code example. However I was able to reproduce for the fun of it. Indeed you can not amend things (such as a change to a file) to a merge commit - this seems to make some sense. Indeed you are post-merge but by using amend you try to go back into the merge commit and modify it.

So best chance you have, I think, is to simply add a commit for the version bump.

Anything else would just make it horribly complicated and prone to corner-cases that don't work and cost you lots of time figuring out how to salvage your commits and versioning. I always like to keep things as simple as posible. But maybe someone else can come up with something better.

This is the post-merge hook I wrote, if anyone is interested; does not check for the branch:

perl -i -pe 's/(\d+)/1+$1/e' VersionNumber 
echo "bump version: `cat VersionNumber`"
git add VersionNumber
git commit --amend -m "bump version: `cat VersionNumber`"
Frido Emans
  • 5,120
  • 2
  • 27
  • 42