#!/bin/bash
# Copyright (c) 2010-2014 David Sugar, Tycho Softworks
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

current=`git branch -a | grep "^[*] " | cut -f2 -d\   `
topic=`echo $current | grep topic[-] | sed s/topic-//`

set -e

case "$1" in
add)
    if test ! -z "$topic" ; then
        echo "*** cannot add from topic branch" ; exit 1 ; fi
    shift
    if test -z "$*" ; then
        echo "*** no topic" ; exit 1 ; fi
    echo "creating topic $* from $current"
    git checkout -b topic-"$*"
    exit 0
    ;;
master)
    shift
    set +e
    git commit -am "changes for $topic"
    git checkout master
    exit 0
    ;;
merge)
    shift
    if test -z "$topic" ; then
        git merge topic-"$*"
    else
        master="${1:-master}"
        git merge $master
    fi
    ;;
save|close)
    shift
    target="${1:-master}"
    if test -z "$topic" ; then
        echo "*** not in topic branch to save" ; exit 1 ; fi
    echo "merging topic $topic into $target"
    set +e
    git commit -am "changes for $topic"
    git -e
    git checkout "$target"
    git merge --squash topic-$topic
    git commit
    git branch -D topic-$topic
    exit 0
    ;;
rebase)
    shift
    target="${1:-master}"
    if test -z "$topic" ; then
        echo "*** not in topic branch to rebase" ; exit 1 ; fi
    git checkout ${target}
    git pull
    git checkout topic-$topic
    git rebase ${target}
    exit 0
    ;;
patch)
    shift
    target="${1:-master}"
    if test -z "$topic" ; then
        echo "*** not in topic branch for patch" ; exit 1 ; fi
    git checkout -b patch-"$topic" $target
    git merge --squash topic-"$topic"
    git commit -a -m "$* changes"
    git format-patch $target
    git checkout topic-"$topic"
    git branch -d patch-"$topic"
    exit 0
    ;;
kill)
    shift
    if test ! -z "$topic" ; then
        if test "$topic" == "$*" ; then
            echo "*** cannot kill from topic branch" ; exit 1 ; fi
    fi
    git branch -D topic-"$*"
    exit 0
    ;;
drop)
    shift
    if test ! -z "$topic" ; then
        if test "$topic" == "$*" ; then
            echo "*** cannot drop from topic branch" ; exit 1 ; fi
    fi
    git branch -d topic-"$*"
    exit 0
    ;;
list)
    git branch | grep topic\- | sed s/topic-//
    exit 0
    ;;
esac

if test -z "$*" ; then
    git branch | grep topic\- | sed s/topic-//
    exit 0
fi

set +e
list=`git branch | grep topic-"$*"`
set -e

if test -z "$list" ; then
    if test ! -z "$topic" ; then
        echo "*** cannot add from topic branch" ; exit 1 ; fi
    echo "creating topic $*..."
    git checkout -b topic-"$*"
    exit 0
fi

echo "switching topic $*..."
git checkout topic-"$*"
exit 0

