繁体   English   中英

如何编写一个shell脚本来检查git存储库是否是最新的?

[英]How to write a shell script that checks if git repository is up to date?

#!/bin/bash
#gedit tidy plugin
init=false
SRC_DIR=~/repos

DIRECTORY="Gedit-Clientside-Plugin"
#making repos directory
if [ ! -d "$SRC_DIR" ]; then mkdir $SRC_DIR; fi



if [ ! -d "$SRC_DIR/$DIRECTORY" ]; then
    init=true
    cd $SRC_DIR && pwd && git clone git://github.com/trentrichardson/Gedit-Clientside-Plugin.git && cd $DIRECTORY
else
    cd $SRC_DIR/$DIRECTORY
fi
#below here is what I'm having trouble with
git pull 1>&1 | grep "Already up-to-date."

if [[ ! $? -eq 0 && ! $init ]]; then
    read -e -p "## Branch moved, build and install Gedit-Clientside-Plugin? [Y/n] " yn
    if [[ $yn == "y" || $yn == "Y" || $yn == "" ]] ; then
        if [ ! -d "/usr/share/gedit/plugins/clientside" ]; then
            sudo cp ~/repos/Gedit-Clientside-Plugin/clientside /usr/share/gedit/plugins/ -r
        else
            echo "Directory already exists."
        fi
    fi
fi

上面的代码是我从stackoverflow上找到的脚本编辑的,用于从git repository安装Emacs。 我希望这个脚本做的是从git repos安装任何程序,并在需要更新时更新它们。 当然,我必须提供安装它的步骤。 在这个脚本的情况下,它只需要将clientside目录复制到/usr/share/gedit/plugins/目录。

我不需要任何有关如何安装任何脚本的帮助,但我需要的是如何检查存储库是否是最新的并从那里开始。

现在我不明白的是这一部分:

git pull 1>&1 | grep "Already up-to-date."

    if [[ ! $? -eq 0 && ! $init ]]; then
.....
    fi

当我运行git pull 1>&1 | grep "Already up-to-date." && $? git pull 1>&1 | grep "Already up-to-date." && $? 在终端输出Already up-to-date. 0Already up-to-date. 0 Already up-to-date. 0 所以我理解这是检查更新的部分,但下一部分不执行(if语句) - 这是将目录复制到gedit插件目录的部分。 我不明白1>$1意思或什么$? 手段。 因此我无法解决问题......而我不明白的是为什么它认为Branch is moved在它不是最新时Branch is moved (我只是假设它说当git pull没有返回0时if语句)。

我确信它有一个简单的解决方案,答案将是教育bash和git。 我感谢所有的帮助。

我正在使用ubuntu 12.04。

我宁愿使用“ git:check pull pull needed ”的解决方案:

git fetch origin
reslog=$(git log HEAD..origin/master --oneline)
if [[ "${reslog}" != "" ]] ; then
  git merge origin/master # completing the pull
  ...

正如Vonc已经注意到的那样,这个问题与“ git:check pull pull needed ”重叠。

在那里,我建议使用以下单行脚本,该脚本采用上次提交版本的SHA1并将其与远程源版本进行比较

[ `git log --pretty=%H ...refs/heads/master^` = `git ls-remote origin
-h refs/heads/master |cut -f1` ] && echo "up to date" || echo "not up to date"

我不得不编辑克劳迪奥的答案

[ "`git log --pretty=%H ...refs/heads/master^ | head -n 1`" = "`git ls-remote origin -h refs/heads/master |cut -f1`" ] && echo "up to date" || echo "not up to date"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM