Merge pull request #1622 from NoahGorny/gitstatusd

Gitstatusd support take 2
pull/1630/head
Nils Winkler 2020-06-23 08:55:08 +02:00 committed by GitHub
commit 8b0c776b98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 148 additions and 27 deletions

View File

@ -43,3 +43,8 @@ For `aliases`, `plugins` and `completions`, the following rules are applied that
Having the order based on a numeric priority in a common directory allows for more flexibility. While in general, aliases are loaded first (since their default priority is 150), it's possible to load some aliases after the plugins, or some plugins after completions by setting the items' load priority. This is more flexible than a fixed type-based order or a strict alphabetical order based on name. Having the order based on a numeric priority in a common directory allows for more flexibility. While in general, aliases are loaded first (since their default priority is 150), it's possible to load some aliases after the plugins, or some plugins after completions by setting the items' load priority. This is more flexible than a fixed type-based order or a strict alphabetical order based on name.
These items are subject to change. When making changes to the internal functionality, this page needs to be updated as well. These items are subject to change. When making changes to the internal functionality, this page needs to be updated as well.
## Plugin Disable Callbacks
Plugins can define a function that will be called when the plugin is being disabled.
The callback name should be `{PLUGIN_NAME}_on_disable`, you can see `gitstatus` for usage example.

View File

@ -395,6 +395,13 @@ $ git config --global --add bash-it.hide-status 1
Setting this flag globally has the same effect as `SCM_CHECK=true`, but only for Git repos. Setting this flag globally has the same effect as `SCM_CHECK=true`, but only for Git repos.
### Speed up git status calculations
As an alternative to ignoring repo status entirely, you can try out the `gitstatus` plugin.
This plugin speeds up all `git status` calculations by up to 10x times!
**NOTE**: You will need to clone `gitstatus` repo from [here](https://github.com/romkatv/gitstatus).
### Pass function renamed to passgen ### Pass function renamed to passgen
The Bash-it `pass` function has been renamed to `passgen` in order to avoid a naming conflict with the [pass password manager](https://www.passwordstore.org/). The Bash-it `pass` function has been renamed to `passgen` in order to avoid a naming conflict with the [pass password manager](https://www.passwordstore.org/).

View File

@ -318,6 +318,17 @@ _bash-it-describe ()
printf '%s\n' "$ bash-it disable $file_type <$file_type name> [$file_type name]... -or- $ bash-it disable $file_type all" printf '%s\n' "$ bash-it disable $file_type <$file_type name> [$file_type name]... -or- $ bash-it disable $file_type all"
} }
_on-disable-callback()
{
_about 'Calls the disabled plugin destructor, if present'
_param '1: plugin name'
_example '$ _on-disable-callback gitstatus'
_group 'lib'
callback=$1_on_disable
_command_exists $callback && $callback
}
_disable-plugin () _disable-plugin ()
{ {
_about 'disables bash_it plugin' _about 'disables bash_it plugin'
@ -326,6 +337,7 @@ _disable-plugin ()
_group 'lib' _group 'lib'
_disable-thing "plugins" "plugin" $1 _disable-thing "plugins" "plugin" $1
_on-disable-callback $1
} }
_disable-alias () _disable-alias ()

View File

@ -0,0 +1,24 @@
cite about-plugin
about-plugin 'speeds up your life by using gitstatus for git status calculations. install from https://github.com/romkatv/gitstatus'
function gitstatus_on_disable() {
about 'Destructor of gitstatus plugin'
group 'gitstatus'
unset SCM_GIT_USE_GITSTATUS
_command_exists gitstatus_stop && gitstatus_stop
}
# No scm-check
[[ $SCM_CHECK == "true" ]] || return
# non-interactive shell
[[ $- == *i* ]] || return
: "${SCM_GIT_GITSTATUS_DIR:="$HOME/gitstatus"}"
if [[ -d ${SCM_GIT_GITSTATUS_DIR} ]]; then
source "${SCM_GIT_GITSTATUS_DIR}/gitstatus.plugin.sh"
# Start the actual gitstatus binary
gitstatus_stop && gitstatus_start -s -1 -u -1 -c -1 -d -1
export SCM_GIT_USE_GITSTATUS=true
fi

