Arda
Arda

Reputation: 10929

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

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.

Upvotes: 11

Views: 17227

Answers (3)

Mike
Mike

Reputation: 7831

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"

Upvotes: 5

Claudio Floreani
Claudio Floreani

Reputation: 2571

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"

Upvotes: 7

VonC
VonC

Reputation: 1324337

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
  ...

Upvotes: 16

Related Questions