0

I'm currently writing a bash script to remind me to rebase git repos when the local master branch is found to be behind origin/master.

So far I have come up with the following, but $? always returns 1 (I suspect this is because even an empty diff still loads less.

#!/bin/bash

REPO_PATH=$1
cd $REPO_PATH

# flow once inside repo
{
  git fetch
  git diff master origin/master
} &> /dev/null

if [ "" = $? ]
then
  echo "Empty"
  # logic to follow
else
  {
  git pull
  } &> /dev/null
  echo "Don't forget to rebase!!!"
  echo $REPO_PATH
fi

# check for changes to master
# If master is behind origin/master
# then pull master and notify me to rebase

# run this at the start of the day (this script should be run from my start_work
# script and should also be periodically run throughout the day. [maybe every
# time I'm about to run coverage/push?])

Anybody have any ideas?

1

You should use git-merge-base’s --is-ancestor test:

if git merge-base --is-ancestor origin/master master; then
    echo Empty
else
    echo "Don't forget to rebase!"
fi
0

Instead of looking at the exit status, you could look at the actual output

eg

git fetch &> /dev/null
diffs=$(git diff master origin/master)

if [ -z "$diffs" ]
then
  echo "Empty"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.