Python 网络服务器中的自动递增版本号,git

Auto increment version number in a Python webserver, with git

我有一个 Python 我在本地开发的网络服务器(使用 Bottle 或 Flask 或任何其他):

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)

每次我有重大更新时,我都会:

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

并且远程版本已更新。 (注意:我在远程仓库上使用 git config receive.denyCurrentBranch updateInstead)。

问题:我经常忘记在每次提交时手动增加 BUILDVERSION,然后很难区分哪个版本是 运行 live 等等(因为连续两次提交可能有一样BUILDVERSION!)

问题:有没有办法在每次提交 Python + Git 时自动递增 BUILDVERSION 或任何东西类似的(BUILDVERSION 也可以是提交 ID...)将以网站页脚中的小字符出现,允许区分 Python 代码的连续版本。

Change version file automatically on commit with git所述, git hooks 更具体地说,可以使用 pre-commit 钩子来做到这一点。

在 Python 的特定情况下,versioneer or bumpversion 可以在 .git/hooks/pre-commit 脚本中使用:

#!/bin/sh
bumpversion minor
git add versionfile

另一种选择是使用 git 提交 ID 而不是 BUILDVERSION:

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

(这需要先pip install gitpython

然后可以使用 git loggit rev-parse --short HEAD 将其与当前提交 ID 进行比较(7 digits 是短 SHA 的 Git 默认值) .