From 176c84fb9f69d0be34d52ca00ccf0b6a0e4a154d Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 30 Nov 2018 20:57:59 +0100 Subject: [PATCH] Extract code to print hg root from the prompt This is useful in other prompts, and potentially also to users. Don't use a dunderscore because we do way too many of them. --- share/functions/__fish_hg_prompt.fish | 18 ++---------------- share/functions/fish_print_hg_root.fish | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 share/functions/fish_print_hg_root.fish diff --git a/share/functions/__fish_hg_prompt.fish b/share/functions/__fish_hg_prompt.fish index ee71d305f..63aa0a57c 100644 --- a/share/functions/__fish_hg_prompt.fish +++ b/share/functions/__fish_hg_prompt.fish @@ -27,22 +27,8 @@ function __fish_hg_prompt --description 'Write out the hg prompt' return 1 end - # Find an hg directory above $PWD - # without calling `hg root` because that's too slow - set -l root - set -l dir (pwd -P) - while test $dir != "/" - if test -f $dir'/.hg/dirstate' - set root $dir"/.hg" - break - end - # Go up one directory - set dir (string replace -r '[^/]*/?$' '' $dir) - end - - if test -z "$root" - return 0 - end + set -l root (fish_print_hg_root) + or return 0 # Read branch and bookmark set -l branch (cat $root/branch 2>/dev/null; or echo default) diff --git a/share/functions/fish_print_hg_root.fish b/share/functions/fish_print_hg_root.fish new file mode 100644 index 000000000..6b28f4416 --- /dev/null +++ b/share/functions/fish_print_hg_root.fish @@ -0,0 +1,22 @@ +function fish_print_hg_root + # If hg isn't installed, there's nothing we can do + if not command -sq hg + return 1 + end + + # Find an hg directory above $PWD + # without calling `hg root` because that's too slow + set -l root + set -l dir (pwd -P) + while test $dir != "/" + if test -f $dir'/.hg/dirstate' + echo $dir/.hg + return 0 + end + # Go up one directory + set dir (string replace -r '[^/]*/?$' '' $dir) + end + + return 1 +end +