使用 git 在 Python 网络服务器中自动增加版本号

Bas*_*asj 5 python git git-commit

我有一个我在本地开发的 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)
Run Code Online (Sandbox Code Playgroud)

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

git commit -am "Commit name" && git push
Run Code Online (Sandbox Code Playgroud)

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

问题:我经常忘记BUILDVERSION在每次提交时手动增加,然后就不容易区分哪个版本正在运行等(因为连续两次提交可能具有相同的BUILDVERSION!)

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

Bas*_*asj 8

正如在使用 git 提交时自动更改版本文件中提到的,git钩子,更具体地说,pre-commit可以使用钩子来做到这一点。

在 Python 的特定情况下,可以在 脚本内部使用versioneerbumpversion.git/hooks/pre-commit

#!/bin/sh
bumpversion minor
git add versionfile
Run Code Online (Sandbox Code Playgroud)

另一种选择是使用 git commit id 而不是 a BUILDVERSION

import git
COMMITID = git.Repo().head.object.hexsha[:7]    # 270ac70
Run Code Online (Sandbox Code Playgroud)

(这需要pip install gitpython先)

然后可以将它与当前提交 ID 进行比较,git log或者git rev-parse --short HEAD(7 位是短 SHA 的 Git 默认值)。