Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change version file automatically on commit with git

We have our master branch that we merge our features into. I need to be able to increment our version on commit/merge to the master automatically as a part of the merge. Is there a way i can do this so that the upped version is committed as a part of this commit without having to have an automatic 're checkout, change, commit' that will effectively double all our commits?

like image 477
mawk044 Avatar asked Jun 14 '13 05:06

mawk044


2 Answers

You can use git hooks for that.

The pre-commit hook specifically. You can create one from the sample in .git/hooks/pre-commit.sample by removing the .sample suffix and editing it. The content of pre-commit will be executed just before the commit.

It could contain something like this

#!/bin/sh
command-that-increases-version version.text
git add version.text

Any modification of version.text will then be included in the commit.

Finally some advice: you may want to avoid doing this altogether, since it may lead to a lot of merge conflicts when different branches store different values in version.text.

like image 179
Klas Mellbourn Avatar answered Sep 17 '22 13:09

Klas Mellbourn


1. main.py

version = open("VERSION.TXT", "r").read()

2. root dir repository

echo 1 > VERSION.TXT

3. add git hook

# cat .git/hooks/post-commit
#/bin/sh
echo "post-commit started"  
version=$(cat VERSION.TXT); y=$((version=version+1)); echo $y > VERSION.TXT 

4. done

git commit .
git push
like image 33
J. Ceron Avatar answered Sep 19 '22 13:09

J. Ceron