7

I have a Python webserver (using Bottle or Flask or any other) that I develop locally:

BUILDVERSION = "0.0.128"

@route('/')
def homepage():    
    ... # using a template, and the homepage shows the BUILDVERSION in the footer
        # so that I always know which live version is running

...

run(host='0.0.0.0', port=8080)

Each time I have a significant update, I do:

git commit -am "Commit name" && git push

and the distant version is updated. (Note: I use git config receive.denyCurrentBranch updateInstead on the distant repo).

Problem: I often forget to manually increment the BUILDVERSION on each commit, and then it's not easy to distinguish which version is running live, etc. (because two consecutive commits could have the same BUILDVERSION!)

Question: Is there a way to have an auto-incrementing BUILDVERSION on each commit with Python + Git? or anything similar (the BUILDVERSION could also be the commit ID...) that would be present in small characters in the footer of the website, allowing to distinguish consecutive versions of the Python code.

3
  • If you run this command inside your repo git log | head -1 | awk '{print $2}' do you get the commit id? Commented Jun 27, 2018 at 21:52
  • @Hackerman on destination, yes (Linux), it works. On source, no (Windows ;)). Commented Jun 27, 2018 at 21:55
  • 1
    Possible duplicate of Change version file automatically on commit with git Commented Jun 27, 2018 at 22:39

1 Answer 1

12

As mentioned in Change version file automatically on commit with git, git hooks and more specifically a pre-commit hook can be used to do that.

In the specific case of Python, versioneer or bumpversion can be used inside the .git/hooks/pre-commit script:

#!/bin/sh
bumpversion minor
git add versionfile

Another option is to use the git commit id instead of a BUILDVERSION:

import git
COMMITID = git.Repo().head.object.hexsha[:7]    # 270ac70

(This requires pip install gitpython first)

Then it's possible to compare it with the current commit ID with git log or git rev-parse --short HEAD (7 digits is the Git default for a short SHA).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.