2015-10-24 00:05:15 +02:00
|
|
|
# vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8
|
|
|
|
################################################################
|
|
|
|
# vcs
|
|
|
|
# This file holds supplemental VCS functions
|
|
|
|
# for the powerlevel9k-ZSH-theme
|
|
|
|
# https://github.com/bhilburn/powerlevel9k
|
|
|
|
################################################################
|
|
|
|
|
2018-11-03 04:21:47 +02:00
|
|
|
set_default POWERLEVEL9K_VCS_SHOW_SUBMODULE_DIRTY false
|
2015-10-24 00:05:15 +02:00
|
|
|
function +vi-git-untracked() {
|
2018-11-18 00:13:25 +02:00
|
|
|
[[ -z "${vcs_comm[gitdir]}" || "${vcs_comm[gitdir]}" == "." ]] && return
|
|
|
|
|
|
|
|
# get the root for the current repo or submodule
|
|
|
|
local repoTopLevel="$(command git rev-parse --show-toplevel 2> /dev/null)"
|
|
|
|
# dump out if we're outside a git repository (which includes being in the .git folder)
|
|
|
|
[[ $? != 0 || -z $repoTopLevel ]] && return
|
|
|
|
|
2019-02-06 11:51:41 +02:00
|
|
|
local untrackedFiles=$(command git ls-files --others --exclude-standard "${repoTopLevel}" 2> /dev/null)
|
2018-11-18 00:13:25 +02:00
|
|
|
|
|
|
|
if [[ -z $untrackedFiles && "$POWERLEVEL9K_VCS_SHOW_SUBMODULE_DIRTY" == "true" ]]; then
|
2019-02-06 11:51:41 +02:00
|
|
|
untrackedFiles+=$(command git submodule foreach --quiet --recursive 'command git ls-files --others --exclude-standard' 2> /dev/null)
|
2018-11-18 00:13:25 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
[[ -z $untrackedFiles ]] && return
|
|
|
|
|
|
|
|
hook_com[unstaged]+=" $(print_icon 'VCS_UNTRACKED_ICON')"
|
|
|
|
VCS_WORKDIR_HALF_DIRTY=true
|
2015-10-24 00:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function +vi-git-aheadbehind() {
|
2018-11-03 04:08:32 +02:00
|
|
|
local ahead behind
|
2015-10-24 00:05:15 +02:00
|
|
|
local -a gitstatus
|
|
|
|
|
|
|
|
# for git prior to 1.7
|
2018-11-03 04:08:32 +02:00
|
|
|
# ahead=$(command git rev-list origin/${hook_com[branch]}..HEAD | wc -l)
|
|
|
|
ahead=$(command git rev-list --count "${hook_com[branch]}"@{upstream}..HEAD 2>/dev/null)
|
2016-02-10 22:57:06 +02:00
|
|
|
(( ahead )) && gitstatus+=( " $(print_icon 'VCS_OUTGOING_CHANGES_ICON')${ahead// /}" )
|
2015-10-24 00:05:15 +02:00
|
|
|
|
|
|
|
# for git prior to 1.7
|
2018-11-03 04:08:32 +02:00
|
|
|
# behind=$(command git rev-list HEAD..origin/${hook_com[branch]} | wc -l)
|
|
|
|
behind=$(command git rev-list --count HEAD.."${hook_com[branch]}"@{upstream} 2>/dev/null)
|
2016-02-10 22:57:06 +02:00
|
|
|
(( behind )) && gitstatus+=( " $(print_icon 'VCS_INCOMING_CHANGES_ICON')${behind// /}" )
|
2015-10-24 00:05:15 +02:00
|
|
|
|
|
|
|
hook_com[misc]+=${(j::)gitstatus}
|
|
|
|
}
|
|
|
|
|
|
|
|
function +vi-git-remotebranch() {
|
2018-11-03 04:10:53 +02:00
|
|
|
local remote
|
|
|
|
local branch_name="${hook_com[branch]}"
|
2015-10-24 00:05:15 +02:00
|
|
|
|
|
|
|
# Are we on a remote-tracking branch?
|
2018-04-11 17:07:24 +02:00
|
|
|
remote=${$(command git rev-parse --verify HEAD@{upstream} --symbolic-full-name 2>/dev/null)/refs\/(remotes|heads)\/}
|
2015-10-24 00:05:15 +02:00
|
|
|
|
2018-04-12 12:39:39 +02:00
|
|
|
if [[ -n "$POWERLEVEL9K_VCS_SHORTEN_LENGTH" ]] && [[ -n "$POWERLEVEL9K_VCS_SHORTEN_MIN_LENGTH" ]]; then
|
2018-11-06 02:11:31 +02:00
|
|
|
if [ ${#hook_com[branch]} -gt ${POWERLEVEL9K_VCS_SHORTEN_MIN_LENGTH} ] && [ ${#hook_com[branch]} -gt ${POWERLEVEL9K_VCS_SHORTEN_LENGTH} ]; then
|
2018-04-12 12:39:39 +02:00
|
|
|
case "$POWERLEVEL9K_VCS_SHORTEN_STRATEGY" in
|
|
|
|
truncate_middle)
|
2018-11-06 02:11:31 +02:00
|
|
|
hook_com[branch]="${branch_name:0:$POWERLEVEL9K_VCS_SHORTEN_LENGTH}${POWERLEVEL9K_VCS_SHORTEN_DELIMITER}${branch_name: -$POWERLEVEL9K_VCS_SHORTEN_LENGTH}"
|
2018-04-12 12:39:39 +02:00
|
|
|
;;
|
|
|
|
truncate_from_right)
|
2018-11-06 02:11:31 +02:00
|
|
|
hook_com[branch]="${branch_name:0:$POWERLEVEL9K_VCS_SHORTEN_LENGTH}${POWERLEVEL9K_VCS_SHORTEN_DELIMITER}"
|
2018-04-12 12:39:39 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2016-02-10 22:57:06 +02:00
|
|
|
hook_com[branch]="$(print_icon 'VCS_BRANCH_ICON')${hook_com[branch]}"
|
2015-10-24 00:05:15 +02:00
|
|
|
# Always show the remote
|
|
|
|
#if [[ -n ${remote} ]] ; then
|
|
|
|
# Only show the remote if it differs from the local
|
|
|
|
if [[ -n ${remote} ]] && [[ "${remote#*/}" != "${branch_name}" ]] ; then
|
2016-02-10 22:57:06 +02:00
|
|
|
hook_com[branch]+="$(print_icon 'VCS_REMOTE_BRANCH_ICON')${remote// /}"
|
2015-10-24 00:05:15 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-07-13 02:28:01 +02:00
|
|
|
set_default POWERLEVEL9K_VCS_HIDE_TAGS false
|
2015-10-24 00:05:15 +02:00
|
|
|
function +vi-git-tagname() {
|
2016-09-26 19:54:43 +02:00
|
|
|
if [[ "$POWERLEVEL9K_VCS_HIDE_TAGS" == "false" ]]; then
|
2016-07-13 02:28:01 +02:00
|
|
|
# If we are on a tag, append the tagname to the current branch string.
|
|
|
|
local tag
|
2018-04-11 17:07:24 +02:00
|
|
|
tag=$(command git describe --tags --exact-match HEAD 2>/dev/null)
|
2015-10-24 00:05:15 +02:00
|
|
|
|
2016-07-13 02:28:01 +02:00
|
|
|
if [[ -n "${tag}" ]] ; then
|
|
|
|
# There is a tag that points to our current commit. Need to determine if we
|
|
|
|
# are also on a branch, or are in a DETACHED_HEAD state.
|
2018-04-11 17:07:24 +02:00
|
|
|
if [[ -z $(command git symbolic-ref HEAD 2>/dev/null) ]]; then
|
2016-07-13 02:28:01 +02:00
|
|
|
# DETACHED_HEAD state. We want to append the tag name to the commit hash
|
|
|
|
# and print it. Unfortunately, `vcs_info` blows away the hash when a tag
|
|
|
|
# exists, so we have to manually retrieve it and clobber the branch
|
|
|
|
# string.
|
|
|
|
local revision
|
2018-04-11 17:07:24 +02:00
|
|
|
revision=$(command git rev-list -n 1 --abbrev-commit --abbrev=${POWERLEVEL9K_VCS_INTERNAL_HASH_LENGTH} HEAD)
|
2016-07-13 02:28:01 +02:00
|
|
|
hook_com[branch]="$(print_icon 'VCS_BRANCH_ICON')${revision} $(print_icon 'VCS_TAG_ICON')${tag}"
|
|
|
|
else
|
|
|
|
# We are on both a tag and a branch; print both by appending the tag name.
|
|
|
|
hook_com[branch]+=" $(print_icon 'VCS_TAG_ICON')${tag}"
|
|
|
|
fi
|
|
|
|
fi
|
2016-04-11 21:37:28 +02:00
|
|
|
fi
|
2015-10-24 00:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Show count of stashed changes
|
|
|
|
# Port from https://github.com/whiteinge/dotfiles/blob/5dfd08d30f7f2749cfc60bc55564c6ea239624d9/.zsh_shouse_prompt#L268
|
|
|
|
function +vi-git-stash() {
|
2018-11-12 00:08:40 +02:00
|
|
|
if [[ -s "${vcs_comm[gitdir]}/logs/refs/stash" ]] ; then
|
|
|
|
local -a stashes=( "${(@f)"$(<${vcs_comm[gitdir]}/logs/refs/stash)"}" )
|
|
|
|
hook_com[misc]+=" $(print_icon 'VCS_STASH_ICON')${#stashes}"
|
2015-10-24 00:05:15 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function +vi-hg-bookmarks() {
|
|
|
|
if [[ -n "${hgbmarks[@]}" ]]; then
|
2016-02-10 22:57:06 +02:00
|
|
|
hook_com[hg-bookmark-string]=" $(print_icon 'VCS_BOOKMARK_ICON')${hgbmarks[@]}"
|
2015-10-24 00:05:15 +02:00
|
|
|
|
|
|
|
# To signal that we want to use the sting we just generated, set the special
|
|
|
|
# variable `ret' to something other than the default zero:
|
|
|
|
ret=1
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function +vi-vcs-detect-changes() {
|
2015-12-18 01:47:37 +02:00
|
|
|
if [[ "${hook_com[vcs]}" == "git" ]]; then
|
2016-12-15 14:08:21 +02:00
|
|
|
|
2018-04-11 17:07:24 +02:00
|
|
|
local remote=$(command git ls-remote --get-url 2> /dev/null)
|
2016-12-15 14:08:21 +02:00
|
|
|
if [[ "$remote" =~ "github" ]] then
|
|
|
|
vcs_visual_identifier='VCS_GIT_GITHUB_ICON'
|
|
|
|
elif [[ "$remote" =~ "bitbucket" ]] then
|
|
|
|
vcs_visual_identifier='VCS_GIT_BITBUCKET_ICON'
|
2017-11-30 14:03:54 +02:00
|
|
|
elif [[ "$remote" =~ "stash" ]] then
|
|
|
|
vcs_visual_identifier='VCS_GIT_BITBUCKET_ICON'
|
2016-12-15 14:08:21 +02:00
|
|
|
elif [[ "$remote" =~ "gitlab" ]] then
|
|
|
|
vcs_visual_identifier='VCS_GIT_GITLAB_ICON'
|
|
|
|
else
|
|
|
|
vcs_visual_identifier='VCS_GIT_ICON'
|
|
|
|
fi
|
|
|
|
|
2015-12-18 01:47:37 +02:00
|
|
|
elif [[ "${hook_com[vcs]}" == "hg" ]]; then
|
|
|
|
vcs_visual_identifier='VCS_HG_ICON'
|
2016-08-31 02:29:36 +02:00
|
|
|
elif [[ "${hook_com[vcs]}" == "svn" ]]; then
|
|
|
|
vcs_visual_identifier='VCS_SVN_ICON'
|
2015-12-18 01:47:37 +02:00
|
|
|
fi
|
|
|
|
|
2015-10-24 00:05:15 +02:00
|
|
|
if [[ -n "${hook_com[staged]}" ]] || [[ -n "${hook_com[unstaged]}" ]]; then
|
|
|
|
VCS_WORKDIR_DIRTY=true
|
|
|
|
else
|
|
|
|
VCS_WORKDIR_DIRTY=false
|
|
|
|
fi
|
|
|
|
}
|
2016-08-25 23:13:15 +02:00
|
|
|
|
|
|
|
function +vi-svn-detect-changes() {
|
2016-10-19 15:35:23 +02:00
|
|
|
local svn_status="$(svn status)"
|
2017-11-27 08:15:44 +02:00
|
|
|
if [[ -n "$(echo "$svn_status" | \grep \^\?)" ]]; then
|
2016-08-31 02:29:36 +02:00
|
|
|
hook_com[unstaged]+=" $(print_icon 'VCS_UNTRACKED_ICON')"
|
2016-08-25 23:13:15 +02:00
|
|
|
VCS_WORKDIR_HALF_DIRTY=true
|
2016-08-25 23:51:16 +02:00
|
|
|
fi
|
2017-11-27 08:15:44 +02:00
|
|
|
if [[ -n "$(echo "$svn_status" | \grep \^\M)" ]]; then
|
2016-08-31 02:29:36 +02:00
|
|
|
hook_com[unstaged]+=" $(print_icon 'VCS_UNSTAGED_ICON')"
|
|
|
|
VCS_WORKDIR_DIRTY=true
|
|
|
|
fi
|
2017-11-27 08:15:44 +02:00
|
|
|
if [[ -n "$(echo "$svn_status" | \grep \^\A)" ]]; then
|
2016-08-31 02:29:36 +02:00
|
|
|
hook_com[staged]+=" $(print_icon 'VCS_STAGED_ICON')"
|
2016-08-25 23:31:00 +02:00
|
|
|
VCS_WORKDIR_DIRTY=true
|
2016-08-25 23:13:15 +02:00
|
|
|
fi
|
|
|
|
}
|