From c2efeb574040e97cca9b8558082af9fc1de4f8a4 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Wed, 30 Mar 2016 16:57:34 -0700 Subject: [PATCH] Make bobthefish git worktree aware Fixes #29 --- README.md | 2 + fish_prompt.fish | 111 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 106 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b19e888..953180c 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ You can override some of the following default options in your `config.fish`: set -g theme_display_git no set -g theme_display_git_untracked no set -g theme_display_git_ahead_verbose yes +set -g theme_git_worktree_support yes set -g theme_display_vagrant yes set -g theme_display_hg yes set -g theme_display_virtualenv no @@ -90,6 +91,7 @@ set -g default_user your_normal_user - `theme_display_ruby`. Use `no` to completely hide all information about Ruby version. By default Ruby version displayed if there is the difference from default settings. - `theme_display_vagrant`. This feature is disabled by default, use `yes` to display Vagrant status in your prompt. Please note that only the VirtualBox and VMWare providers are supported. - `theme_show_exit_status`. Set this option to yes to have the prompt show the last exit code if it was non_zero instead of just the exclamation mark. +- `theme_git_worktree_support`. If you do any git worktree shenanigans, setting this to `yes` will fix incorrect project-relative path display. If you don't do any git worktree shenanigans, leave it disabled. It's faster this way :) [fish]: https://github.com/fish-shell/fish-shell [screenshot]: https://cloud.githubusercontent.com/assets/53660/14413486/a5300f2c-ff30-11e5-9852-dd0643834a40.gif diff --git a/fish_prompt.fish b/fish_prompt.fish index 8e2008c..051e99a 100644 --- a/fish_prompt.fish +++ b/fish_prompt.fish @@ -20,6 +20,7 @@ # set -g theme_display_git no # set -g theme_display_git_untracked no # set -g theme_display_git_ahead_verbose yes +# set -g theme_git_worktree_support yes # set -g theme_display_vagrant yes # set -g theme_display_hg yes # set -g theme_display_virtualenv no @@ -67,7 +68,46 @@ end function __bobthefish_git_project_dir -S -d 'Print the current git project base directory' [ "$theme_display_git" = 'no' ]; and return - command git rev-parse --show-toplevel ^/dev/null + if [ "$theme_git_worktree_support" != 'yes' ] + command git rev-parse --show-toplevel ^/dev/null + return + end + + set -l git_dir (command git rev-parse --git-dir ^/dev/null); or return + + pushd $git_dir + set git_dir $PWD + popd + + switch $PWD/ + case $git_dir/\* + # Nothing works quite right if we're inside the git dir + # TODO: fix the underlying issues then re-enable the stuff below + + # # if we're inside the git dir, sweet. just return that. + # set -l toplevel (command git rev-parse --show-toplevel ^/dev/null) + # if [ "$toplevel" ] + # switch $git_dir/ + # case $toplevel/\* + # echo $git_dir + # end + # end + return + end + + set -l project_dir (dirname $git_dir) + + switch $PWD/ + case $project_dir/\* + echo $project_dir + return + end + + set project_dir (command git rev-parse --show-toplevel ^/dev/null) + switch $PWD/ + case $project_dir/\* + echo $project_dir + end end function __bobthefish_hg_project_dir -S -d 'Print the current hg project base directory' @@ -378,15 +418,72 @@ function __bobthefish_prompt_git -S -a current_dir -d 'Display the actual git st echo -ns (__bobthefish_git_branch) $flags ' ' set_color normal - set -l project_pwd (__bobthefish_project_pwd $current_dir) - if [ "$project_pwd" ] - if [ -w "$PWD" ] - __bobthefish_start_segment $__bobthefish_dk_grey $__bobthefish_med_grey - else - __bobthefish_start_segment $__bobthefish_med_red $__bobthefish_lt_red + if [ "$theme_git_worktree_support" != 'yes' ] + set -l project_pwd (__bobthefish_project_pwd $current_dir) + if [ "$project_pwd" ] + if [ -w "$PWD" ] + __bobthefish_start_segment $__bobthefish_dk_grey $__bobthefish_med_grey + else + __bobthefish_start_segment $__bobthefish_med_red $__bobthefish_lt_red + end + + echo -ns $project_pwd ' ' + end + return + end + + set -l project_pwd (command git rev-parse --show-prefix ^/dev/null | sed -e 's#/$##') + set -l work_dir (command git rev-parse --show-toplevel ^/dev/null) + + # only show work dir if it's a parent… + if [ "$work_dir" ] + switch $PWD/ + case $work_dir/\* + set work_dir (echo $work_dir | sed -e "s#^$current_dir##") + case \* + set -e work_dir + end + end + + if [ "$project_pwd" -o "$work_dir" ] + set -l bg_color $__bobthefish_dk_grey + set -l fg_color $__bobthefish_med_grey + if not [ -w "$PWD" ] + set bg_color $__bobthefish_med_red + set fg_color $__bobthefish_lt_red + end + + __bobthefish_start_segment $bg_color $fg_color + + # handle work_dir != project dir + if [ "$work_dir" ] + set -l work_parent (dirname $work_dir | sed -e 's#^/##') + if [ "$work_parent" ] + set_color --background $bg_color $fg_color + echo -n "$work_parent/" + end + set_color fff --bold + echo -n (basename $work_dir) + set_color --background $bg_color $fg_color + [ "$project_pwd" ] + and echo -n '/' end echo -ns $project_pwd ' ' + else + set project_pwd (echo $PWD | sed -e "s#^$current_dir##" -e 's#^/##') + if [ "$project_pwd" ] + set -l bg_color $__bobthefish_dk_grey + set -l fg_color $__bobthefish_med_grey + if not [ -w "$PWD" ] + set bg_color $__bobthefish_med_red + set fg_color $__bobthefish_lt_red + end + + __bobthefish_start_segment $bg_color $fg_color + + echo -ns $project_pwd ' ' + end end end