View File

@ -32,6 +32,10 @@ export TODO="t"
# Set this to false to turn off version control status checking within the prompt for all themes # Set this to false to turn off version control status checking within the prompt for all themes
export SCM_CHECK=true export SCM_CHECK=true
# Set to actual location of gitstatus directory if installed
#export SCM_GIT_GITSTATUS_DIR="$HOME/gitstatus"
# per default gitstatus uses 2 times as many threads as CPU cores, you can change this here if you must
#export GITSTATUS_NUM_THREADS=8
# Set Xterm/screen/Tmux title with only a short hostname. # Set Xterm/screen/Tmux title with only a short hostname.
# Uncomment this (or set SHORT_HOSTNAME to something else), # Uncomment this (or set SHORT_HOSTNAME to something else),

View File

@ -39,7 +39,12 @@ function local_teardown {
@test "search: git" { @test "search: git" {
run _bash-it-search 'git' --no-color run _bash-it-search 'git' --no-color
assert_line -n 0 ' aliases: git gitsvn ' assert_line -n 0 ' aliases: git gitsvn '
assert_line -n 1 ' plugins: autojump git git-subrepo jgitflow jump ' assert_line -n 1 -p ' plugins:'
for plugin in "autojump" "git" "gitstatus" "git-subrepo" "jgitflow" "jump"
do
echo $plugin
assert_line -n 1 -p $plugin
done
assert_line -n 2 ' completions: git git_flow git_flow_avh ' assert_line -n 2 ' completions: git git_flow git_flow_avh '
} }

View File

@ -34,6 +34,8 @@ SCM_GIT_SHOW_CURRENT_USER=${SCM_GIT_SHOW_CURRENT_USER:=false}
SCM_GIT_SHOW_MINIMAL_INFO=${SCM_GIT_SHOW_MINIMAL_INFO:=false} SCM_GIT_SHOW_MINIMAL_INFO=${SCM_GIT_SHOW_MINIMAL_INFO:=false}
SCM_GIT_SHOW_STASH_INFO=${SCM_GIT_SHOW_STASH_INFO:=true} SCM_GIT_SHOW_STASH_INFO=${SCM_GIT_SHOW_STASH_INFO:=true}
SCM_GIT_SHOW_COMMIT_COUNT=${SCM_GIT_SHOW_COMMIT_COUNT:=true} SCM_GIT_SHOW_COMMIT_COUNT=${SCM_GIT_SHOW_COMMIT_COUNT:=true}
SCM_GIT_USE_GITSTATUS=${SCM_GIT_USE_GITSTATUS:=false}
SCM_GIT_GITSTATUS_RAN=${SCM_GIT_GITSTATUS_RAN:=false}
SCM_GIT='git' SCM_GIT='git'
SCM_GIT_CHAR='±' SCM_GIT_CHAR='±'
@ -191,6 +193,12 @@ function git_prompt_minimal_info {
} }
function git_prompt_vars { function git_prompt_vars {
if ${SCM_GIT_USE_GITSTATUS} && _command_exists gitstatus_query && gitstatus_query && [[ "${VCS_STATUS_RESULT}" == "ok-sync" ]]; then # use faster gitstatus
SCM_GIT_GITSTATUS_RAN=true # use this in githelpers and below to choose gitstatus output
else
SCM_GIT_GITSTATUS_RAN=false
fi
if _git-branch &> /dev/null; then if _git-branch &> /dev/null; then
SCM_GIT_DETACHED="false" SCM_GIT_DETACHED="false"
SCM_BRANCH="${SCM_THEME_BRANCH_PREFIX}\$(_git-friendly-ref)$(_git-remote-info)" SCM_BRANCH="${SCM_THEME_BRANCH_PREFIX}\$(_git-friendly-ref)$(_git-remote-info)"
@ -206,7 +214,12 @@ function git_prompt_vars {
SCM_BRANCH="${detached_prefix}\$(_git-friendly-ref)" SCM_BRANCH="${detached_prefix}\$(_git-friendly-ref)"
fi fi
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
commits_behind=${VCS_STATUS_COMMITS_BEHIND}
commits_ahead=${VCS_STATUS_COMMITS_AHEAD}
else
IFS=$'\t' read -r commits_behind commits_ahead <<< "$(_git-upstream-behind-ahead)" IFS=$'\t' read -r commits_behind commits_ahead <<< "$(_git-upstream-behind-ahead)"
fi
if [[ "${commits_ahead}" -gt 0 ]]; then if [[ "${commits_ahead}" -gt 0 ]]; then
SCM_BRANCH+="${SCM_GIT_AHEAD_BEHIND_PREFIX_CHAR}${SCM_GIT_AHEAD_CHAR}" SCM_BRANCH+="${SCM_GIT_AHEAD_BEHIND_PREFIX_CHAR}${SCM_GIT_AHEAD_CHAR}"
[[ "${SCM_GIT_SHOW_COMMIT_COUNT}" = "true" ]] && SCM_BRANCH+="${commits_ahead}" [[ "${SCM_GIT_SHOW_COMMIT_COUNT}" = "true" ]] && SCM_BRANCH+="${commits_ahead}"
@ -218,13 +231,23 @@ function git_prompt_vars {
if [[ "${SCM_GIT_SHOW_STASH_INFO}" = "true" ]]; then if [[ "${SCM_GIT_SHOW_STASH_INFO}" = "true" ]]; then
local stash_count local stash_count
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
stash_count=${VCS_STATUS_STASHES}
else
stash_count="$(git stash list 2> /dev/null | wc -l | tr -d ' ')" stash_count="$(git stash list 2> /dev/null | wc -l | tr -d ' ')"
fi
[[ "${stash_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_GIT_STASH_CHAR_PREFIX}${stash_count}${SCM_GIT_STASH_CHAR_SUFFIX}" [[ "${stash_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_GIT_STASH_CHAR_PREFIX}${stash_count}${SCM_GIT_STASH_CHAR_SUFFIX}"
fi fi
SCM_STATE=${GIT_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN} SCM_STATE=${GIT_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
if ! _git-hide-status; then if ! _git-hide-status; then
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
untracked_count=${VCS_STATUS_NUM_UNTRACKED}
unstaged_count=${VCS_STATUS_NUM_UNSTAGED}
staged_count=${VCS_STATUS_NUM_STAGED}
else
IFS=$'\t' read -r untracked_count unstaged_count staged_count <<< "$(_git-status-counts)" IFS=$'\t' read -r untracked_count unstaged_count staged_count <<< "$(_git-status-counts)"
fi
if [[ "${untracked_count}" -gt 0 || "${unstaged_count}" -gt 0 || "${staged_count}" -gt 0 ]]; then if [[ "${untracked_count}" -gt 0 || "${unstaged_count}" -gt 0 || "${staged_count}" -gt 0 ]]; then
SCM_DIRTY=1 SCM_DIRTY=1
if [[ "${SCM_GIT_SHOW_DETAILS}" = "true" ]]; then if [[ "${SCM_GIT_SHOW_DETAILS}" = "true" ]]; then
@ -236,6 +259,7 @@ function git_prompt_vars {
fi fi
fi fi
# no if for gitstatus here, user extraction is not supported by it
[[ "${SCM_GIT_SHOW_CURRENT_USER}" == "true" ]] && SCM_BRANCH+="$(git_user_info)" [[ "${SCM_GIT_SHOW_CURRENT_USER}" == "true" ]] && SCM_BRANCH+="$(git_user_info)"
SCM_PREFIX=${GIT_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX} SCM_PREFIX=${GIT_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}

View File

@ -9,11 +9,19 @@ function _git-symbolic-ref {
# same commit. _git-branch is used to explicitly choose the checked-out # same commit. _git-branch is used to explicitly choose the checked-out
# branch. # branch.
function _git-branch { function _git-branch {
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
test -n "${VCS_STATUS_LOCAL_BRANCH}" && echo "${VCS_STATUS_LOCAL_BRANCH}" || return 1
else
git symbolic-ref -q --short HEAD 2> /dev/null || return 1 git symbolic-ref -q --short HEAD 2> /dev/null || return 1
fi
} }
function _git-tag { function _git-tag {
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
test -n "${VCS_STATUS_TAG}" && echo "${VCS_STATUS_TAG}"
else
git describe --tags --exact-match 2> /dev/null git describe --tags --exact-match 2> /dev/null
fi
} }
function _git-commit-description { function _git-commit-description {
@ -21,12 +29,20 @@ function _git-commit-description {
} }
function _git-short-sha { function _git-short-sha {
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
echo ${VCS_STATUS_COMMIT:0:7}
else
git rev-parse --short HEAD git rev-parse --short HEAD
fi
} }
# Try the checked-out branch first to avoid collision with branches pointing to the same ref. # Try the checked-out branch first to avoid collision with branches pointing to the same ref.
function _git-friendly-ref { function _git-friendly-ref {
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
_git-branch || _git-tag || _git-short-sha # there is no tag based describe output in gitstatus
else
_git-branch || _git-tag || _git-commit-description || _git-short-sha _git-branch || _git-tag || _git-commit-description || _git-short-sha
fi
} }
function _git-num-remotes { function _git-num-remotes {
@ -101,13 +117,36 @@ function _git-status-counts {
} }
function _git-remote-info { function _git-remote-info {
[[ "$(_git-upstream)" == "" ]] && return || true
[[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && local same_branch_name=true || true # prompt handling only, reimplement because patching the routine below gets ugly
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then
[[ "${VCS_STATUS_REMOTE_NAME}" == "" ]] && return
[[ "${VCS_STATUS_LOCAL_BRANCH}" == "${VCS_STATUS_REMOTE_BRANCH}" ]] && local same_branch_name=true
local same_branch_name=
[[ "${VCS_STATUS_LOCAL_BRANCH}" == "${VCS_STATUS_REMOTE_BRANCH}" ]] && same_branch_name=true
# no multiple remote support in gitstatusd
if [[ "${SCM_GIT_SHOW_REMOTE_INFO}" = "true" || "${SCM_GIT_SHOW_REMOTE_INFO}" = "auto" ]]; then
if [[ "${same_branch_name}" != "true" ]]; then
remote_info="${VCS_STATUS_REMOTE_NAME}/${VCS_STATUS_REMOTE_BRANCH}"
else
remote_info="${VCS_STATUS_REMOTE_NAME}"
fi
elif [[ ${same_branch_name} != "true" ]]; then
remote_info="${VCS_STATUS_REMOTE_BRANCH}"
fi
if [[ -n "${remote_info}" ]];then
# no support for gone remote branches in gitstatusd
local branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX}"
echo "${branch_prefix}${remote_info}"
fi
else
[[ "$(_git-upstream)" == "" ]] && return
[[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && local same_branch_name=true
local same_branch_name= local same_branch_name=
[[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && same_branch_name=true [[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && same_branch_name=true
if ([[ "${SCM_GIT_SHOW_REMOTE_INFO}" = "auto" ]] && [[ "$(_git-num-remotes)" -ge 2 ]]) || if [[ ("${SCM_GIT_SHOW_REMOTE_INFO}" = "auto" && "$(_git-num-remotes)" -ge 2) ||
[[ "${SCM_GIT_SHOW_REMOTE_INFO}" = "true" ]]; then "${SCM_GIT_SHOW_REMOTE_INFO}" = "true" ]]; then
if [[ "${same_branch_name}" != "true" ]]; then if [[ "${same_branch_name}" != "true" ]]; then
remote_info="\$(_git-upstream)" remote_info="\$(_git-upstream)"
else else
@ -125,6 +164,7 @@ function _git-remote-info {
fi fi
echo "${branch_prefix}${remote_info}" echo "${branch_prefix}${remote_info}"
fi fi
fi
} }
# Unused by bash-it, present for API compatibility # Unused by bash-it, present for API compatibility