Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plugin][mercurial] Optimize in_hg and hg_get_branch_name #5613

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 46 additions & 10 deletions plugins/mercurial/mercurial.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,60 @@ alias hgca='hg commit --amend'
# list unresolved files (since hg does not list unmerged files in the status command)
alias hgun='hg resolve --list'

# Configuration

# Comment this line if you don't want to show if the branch is dirty or not.
# This can save a lot of time of very large repositories.
SHOW_DIRTY_BRANCH=1

function hg_get_dir() {
# Defines path as current directory
local current_dir=$PWD
# While current path is not root path
while [[ $current_dir != '/' ]]; do
if [[ -d "${current_dir}/.hg" ]]; then
echo "${current_dir}/.hg"
return
fi
# Defines path as parent directory and keeps looking for
current_dir="${current_dir:h}"
done
}

function in_hg() {
if [[ -d .hg ]] || $(hg summary > /dev/null 2>&1); then
if [[ $(hg_get_dir) != "" ]]; then
echo 1
fi
}

function hg_get_branch_id() {
local hg_id_tip=`hg log -T "{node|short}" -l 1 -b .`
local hg_id=`hg id -i -r .`

if [[ $hg_id != $hg_id_tip ]]; then
echo "@${hg_id}"
fi
}

function hg_get_branch_name() {
if [ $(in_hg) ]; then
echo $(hg branch)
local hg_dir=$(hg_get_dir)
if [[ $hg_dir != "" ]]; then
local hg_id=$(hg_get_branch_id)
if [[ -f "${hg_dir}/branch" ]]; then
echo $(<"${hg_dir}/branch")$hg_id
else
echo "default${hg_id}"
fi
fi
}

function hg_prompt_info {
if [ $(in_hg) ]; then
_DISPLAY=$(hg_get_branch_name)
echo "$ZSH_PROMPT_BASE_COLOR$ZSH_THEME_HG_PROMPT_PREFIX\
$ZSH_THEME_REPO_NAME_COLOR$_DISPLAY$ZSH_PROMPT_BASE_COLOR$ZSH_PROMPT_BASE_COLOR$(hg_dirty)$ZSH_THEME_HG_PROMPT_SUFFIX$ZSH_PROMPT_BASE_COLOR"
unset _DISPLAY
local branch=$(hg_get_branch_name)
if [[ $branch != "" ]]; then
if [[ $SHOW_DIRTY_BRANCH == 1 ]]; then
branch="${branch}$(hg_dirty)"
fi
echo "$ZSH_THEME_HG_PROMPT_PREFIX${branch}$ZSH_THEME_HG_PROMPT_SUFFIX"
fi
}

Expand All @@ -57,9 +93,9 @@ function hg_dirty {
}

function hgic() {
hg incoming "$@" | grep "changeset" | wc -l
hg incoming "$@" | grep "changeset" | wc -l
}

function hgoc() {
hg outgoing "$@" | grep "changeset" | wc -l
hg outgoing "$@" | grep "changeset" | wc -l
}