Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

bash

shell

#!/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.

like image 953
Logan Avatar asked Dec 06 '12 03:12

Logan


People also ask

How do I check for git updates?

To check your Git version, open Command Prompt (Windows), Terminal (Mac), or the Linux terminal. The Git version you're currently using will be returned. Now that you know which version of Git you're using, you can decide if you want to update it or not.

How do you check the status of your local git repository?

To check the status, open the git bash, and run the status command on your desired directory. It will run as follows: $ git status.

How do you check if there are new commits in git?

Viewing a list of the latest commits. If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.

How to check Git for changes and then loop through changed files?

- Stack Overflow Shell script to check git for changes and then loop through changed files? Checks the remote git repository for any changes to pull. If there are changes in the remote git repository pull those changes. Loops through the files that are new or have been modified.

How to check if git branch is up to date?

If you use -v with git remote update ( git remote -v update) you can see which branches got updated, so you don't really need any further commands. Show activity on this post. you can use git status -uno to check if your local branch is up-to-date with the origin one. Show activity on this post.

How to get Git refs up to date?

First use git remote update, to bring your remote refs up to date. Then you can do one of several things, such as: git status -uno will tell you whether the branch you are tracking is ahead, behind or has diverged.

Can I git pull from a script?

You should be able to git pull from the script, sure. Once you have merged the changes in, there won't be any differences to report between the two master branches, so you'd have to diff HEAD^ and HEAD instead. If you're scripting and want to make sure the repo is in a committed state (e.g. no new, modified, or staged files), try this:


Video Answer


1 Answers

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
  ...
like image 79
VonC Avatar answered Oct 07 '22 00:10

VonC