bash 如何编写一个 shell 脚本来检查 git 存储库是否是最新的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13736385/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a shell script that checks if git repository is up to date?
提问by Logan
#!/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
The above code is something I have edited from a script I have found on stackoverflow to install Emacs from git repository. What I want this script to do is to install any programs from git repos, and update them if an update is necessary. Of course I will have to provide the steps to install it. In this script's case it just needs to copy the clientsidedirectory to /usr/share/gedit/plugins/directory.
上面的代码是我从我在 stackoverflow 上找到的脚本中编辑的,用于从 git 存储库安装 Emacs。我想让这个脚本做的是从 git repos 安装任何程序,并在需要更新时更新它们。当然,我将不得不提供安装它的步骤。在这个脚本的情况下,它只需要将clientside目录复制到/usr/share/gedit/plugins/目录。
I do not need any help with how to install any script but what I need is how to check if repository is up to date and go from there.
我不需要关于如何安装任何脚本的任何帮助,但我需要的是如何检查存储库是否是最新的并从那里开始。
Right now what I don't understand is this part:
现在我不明白的是这部分:
git pull 1>&1 | grep "Already up-to-date."
    if [[ ! $? -eq 0 && ! $init ]]; then
.....
    fi
When I run git pull 1>&1 | grep "Already up-to-date." && $?in terminal the output is Already up-to-date. 0. So I understand that this is the part that checks for updates however the next part doesn't execute (the if statement)- which is the part that copies the directory to gedit plugin directory. I do not understand what 1>$1means or what $?means. Therefore I could not solve the problem... And what I do not understand is why it thinks that Branch is movedwhen it is not up to date (I'm just assuming that it says that when git pulldoesn't return 0in the if statement).
当我git pull 1>&1 | grep "Already up-to-date." && $?在终端中运行时,输出是Already up-to-date. 0. 所以我知道这是检查更新的部分,但是下一部分不执行(if 语句)- 这是将目录复制到 gedit 插件目录的部分。我不明白什么1>$1意思或什么$?意思。因此我无法解决问题......我不明白的是为什么它认为Branch is movedwhen 它不是最新的(我只是假设它说 when在 if 语句git pull中不返回0)。
I'm sure it has a simple solution and the answer will be both educating for bash and git. I appreciate all the help.
我敢肯定它有一个简单的解决方案,答案将是对 bash 和 git 的教育。我感谢所有的帮助。
I'm using ubuntu 12.04.
我正在使用 ubuntu 12.04。
回答by VonC
I would rather use the solution of "git: check if pull needed":
我宁愿使用“ git: check if pull needed”的解决方案:
git fetch origin
reslog=$(git log HEAD..origin/master --oneline)
if [[ "${reslog}" != "" ]] ; then
  git merge origin/master # completing the pull
  ...
回答by Claudio Floreani
As already noticed by Vonc, this question overlaps "git: check if pull needed".
正如 Vonc 已经注意到的,这个问题与“ git: check if pull needed”重叠。
ThereI suggested the following one-line script that takes the SHA1 of your last commited version and compares it to the one of the remote origin
在那里,我建议使用以下单行脚本,该脚本采用上次提交版本的 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"
回答by Mike
I had to edit Claudio's answer
我不得不编辑克劳迪奥的答案
[ "`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"

