# Copyright (C) 2012 Andrea Valle
#   
# This file is part of git4f.
# 
# git4f is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# git4f is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with git4f.  If not, see <http://www.gnu.org/licenses/>.

[alias]
  #get/set integration branch
  intbr       = "!f() { [ $# -gt 1 ] && { echo Too many parameters >&2 && exit 2 ;};                                              \
                        [ $# -eq 0 ] && { git config 4f.intbr && exit 0 || { echo Integration branch not set >&2 && exit 1 ;}; }; \
                        git show-ref --quiet --verify \"refs/heads/$1\" || { echo Not a valid branch $1 >&2 && exit 1 ;};         \
                        git config 4f.intbr $1 ;                                                                                  \
                      }; f"
  intbr-unset = config --unset 4f.intbr
  intbr-istracked = "!f() { br_int=$(git intbr) || exit 1; \
                            u=$(git rev-parse --symbolic-full-name --abbrev-ref $br_int@{upstream} 2>/dev/null) || \
                              { echo Integration branch \\'$br_int\\' not tracked >&2 && exit 1 ;} ; \
                            echo \"$br_int <- $u\"; \
                          }; f"
  intbr-pull = "!f() { git chk-intbr || exit 1;                                        \
                       d=$(git intbr-istracked 2>&1) &&                                \
                         { echo Pulling \\'$(git intbr)\\' && git pull || exit $?;} || \
                         { echo Intbr \\'$(git intbr)\\' not tracked, jump pull. ;}    \
                     }; f"
  intbr-push = "!f() { git chk-intbr            || exit 1;                               \
                       d=$(git intbr-istracked) || { echo Cannot push. >&2 && exit 1 ;}; \
                       remote=$(git config --get branch.$(git intbr).remote);            \
                       echo Pushing \\'$(git intbr)\\' to \\'$remote\\';                 \
                       git push $remote $(git intbr);                                    \
                     }; f"
  #
  #switch aliases
  chk-intbr   = "!git intbr 1>/dev/null && git checkout $(git intbr)"
  chk-back    = checkout @{-1}

  currbr      = "!f() { currbr=$(git symbolic-ref -q HEAD) || { echo detached-head >&2 && exit 1 ;}; \
                        currbr=$(git rev-parse --symbolic-full-name --abbrev-ref $currbr);           \
                        echo $currbr;                                                                \
                      }; f"

  #feature branch mgt
  ftrbr-start = "!f() { [ $# -ne 1 ] && echo \"Usage: git ftrbr-start <brname>\" >&2 && exit 2; \
                         br_ftr_pre=$(git config --get 4f.ftrbr-prefix) 2>/dev/null;            \
                         git intbr-pull || exit 1;                                              \
                         echo Creating new ftr branch starting from \\'$(git intbr)\\';         \
                         git checkout -b ${br_ftr_pre}$1                                        \
                       ;}; f"

  ftrbr-integrate = "!f() { br_start=$(git currbr) || { echo Cannot integrate. >&2 && exit 1 ;};  \
                            git intbr-pull || exit 1;                                             \
                            echo Merging \\'$br_start\\' into \\'$(git intbr)\\';                 \
                            git merge $(git config --get 4f.ftrbr-integrate-merge-opt) $br_start; \
                          }; f"

  ftrbr-integrate-rebase   = "!f() { br_start=$(git currbr)  || { echo Cannot integrate. >&2 && exit 1 ;};  \
                                     git ftrbr-update-rebase || exit 1;                                     \
                                     git chk-intbr           || exit 1;                                     \
                                     echo Merging \\'$br_start\\' into \\'$(git intbr)\\';                  \
                                     git merge $(git config --get 4f.ftrbr-integrate-rebase-opt) $br_start; \
                             }; f"
                              
  ftrbr-update-merge = "!f() { br_start=$(git currbr) || { echo Cannot update. >&2 && exit 1 ;};     \
                               git intbr-pull         || exit 1;                                     \
                               git checkout $br_start || exit 1;                                     \
                               echo Merging \\'$(git intbr)\\' into \\'$br_start\\';                 \
                               git merge $(git config --get 4f.ftrbr-update-merge-opt) $(git intbr); \
                             }; f"

  ftrbr-update-rebase   = "!f() { br_start=$(git currbr) || { echo Cannot update. >&2 && exit 1 ;}; \
                                  git intbr-pull         || exit 1;                                 \
                                  git checkout $br_start || exit 1;                                 \
                                  echo Rebasing \\'$br_start\\' onto \\'$(git intbr)\\';            \
                                  git rebase $(git intbr);                                          \
                                }; f"

  ftrbr-push = "!f() { d=$(git intbr-istracked) || { echo Cannot push. >&2 && exit 1 ;}; \
                       git ftrbr-integrate      || exit 1;                               \
                       git intbr-push;                                                   \
                     }; f"

  ftrbr-push-rebase = "!f() { d=$(git intbr-istracked)   || { echo Cannot push. >&2 && exit 1 ;}; \
                              git ftrbr-integrate-rebase || exit 1;                               \
                              git intbr-push;                                                     \
                            }; f"

#configurations
#[4f]
#  intbr                      = master
#  ftrbr-prefix               = ftr/
#  ftrbr-integrate-merge-opt  = 
#  ftrbr-integrate-rebase-opt = 
#  ftrbr-update-merge-opt     = --no-ff