# HG changeset patch # User Anton Shestakov <engored@ya.ru> # Date 1409850121 -32400 # Node ID 0657790983280ff68f8eef04837a2b9469dd6c4c # Parent 89d19a889252e2c0be704a9d424e43b07f0b4502 Move hg and git info getting in procedures. diff -r 89d19a889252 -r 065779098328 .bash_prompt --- a/.bash_prompt Thu Sep 04 20:26:57 2014 +0900 +++ b/.bash_prompt Fri Sep 05 02:02:01 2014 +0900 @@ -47,65 +47,15 @@ [[ "$TEST_DIR" == '/' ]] && break TEST_DIR=$(dirname "$TEST_DIR") done - - if [[ $HG_TRACKED && $USE_HG_PROMPT ]] ; then - local PROMPT_TEMPLATE="${P}{branch|quiet}{${Rd}{closed}}${P}☿{rev}{${B}+{tags|quiet|+}}{${Gd}*{bookmark}}{${R}{update}}{${G}+{rev|merge}}${R}{status|modified}${RESET}" - local HG_INFO=" $(hg prompt $PROMPT_TEMPLATE)" + + local HG_INFO= + if [[ $HG_TRACKED ]] ; then + _set_hg_info fi - if [[ $HG_TRACKED && ! $USE_HG_PROMPT ]] ; then - local SUM=$(LC_ALL=C hg sum 2>/dev/null) - local PARENTS=($(grep -Po '(?<=parent: ).*(?=:)' <<< "$SUM")) - local BRANCH=$(grep -Po '(?<=branch: ).*$' <<< "$SUM") - local COMMIT=$(grep -Po '(?<=commit: ).*$' <<< "$SUM") - local UPDATE=$(grep -Po '(?<=update: ).*$' <<< "$SUM") - - local REV=${PARENTS[0]} - local MERGEREV=${PARENTS[1]} - - BRANCH=${BRANCH/default/} - [ -z "$MERGEREV" ] || MERGEREV="+$MERGEREV" - case "$COMMIT" in - *'(clean)'*) - COMMIT='' - ;; - *added*|*modified*|*deleted*) - COMMIT='!' - ;; - esac - case "$UPDATE" in - *'(update)'*) - UPDATE='^' - ;; - *'(merge)'*) - UPDATE='' - ;; - *'(current)'*) - UPDATE='' - ;; - esac - local HG_INFO=" ${P}${BRANCH}☿${REV}${R}${UPDATE}${G}${MERGEREV}${R}${COMMIT}${RESET}" - fi + + local GIT_INFO= if [[ $GIT_TRACKED ]] ; then - local GIT_STATUS=$(LC_ALL=C git status 2>/dev/null) - local GIT_BRANCH=$(grep -Po '(?<=On branch ).*$' <<< "$GIT_STATUS") - local GIT_REV=$(LC_ALL=C git rev-parse --short HEAD 2>/dev/null) - - if [[ $GIT_STATUS == *'Your branch is behind'* ]] ; then - local GIT_BEHIND="$R^" - [[ $GIT_STATUS =~ behind[^$'\n']+([0-9]+)' 'commit ]] && GIT_BEHIND+=${BASH_REMATCH[1]} - fi - if [[ $GIT_STATUS == *'Your branch is ahead'* ]] ; then - local GIT_AHEAD="$Y^" - [[ $GIT_STATUS =~ ahead[^$'\n']+([0-9]+)' 'commit ]] && GIT_AHEAD+=${BASH_REMATCH[1]} - fi - if [[ $GIT_STATUS == *'Changes to be committed'* ]] ; then - local GIT_STAGED="$R!" - fi - if [[ $GIT_STATUS == *'Changes not staged'* ]] ; then - local GIT_UNSTAGED="$P?" - fi - - local GIT_INFO=" ${G}${GIT_BRANCH}±${GIT_REV}${GIT_BEHIND}${GIT_AHEAD}${GIT_STAGED}${GIT_UNSTAGED}${RESET}" + _set_git_info fi fi @@ -119,6 +69,68 @@ ${PROMPT_COLOR}\\\$${RESET} " } +function _set_hg_info { + if [[ $USE_HG_PROMPT ]] ; then + local PROMPT_TEMPLATE="${P}{branch|quiet}{${Rd}{closed}}${P}☿{rev}{${B}+{tags|quiet|+}}{${Gd}*{bookmark}}{${R}{update}}{${G}+{rev|merge}}${R}{status|modified}${RESET}" + HG_INFO=" $(hg prompt $PROMPT_TEMPLATE)" + else + local SUM=$(LC_ALL=C hg sum 2>/dev/null) + local PARENTS=($(grep -Po '(?<=parent: ).*(?=:)' <<< "$SUM")) + local BRANCH=$(grep -Po '(?<=branch: ).*$' <<< "$SUM") + local COMMIT=$(grep -Po '(?<=commit: ).*$' <<< "$SUM") + local UPDATE=$(grep -Po '(?<=update: ).*$' <<< "$SUM") + + local REV=${PARENTS[0]} + local MERGEREV=${PARENTS[1]} + + BRANCH=${BRANCH/default/} + [ -z "$MERGEREV" ] || MERGEREV="+$MERGEREV" + case "$COMMIT" in + *'(clean)'*) + COMMIT='' + ;; + *added*|*modified*|*deleted*) + COMMIT='!' + ;; + esac + case "$UPDATE" in + *'(update)'*) + UPDATE='^' + ;; + *'(merge)'*) + UPDATE='' + ;; + *'(current)'*) + UPDATE='' + ;; + esac + HG_INFO=" ${P}${BRANCH}☿${REV}${R}${UPDATE}${G}${MERGEREV}${R}${COMMIT}${RESET}" + fi +} + +function _set_git_info { + local STATUS=$(LC_ALL=C git status 2>/dev/null) + local BRANCH=$(grep -Po '(?<=On branch ).*$' <<< "$STATUS") + local REV=$(LC_ALL=C git rev-parse --short HEAD 2>/dev/null) + + if [[ $STATUS == *'Your branch is behind'* ]] ; then + local BEHIND="$R^" + [[ $STATUS =~ behind[^$'\n']+([0-9]+)' 'commit ]] && BEHIND+=${BASH_REMATCH[1]} + fi + if [[ $STATUS == *'Your branch is ahead'* ]] ; then + local AHEAD="$Y^" + [[ $STATUS =~ ahead[^$'\n']+([0-9]+)' 'commit ]] && AHEAD+=${BASH_REMATCH[1]} + fi + if [[ $STATUS == *'Changes to be committed'* ]] ; then + local STAGED="$R!" + fi + if [[ $STATUS == *'Changes not staged'* ]] ; then + local UNSTAGED="$P?" + fi + + GIT_INFO=" ${G}${BRANCH}±${P}${REV}${BEHIND}${AHEAD}${STAGED}${UNSTAGED}${RESET}" +} + # first find prompt in hgrc, then check it's actually enabled if grep -w '^prompt' ~/.hgrc &>/dev/null && hg help prompt &>/dev/null ; then USE_HG_PROMPT=1