From d8395ba2e9c0a91f0df7519c4037e032fd8afaf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Fri, 31 Jul 2015 16:14:37 +0200 Subject: [PATCH] =?UTF-8?q?Create=20a=20bash=20version=20of=20git-psykoreb?= =?UTF-8?q?ase=20=E2=80=94=20Fixes=20#411?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/git-psykorebase | 86 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100755 bin/git-psykorebase diff --git a/bin/git-psykorebase b/bin/git-psykorebase new file mode 100755 index 0000000..8059082 --- /dev/null +++ b/bin/git-psykorebase @@ -0,0 +1,86 @@ +#!/usr/bin/env bash +PROGRAM=$0 +PRIMARY_BRANCH="" +SECONDARY_BRANCH="" +FF="--ff" +CONTINUE="no" + +function current_branch() { + git rev-parse --abbrev-ref HEAD +} + +function usage() +{ + echo "USAGE: ${PROGRAM} PRIMARY_BRANCH [SECONDARY_BRANCH]" + echo "USAGE: ${PROGRAM} --continue" + echo "" + echo "OPTIONS:" + echo " --no-ff: Force rebase commit." + echo " -c|--continue: Continue after the user updates conflicts." +} + +while [[ $# > 0 ]] +do + key="$1" + + case $key in + --no-ff) + FF="--no-ff" + ;; + -c|--continue) + CONTINUE=yes + ;; + *) + if [[ "$PRIMARY_BRANCH" == "" ]]; then + PRIMARY_BRANCH=$key + elif [[ "$SECONDARY_BRANCH" == "" ]]; then + SECONDARY_BRANCH=$key + else + usage + exit 20 # Error during arguments parsing + fi + ;; + esac + shift # past argument or value +done + +if [[ "$SECONDARY_BRANCH" == "" ]]; then + SECONDARY_BRANCH=$(current_branch) +fi + +if [[ "$CONTINUE" == "yes" ]]; then + TARGET_BRANCH=$(current_branch) + set $(echo "$TARGET_BRANCH" | sed -e "s/-rebased-on-top-of-/\n/g") + if [[ $# != 2 ]]; then + echo "Couldn't continue rebasing on ${TARGET_BRANCH}" + exit 30 # Impossible to detect PRIMARY_BRANCH AND SECONDARY_BRANCH + fi + + SECONDARY_BRANCH=$1 + PRIMARY_BRANCH=$2 + + echo "Continuing rebasing of $SECONDARY_BRANCH on top of $PRIMARY_BRANCH" + git commit || exit 51 + git branch -d ${SECONDARY_BRANCH} || exit 52 + git branch -m ${TARGET_BRANCH} ${SECONDARY_BRANCH} || exit 53 + +elif [[ "$PRIMARY_BRANCH" == "" ]]; then + usage + exit 10 # Missing arguments PRiMARY_BRANCH +else + echo "Rebasing $SECONDARY_BRANCH on top of $PRIMARY_BRANCH" + TARGET_BRANCH="${SECONDARY_BRANCH}-rebased-on-top-of-${PRIMARY_BRANCH}" + + git checkout ${PRIMARY_BRANCH} || exit 41 + git checkout -b ${TARGET_BRANCH} || exit 42 + git merge ${SECONDARY_BRANCH} ${FF} \ + -m "Psycho-rebased branch ${SECONDARY_BRANCH} on top of ${PRIMARY_BRANCH}" + + if [[ $? == 0 ]]; then + git branch -d ${SECONDARY_BRANCH} || exit 43 + git branch -m ${TARGET_BRANCH} ${SECONDARY_BRANCH} || exit 44 + else + echo "Resolve the conflict and run ``${PROGRAM} --continue``." + exit 1 + fi +fi