我在git存储库中有一些愚蠢的bash脚本,供其他一些人使用。这些都是在我无法控制的客户端上运行的,但我想鼓励用户保持它们是最新的,而不是强迫用户每次都连接到远程。
Bash伪代码应该是这样的:
if [[ thirty_days_ago < date_of_last_fetch ]] then;
echo "Warning, these scripts are more than 30 days old. You should try a git pull"
fi
我已经做了一些搜索,但还没有找到一个可以运行的命令或一个mtime可以读取的文件,以指示用户最后一次检查远程。我希望这是一个选项,它将在每个远程可用,但我会接受我能得到的。
文件
.git/refs/remotes/remotename/branchname
在每次获取分支时都会更新。查看mtime
如果要检查远程分支是否已合并到本地分支中,可以检查"remotename/branchname"one_answers"branchname"的refs是否相等:
getref='git log --format="%H" -n 1'
if [ `$getref remotename/branchname` != `$getref branchname` ] ; then
echo "the local branch and the remote branch diverged! Please pull!"
fi
要做到这一点,最简单的方法是有一个脚本来执行获取/拉取,并在.git
目录下的一个文件中手动记录时间。
您也可以使用最后一次上游提交的时间作为粗略的度量。
if [ "$(git log -1 --format=%ct origin/master)" -lt "$(date -d'30 days ago' +%s)" ]; then
echo 'Warning....'
fi
我结合了几个解决方案,使用stat [OS dependent flags] .git/FETCH_HEAD
来获得该文件的modified time
,如下面的链接答案。
无论是否取到任何东西,它都会在每次取操作发生时更新
https://stackoverflow.com/a/54264210/622276此代码段是我自己维护的bash提示git集成的一部分,但请随意窃取代码段。
# No repo == no more work
local REPO_ROOT=`git rev-parse --show-toplevel 2> /dev/null`
if [[ -n $REPO_ROOT && -e "$REPO_ROOT/.git/FETCH_HEAD" ]]; then
case $OSTYPE in
darwin*)
local LAST_FETCH="$(stat -f '%m' $REPO_ROOT/.git/FETCH_HEAD)"
local FETCH_THRESHOLD="$(date -v-15m +%s)"
;;
*)
local LAST_FETCH="$(stat -c %Y $REPO_ROOT/.git/FETCH_HEAD)"
local FETCH_THRESHOLD="$(date -d'15 minutes ago' +%s)"
;;
esac
# Fork fetch process in background
if [[ $LAST_FETCH -lt $FETCH_THRESHOLD ]]; then
git fetch --all --quiet --prune 2> /dev/null &
fi
fi