From 9a134acd06f14aec9ad65874d271ca2cf814acce Mon Sep 17 00:00:00 2001 From: kovica Date: Fri, 12 Dec 2014 11:54:00 +0100 Subject: [PATCH 1/5] Update base.theme.bash Faster Mercurial status --- themes/base.theme.bash | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 364f734d..974b1c3f 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -127,6 +127,23 @@ function svn_prompt_vars { SCM_CHANGE=$(svn info 2> /dev/null | sed -ne 's#^Revision: ##p' ) } +function get_hg_root { + CURRENT_DIR=$(pwd) + + while [ "$CURRENT_DIR" != "/" ]; do + echo $CURRENT_DIiR + + if [ -d "$CURRENT_DIR/.hg" ]; then + echo "$CURRENT_DIR/.hg" + return 0 + fi + + CURRENT_DIR=$(dirname $CURRENT_DIR) + done + + return 1 +} + function hg_prompt_vars { if [[ -n $(hg status 2> /dev/null) ]]; then SCM_DIRTY=1 @@ -137,8 +154,10 @@ function hg_prompt_vars { fi SCM_PREFIX=${HG_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX} SCM_SUFFIX=${HG_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX} - SCM_BRANCH=$(hg summary 2> /dev/null | grep branch: | awk '{print $2}') - SCM_CHANGE=$(hg summary 2> /dev/null | grep parent: | awk '{print $2}') + + HG_ROOT=$(get_hg_root) + SCM_BRANCH=$(cat $HG_ROOT/branch) + SCM_CHANGE=$(hexdump -n 10 -e '1/1 "%02x"' $HG_ROOT/dirstate | cut -c-12) } function rvm_version_prompt { From 8914d452d8f4f9f97218282165c53d40463adf64 Mon Sep 17 00:00:00 2001 From: Kovica Date: Fri, 12 Dec 2014 15:08:07 +0100 Subject: [PATCH 2/5] We don't need return values --- themes/base.theme.bash | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 974b1c3f..b9f18960 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -128,20 +128,18 @@ function svn_prompt_vars { } function get_hg_root { - CURRENT_DIR=$(pwd) + local CURRENT_DIR=$(pwd) while [ "$CURRENT_DIR" != "/" ]; do echo $CURRENT_DIiR if [ -d "$CURRENT_DIR/.hg" ]; then echo "$CURRENT_DIR/.hg" - return 0 + return fi - + CURRENT_DIR=$(dirname $CURRENT_DIR) done - - return 1 } function hg_prompt_vars { From 6f126b80e45f94b51087fd92fffd41f256535942 Mon Sep 17 00:00:00 2001 From: Kovica Date: Fri, 12 Dec 2014 15:19:28 +0100 Subject: [PATCH 3/5] Added comments --- themes/base.theme.bash | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index b9f18960..5d09d778 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -127,6 +127,13 @@ function svn_prompt_vars { SCM_CHANGE=$(svn info 2> /dev/null | sed -ne 's#^Revision: ##p' ) } +# this functions returns absolute location of .hg directory if one exists +# It starts in the current directory and moves its way up until it hits /. +# If we get to / then no Mercurial repository was found. +# Example: +# - lets say we cd into ~/Projects/Foo/Bar +# - .hg is located in ~/Projects/Foo/.hg +# - get_hg_root starts at ~/Projects/Foo/Bar and sees that there is no .hg directory, so then it goes into ~/Projects/Foo function get_hg_root { local CURRENT_DIR=$(pwd) @@ -154,7 +161,10 @@ function hg_prompt_vars { SCM_SUFFIX=${HG_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX} HG_ROOT=$(get_hg_root) + + # Mercurial holds it's current branch in .hg/branch file SCM_BRANCH=$(cat $HG_ROOT/branch) + # Mercurial holds various information about the working directory in .hg/dirstate file. More on http://mercurial.selenic.com/wiki/DirState SCM_CHANGE=$(hexdump -n 10 -e '1/1 "%02x"' $HG_ROOT/dirstate | cut -c-12) } From 324e1edfced2872f2eeb52afa66877956b97f531 Mon Sep 17 00:00:00 2001 From: Kovica Date: Fri, 12 Dec 2014 15:51:12 +0100 Subject: [PATCH 4/5] Don't need that echo --- themes/base.theme.bash | 2 -- 1 file changed, 2 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 5d09d778..939532a4 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -138,8 +138,6 @@ function get_hg_root { local CURRENT_DIR=$(pwd) while [ "$CURRENT_DIR" != "/" ]; do - echo $CURRENT_DIiR - if [ -d "$CURRENT_DIR/.hg" ]; then echo "$CURRENT_DIR/.hg" return From 35adbaf7b35c3d1d53ffab870ef0fccc7fe9b141 Mon Sep 17 00:00:00 2001 From: Kovica Date: Fri, 12 Dec 2014 20:32:04 +0100 Subject: [PATCH 5/5] On new repositories ./hg/branch and .hg/dirstate do not exist. In this case use "hg summary" --- themes/base.theme.bash | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 939532a4..06c7e3cc 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -160,10 +160,19 @@ function hg_prompt_vars { HG_ROOT=$(get_hg_root) - # Mercurial holds it's current branch in .hg/branch file - SCM_BRANCH=$(cat $HG_ROOT/branch) - # Mercurial holds various information about the working directory in .hg/dirstate file. More on http://mercurial.selenic.com/wiki/DirState - SCM_CHANGE=$(hexdump -n 10 -e '1/1 "%02x"' $HG_ROOT/dirstate | cut -c-12) + if [ -f $HG_ROOT/branch ]; then + # Mercurial holds it's current branch in .hg/branch file + SCM_BRANCH=$(cat $HG_ROOT/branch) + else + SCM_BRANCH=$(hg summary 2> /dev/null | grep branch: | awk '{print $2}') + fi + + if [ -f $HG_ROOT/dirstate ]; then + # Mercurial holds various information about the working directory in .hg/dirstate file. More on http://mercurial.selenic.com/wiki/DirState + SCM_CHANGE=$(hexdump -n 10 -e '1/1 "%02x"' $HG_ROOT/dirstate | cut -c-12) + else + SCM_CHANGE=$(hg summary 2> /dev/null | grep parent: | awk '{print $2}') + fi } function rvm_version_prompt {