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

15,578

Solution 1

I would rather use the solution of "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
  ...

Solution 2

As already noticed by Vonc, this question overlaps "git: check if pull needed".

There I 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

[ `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"

Solution 3

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"
Share:
15,578
Logan
Author by

Logan

Follow me @github/aponxi! I write free, open source code!

Updated on July 22, 2022

Comments

  • Logan
    Logan almost 2 years
    #!/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 clientside directory to /usr/share/gedit/plugins/ directory.

    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>$1 means or what $? means. Therefore I could not solve the problem... And what I do not understand is why it thinks that Branch is moved when it is not up to date (I'm just assuming that it says that when git pull doesn't return 0 in the if statement).

    I'm sure it has a simple solution and the answer will be both educating for bash and git. I appreciate all the help.

    I'm using ubuntu 12.04.

    • Amadan
      Amadan over 11 years
      1>&1 is weird, I have no clue; it redirects stdout to stdout. You usually see it in 2>&1 form, which redirects stderr (2) to stdout (1). $? is last exit code; it will be 0 if grep matched something, non-0 if it failed to match (or experienced another error, like accessing non-existent file).
    • Logan
      Logan over 11 years
      So I'm guessing that 1>&1 is totally pointless... wouldn't make a difference if it was there or not.
  • VonC
    VonC about 11 years
    Interesting alternative. +1
  • Max
    Max over 9 years
    thanks for this! i took this simple one-liner and throw it into some small check_mk monitoring script: github.com/MaxWinterstein/check_git_pullstatus