lib/githelpers: `shfmt` && `shellcheck`

My apologies to future `git blame` hunters ♥
pull/2038/head
John D Pell 2022-03-03 22:54:08 -08:00
parent fbc5d0a5af
commit 6bacd5fb1c
2 changed files with 116 additions and 110 deletions

View File

@ -158,6 +158,7 @@ themes/candy
themes/command_duration.theme.bash themes/command_duration.theme.bash
themes/easy themes/easy
themes/essential themes/essential
themes/githelpers.theme.bash
themes/modern themes/modern
themes/norbu themes/norbu
themes/p4helpers.theme.bash themes/p4helpers.theme.bash

View File

@ -1,99 +1,106 @@
#!/usr/bin/env bash # shellcheck shell=bash
function _git-symbolic-ref { function _git-symbolic-ref() {
git symbolic-ref -q HEAD 2> /dev/null git symbolic-ref -q HEAD 2> /dev/null
} }
# When on a branch, this is often the same as _git-commit-description, # When on a branch, this is often the same as _git-commit-description,
# but this can be different when two branches are pointing to the # but this can be different when two branches are pointing to the
# 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 if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then
test -n "${VCS_STATUS_LOCAL_BRANCH}" && echo "${VCS_STATUS_LOCAL_BRANCH}" || return 1 if [[ -n "${VCS_STATUS_LOCAL_BRANCH:-}" ]]; then
else echo "${VCS_STATUS_LOCAL_BRANCH}"
git symbolic-ref -q --short HEAD 2> /dev/null || return 1 else
fi return 1
fi
else
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 if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then
test -n "${VCS_STATUS_TAG}" && echo "${VCS_STATUS_TAG}" if [[ -n "${VCS_STATUS_TAG:-}" ]]; then
else echo "${VCS_STATUS_TAG}"
git describe --tags --exact-match 2> /dev/null fi
fi else
git describe --tags --exact-match 2> /dev/null
fi
} }
function _git-commit-description { function _git-commit-description() {
git describe --contains --all 2> /dev/null git describe --contains --all 2> /dev/null
} }
function _git-short-sha { function _git-short-sha() {
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then
echo ${VCS_STATUS_COMMIT:0:7} echo "${VCS_STATUS_COMMIT:0:7}"
else else
git rev-parse --short HEAD git rev-parse --short HEAD
fi 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 if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then
_git-branch || _git-tag || _git-short-sha # there is no tag based describe output in gitstatus _git-branch || _git-tag || _git-short-sha # there is no tag based describe output in gitstatus
else else
_git-branch || _git-tag || _git-commit-description || _git-short-sha _git-branch || _git-tag || _git-commit-description || _git-short-sha
fi fi
} }
function _git-num-remotes { function _git-num-remotes() {
git remote | wc -l git remote | wc -l
} }
function _git-upstream { function _git-upstream() {
local ref local ref
ref="$(_git-symbolic-ref)" || return 1 ref="$(_git-symbolic-ref)" || return 1
git for-each-ref --format="%(upstream:short)" "${ref}" git for-each-ref --format="%(upstream:short)" "${ref}"
} }
function _git-upstream-remote { function _git-upstream-remote() {
local upstream local upstream branch
upstream="$(_git-upstream)" || return 1 upstream="$(_git-upstream)" || return 1
local branch branch="$(_git-upstream-branch)" || return 1
branch="$(_git-upstream-branch)" || return 1 echo "${upstream%"/${branch}"}"
echo "${upstream%"/${branch}"}"
} }
function _git-upstream-branch { function _git-upstream-branch() {
local ref local ref
ref="$(_git-symbolic-ref)" || return 1 ref="$(_git-symbolic-ref)" || return 1
# git versions < 2.13.0 do not support "strip" for upstream format # git versions < 2.13.0 do not support "strip" for upstream format
# regex replacement gives the wrong result for any remotes with slashes in the name, # regex replacement gives the wrong result for any remotes with slashes in the name,
# so only use when the strip format fails. # so only use when the strip format fails.
git for-each-ref --format="%(upstream:strip=3)" "${ref}" 2> /dev/null || git for-each-ref --format="%(upstream)" "${ref}" | sed -e "s/.*\/.*\/.*\///" git for-each-ref --format="%(upstream:strip=3)" "${ref}" 2> /dev/null || git for-each-ref --format="%(upstream)" "${ref}" | sed -e "s/.*\/.*\/.*\///"
} }
function _git-upstream-behind-ahead { function _git-upstream-behind-ahead() {
git rev-list --left-right --count "$(_git-upstream)...HEAD" 2> /dev/null git rev-list --left-right --count "$(_git-upstream)...HEAD" 2> /dev/null
} }
function _git-upstream-branch-gone { function _git-upstream-branch-gone() {
[[ "$(git status -s -b | sed -e 's/.* //')" == "[gone]" ]] [[ "$(git status -s -b | sed -e 's/.* //')" == "[gone]" ]]
} }
function _git-hide-status { function _git-hide-status() {
[[ "$(git config --get bash-it.hide-status)" == "1" ]] [[ "$(git config --get bash-it.hide-status)" == "1" ]]
} }
function _git-status { function _git-status() {
local git_status_flags= local git_status_flags=
[[ "${SCM_GIT_IGNORE_UNTRACKED}" = "true" ]] && git_status_flags='-uno' || true if [[ "${SCM_GIT_IGNORE_UNTRACKED:-}" == "true" ]]; then
git status --porcelain ${git_status_flags} 2> /dev/null git_status_flags='-uno'
fi
git status --porcelain "${git_status_flags:---}" 2> /dev/null
} }
function _git-status-counts { function _git-status-counts() {
_git-status | awk ' _git-status | awk '
BEGIN { BEGIN {
untracked=0; untracked=0;
unstaged=0; unstaged=0;
@ -116,60 +123,58 @@ function _git-status-counts {
}' }'
} }
function _git-remote-info { function _git-remote-info() {
local same_branch_name="" branch_prefix
# 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?}" ]] && 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
branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX:-}"
echo "${branch_prefix}${remote_info:-}"
fi
else
[[ "$(_git-upstream)" == "" ]] && return
# prompt handling only, reimplement because patching the routine below gets ugly [[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && same_branch_name=true
if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then if [[ ("${SCM_GIT_SHOW_REMOTE_INFO}" == "auto" && "$(_git-num-remotes)" -ge 2) ||
[[ "${VCS_STATUS_REMOTE_NAME}" == "" ]] && return "${SCM_GIT_SHOW_REMOTE_INFO}" == "true" ]]; then
[[ "${VCS_STATUS_LOCAL_BRANCH}" == "${VCS_STATUS_REMOTE_BRANCH}" ]] && local same_branch_name=true if [[ ${same_branch_name:-} != "true" ]]; then
local same_branch_name= # shellcheck disable=SC2016
[[ "${VCS_STATUS_LOCAL_BRANCH}" == "${VCS_STATUS_REMOTE_BRANCH}" ]] && same_branch_name=true remote_info='$(_git-upstream)'
# no multiple remote support in gitstatusd else
if [[ "${SCM_GIT_SHOW_REMOTE_INFO}" = "true" || "${SCM_GIT_SHOW_REMOTE_INFO}" = "auto" ]]; then remote_info="$(_git-upstream-remote)"
if [[ "${same_branch_name}" != "true" ]]; then fi
remote_info="${VCS_STATUS_REMOTE_NAME}/${VCS_STATUS_REMOTE_BRANCH}" elif [[ ${same_branch_name:-} != "true" ]]; then
else # shellcheck disable=SC2016
remote_info="${VCS_STATUS_REMOTE_NAME}" remote_info='$(_git-upstream-branch)'
fi fi
elif [[ ${same_branch_name} != "true" ]]; then if [[ -n "${remote_info:-}" ]]; then
remote_info="${VCS_STATUS_REMOTE_BRANCH}" local branch_prefix
fi if _git-upstream-branch-gone; then
if [[ -n "${remote_info:-}" ]];then branch_prefix="${SCM_THEME_BRANCH_GONE_PREFIX:-}"
# no support for gone remote branches in gitstatusd else
local branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX}" branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX:-}"
echo "${branch_prefix}${remote_info}" fi
fi echo "${branch_prefix}${remote_info:-}"
else fi
[[ "$(_git-upstream)" == "" ]] && return fi
[[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && local same_branch_name=true
local same_branch_name=
[[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && same_branch_name=true
if [[ ("${SCM_GIT_SHOW_REMOTE_INFO}" = "auto" && "$(_git-num-remotes)" -ge 2) ||
"${SCM_GIT_SHOW_REMOTE_INFO}" = "true" ]]; then
if [[ "${same_branch_name}" != "true" ]]; then
remote_info="\$(_git-upstream)"
else
remote_info="$(_git-upstream-remote)"
fi
elif [[ ${same_branch_name} != "true" ]]; then
remote_info="\$(_git-upstream-branch)"
fi
if [[ -n "${remote_info:-}" ]];then
local branch_prefix
if _git-upstream-branch-gone; then
branch_prefix="${SCM_THEME_BRANCH_GONE_PREFIX}"
else
branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX}"
fi
echo "${branch_prefix}${remote_info}"
fi
fi
} }
# Unused by bash-it, present for API compatibility # Unused by bash-it, present for API compatibility
function git_status_summary { function git_status_summary() {
awk ' awk '
BEGIN { BEGIN {
untracked=0; untracked=0;
unstaged=0; unstaged=0;