Bump2version + pre-commit hook in python code

127 Views Asked by At

I'm using bump2version python module to increment my python codebase version automatically. When the pre-commit hook runs after the commit, the version.sh goes into indefinite loop and version keeps on incrementing in init.py. Did I miss anything

.bumpversion.cfg

[bumpversion]
current_version = 0.0.0
commit = True
message = "Merge the new version"
[bumpversion:file:__init__.py]

.pre-commit-config.yaml

repos:
  - repo : local
    hooks:
      - id: VersionIncrement
        name: VersionIncrement
        entry: ./version.sh
        language: script      
        exclude: (.pre-commit-config.yaml)|(.git/)

version.sh

#!/usr/bin/env bash

git_branch_name=`git branch --show-current`
echo $git_branch_name
if [[ $git_branch_name =~ "feature/" ]]; then
   bump2version --allow-dirty --verbose patch
   exit 0
elif [[ $git_branch_name == "dev" ]]; then
   bump2version --allow-dirty --verbose minor
   exit 0
elif [[ $git_branch_name == "master" ]]; then
   bump2version --allow-dirty --verbose major
   exit 0
else
   echo "branch does not exist"
   exit 1
fi
1

There are 1 best solutions below

0
On

if bump2version is making a commit, yes you'll get an infinite commit with your setup

I would not recommend automatically bumping a version like you're doing (if you really want to increment some number after every commit, consider using git describe which does this automatically without having to change your committed code). both git and pre-commit are not designed to do what you're doing.

if you really want to continue with what you're doing you can skip the infinite recursion by doing something like:

SKIP=VersionIncrement bump2version ...

which will utilize SKIP= to bypass itself

or you can use bump2version's --dry-run which may be more appropriate

that said, I really don't expect making a commit to work while you're making a commit -- and will probably break git or your history in mysterious ways (especially when coupled with git commit -a or such)


disclaimer: I wrote pre-commit