gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:03:11 26 4
gpt4 key购买 nike

我有一个本地开发的 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,然后就不容易区分哪个版本正在运行等等(因为两个连续的提交可能有相同的 构建版本!)

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

最佳答案

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

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

#!/bin/sh
bumpversion minor
git add versionfile

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

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

(这需要先pip install gitpython)

然后可以使用 git loggit rev-parse --short HEAD 将它与当前提交 ID 进行比较(7 位数字是 Git 默认的短上海)。

关于python - 使用 git 在 Python 网络服务器中自动增加版本号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51071891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com