Skip to content

Instantly share code, notes, and snippets.

@tifletcher
Created June 10, 2019 21:58
Show Gist options
  • Save tifletcher/290714341e552421e49149a82821ebd9 to your computer and use it in GitHub Desktop.
Save tifletcher/290714341e552421e49149a82821ebd9 to your computer and use it in GitHub Desktop.
git mergediff
#!/bin/bash
# preferences
# set DEBUG to "-v" or "" -- it will
# 1. activate shell tracing
# 2. be passed to curl as an argument
if [ -z "$DEBUG" ];
then
DEBUG=""
fi
# preconditions
set -e
if [ ! -z $DEBUG ];
then
set -x
fi
require () {
if ! command -v $1 > /dev/null 2>&1 ;
then
echo "Requires $1$2. Install $1 and rerun."
exit
fi
}
require git
# help command
if [ "$1" == "help" -o "$1" == "--help" ] ;
then
cat <<EOF
mergediff [srcref] targetref
Sets the state of the working tree to <targetref> and HEAD to <srcref>.
Uses current branch as the source reference when the argument is omitted.
Note that any changes in the working in the working tree when this command is run may result in undefined behavior.
EXAMPLE:
Show remote branch 'my-pr' as a diff against develop:
git mergediff develop origin/my-pr
Show remote branch 'my-pr' as a diff against the current branch:
git mergediff origin/my-pr
EOF
exit
fi
git fetch --all
if [ "$#" == "2" ] ;
then
SRCBRANCH=$1
shift
fi
TARGETBRANCH=$1
if [ ! -z "$SRCBRANCH" ];
then
echo
echo "Will run: git checkout $SRCBRANCH && git reset --hard"
read -p"Press enter to continue"
git checkout $SRCBRANCH && git reset --hard
fi
SRCREF=`git symbolic-ref HEAD`
git checkout $TARGETBRANCH
git symbolic-ref HEAD $SRCREF
git reset
git status -s
exit
# fallback to help command
$0 --help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment