From 31a86c932785cbd0f738b295913ce975ba00ccb3 Mon Sep 17 00:00:00 2001 From: Taahir Ahmed Date: Fri, 18 Oct 2013 21:03:42 -0500 Subject: [PATCH] Added "upwards find" tests for expensive dvcs. Mercurial and Bazaar are written in Python, which, on my system, causes a noticeable hitch in the prompt generation when changing directories. I have added code that, before invoking the 'hg' or 'bzr' executables, first tests if we could possibly be in a bazaar or hg repository checkout by traversing the directory structure upwards to the root, looking for an '.hg' or '.bzr' folder. At least for me, this makes the prompt instantaneous. There may be more elegant solutions to this problem, but this one works. --- liquidprompt | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/liquidprompt b/liquidprompt index 26021eb..51f90e0 100755 --- a/liquidprompt +++ b/liquidprompt @@ -788,16 +788,33 @@ _lp_git_branch_color() } +_lp_upwards_find() +{ + while [[ $PWD != / ]] ; do + find "$PWD"/ -maxdepth 1 -name '$@' 2> /dev/null + cd .. + done +} + # MERCURIAL # # Get the branch name of the current directory _lp_hg_branch() { [[ "$LP_ENABLE_HG" != 1 ]] && return + + # First do a simple search to avoid having to invoke hg -- at least on my + # machine, the python startup causes a noticeable hitch when changing + # directories. + [[ -z $(_lp_upwards_find '.hg') ]] && return + + # We found an .hg folder, so we need to invoke hg and see if we're actually + # in a repository. + local branch branch="$(hg branch 2>/dev/null)" - [[ $? -eq 0 ]] && _lp_escape "$branch" + [[ $? -eq 0 ]] && _lp_escape "$branch" } # Set a color depending on the branch state: @@ -1013,6 +1030,15 @@ _lp_fossil_branch_color() _lp_bzr_branch() { [[ "$LP_ENABLE_BZR" != 1 ]] && return + + # First do a simple search to avoid having to invoke bzr -- at least on my + # machine, the python startup causes a noticeable hitch when changing + # directories. + [[ -z $(_lp_upwards_find '.bzr') ]] && return + + # We found an .bzr folder, so we need to invoke bzr and see if we're + # actually in a repository. + local branch branch="$(bzr nick 2> /dev/null)" [[ $? -ne 0 ]] && return @@ -1029,6 +1055,15 @@ _lp_bzr_branch() _lp_bzr_branch_color() { [[ "$LP_ENABLE_BZR" != 1 ]] && return + + # First do a simple search to avoid having to invoke bzr -- at least on my + # machine, the python startup causes a noticeable hitch when changing + # directories. + [[ -z $(_lp_upwards_find '.bzr') ]] && return + + # We found an .bzr folder, so we need to invoke bzr and see if we're + # actually in a repository. + local output output="$(bzr version-info --check-clean --custom --template='{branch_nick} {revno} {clean}' 2> /dev/null)" [[ $? -ne 0 ]] && return