【问题标题】:Update version number on each commit每次提交时更新版本号
【发布时间】:2020-05-07 09:09:03
【问题描述】:

我在 How to write a Python script header 上遵循良好做法。我喜欢包含版本号的想法,所以我的 Python 文件中每个都有一行写着:__version__ = '1.0.0',彼此不同(文件版本彼此不相关)。 但是,在提交之前手动更改每个文件以编辑版本号非常繁琐,我最终会停止这样做。

有没有办法搜索在新提交时更改的每个文件并更新其版本号?


此问题已被标记为与Automatic version number both in setup.py (setuptools) AND source code? 重复。但是,这种情况有所不同,原因如下:

  1. 我有几个文件,每个文件都有自己的版本(事实上,我什至还没有 setup.py)。代码应单独更改每个文件的版本,这意味着一个文件可以在 50.3.4 版本上,另一个在 0.0.2 版本上。
  2. 我不关心 git 的标签。我根本不在 git 上保存版本。
  3. 我想避免自己设置版本号。他想在 Setup 或 git 上设置版本,然后另一个相同的问题。我希望版本号自动增加。例子。如果版本是 1.0.2,我希望它在提交后变为 1.0.3(如果该特定文件确实已更改)。

问题想要(手动)在 setup.py 文件或 git 上选择一个版本,然后相应地更新另一个版本。我根本没有 git 版本。

接受的解决方案(这确实是问题的解决方案)根本没有帮助,例如因为我必须手动更改文件的版本,然后它将该版本添加到 git(这与我的出于所有列出的原因想要)。

【问题讨论】:

标签: python git


【解决方案1】:

所以我做了自己的 shell 脚本。该脚本从git status 的输出中检查每个文件的版本行,以查看发生更改的文件并将数字加一。

if [ "$1" != '' ]; then
IFS="
"
git status -s
for p in `git status -s`    # For each file in git status
do
    file=`echo "$p" | rev | cut -d ' ' -f1 | rev`       # Take the name of the file
    if [ "$file" != "auto-version-commit.sh" ]; then    # Ignores himself
        if grep -q "__version__ = " $file; then     # Check version is in the file
            echo "Increasing version of file $file"
            oldver=`grep "__version__ = " $file`    # oldver=__version__ = 'z.y.x'
            verf1=`echo $oldver | cut -d '.' -f1`   # __version__ = 'z
            verf2=`echo $oldver | cut -d '.' -f2`   # y
            verf3=`echo $oldver | cut -d '.' -f3`   # x'
            oldnum=`echo $verf3 | sed 's/.$//'` # removes the quote from verf3
            newnum=`expr $oldnum + 1`       # x += 1
            newver="$verf1.$verf2.$newnum\'"    # joins string again.
            sed -i "s/$oldver\$/$newver/g" $file    # replaces line with the new one
        fi
    fi
done

git add -A
git commit -m $1
else
echo No commit message found, please add a message.
fi

我知道这段代码很糟糕。一般来说,我不太擅长 Linux 命令和 shell 脚本。请对代码进行任何更正(当然还有任何替代解决方案)。我相信通过更好地理解正则表达式,可以显着减少此文件。

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 2014-06-08
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    相关资源
    最近更新 更多