“保持”的最佳方式;硬编码;Git存储库中的版本号

“保持”的最佳方式;硬编码;Git存储库中的版本号,git,version-control,Git,Version Control,我在Github上有一个软件(一些脚本)。比如说,有人通过直接链接下载软件(没有克隆,我不认为用户有Git)。如何在启动脚本时显示当前版本号?如果有人真的克隆了存储库,那么只需运行git descripe就很容易了。但是,正如我所说,我不想假设Git是可用的 我只找到了下面的解决方案,这是相当烦人和糟糕的。我创建了一个post-commit和一个pre-commit钩子,使存储库中的文件version.txt保持最新,如下所示: 预提交: #!/bin/sh #################

我在Github上有一个软件(一些脚本)。比如说,有人通过直接链接下载软件(没有克隆,我不认为用户有Git)。如何在启动脚本时显示当前版本号?如果有人真的克隆了存储库,那么只需运行git descripe就很容易了。但是,正如我所说,我不想假设Git是可用的

我只找到了下面的解决方案,这是相当烦人和糟糕的。我创建了一个post-commit和一个pre-commit钩子,使存储库中的文件version.txt保持最新,如下所示:

预提交:

#!/bin/sh

#####################################################
#hook for handling version.txt
#we get the current version number in the form x.y-h, 
#extract x.y and write the next version number
#x.(y+1) to version.txt before push
#####################################################
VER=`git describe --long`
MAIN=`echo $VER | awk -F "-" '/1/ {print $1}'`
SUB=`echo $VER | awk -F "-" '/1/ {print $2}'`
SUBNEXT=$((SUB+1))
echo $MAIN-$SUBNEXT > version.txt
git add version.txt
#!/bin/bash

####################
#update version.txt
####################
git rm -q version.txt
VER=`git describe --long`
echo $VER > version.txt
提交后:

#!/bin/sh

#####################################################
#hook for handling version.txt
#we get the current version number in the form x.y-h, 
#extract x.y and write the next version number
#x.(y+1) to version.txt before push
#####################################################
VER=`git describe --long`
MAIN=`echo $VER | awk -F "-" '/1/ {print $1}'`
SUB=`echo $VER | awk -F "-" '/1/ {print $2}'`
SUBNEXT=$((SUB+1))
echo $MAIN-$SUBNEXT > version.txt
git add version.txt
#!/bin/bash

####################
#update version.txt
####################
git rm -q version.txt
VER=`git describe --long`
echo $VER > version.txt

令人恼火的是,version.txt总是以git状态显示为未跟踪文件。处理此问题的最佳方法是什么?

在git repo中将版本记录到文件中的方法是没有git的用户唯一可用的访问权限(因为他们无法通过git命令读取版本)

为了提高效率,您可以使用pre-push钩子,而不是pre-commit钩子和post-commit钩子

在预推钩子中,只需使用最新版本更新
version.txt
文件并提交更改。脚本如下所示:

#!/bin/sh

ver=$(git describe --long)
echo $ver > version.txt
git add version.txt
git commit -m 'update version.txt'
echo "update version successful"