You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
365 lines
9.4 KiB
365 lines
9.4 KiB
13 years ago
|
#
|
||
|
# Displays Git repository information.
|
||
|
#
|
||
|
# Authors:
|
||
|
# Sorin Ionescu <sorin.ionescu@gmail.com>
|
||
|
#
|
||
|
|
||
13 years ago
|
# Gets the path to the Git directory.
|
||
13 years ago
|
function _git-dir {
|
||
13 years ago
|
local git_dir="${$(git rev-parse --git-dir):A}"
|
||
13 years ago
|
|
||
13 years ago
|
if [[ -n "$git_dir" ]]; then
|
||
13 years ago
|
print "$git_dir"
|
||
|
return 0
|
||
|
fi
|
||
|
|
||
|
return 1
|
||
|
}
|
||
|
|
||
13 years ago
|
# Gets the Git special action (am, merge, rebase, etc.).
|
||
|
# Borrowed from vcs_info and edited.
|
||
13 years ago
|
function _git-action {
|
||
13 years ago
|
local action=''
|
||
|
local action_dir
|
||
13 years ago
|
local git_dir="$(_git-dir)"
|
||
13 years ago
|
|
||
|
for action_dir in \
|
||
|
"${git_dir}/rebase-apply" \
|
||
|
"${git_dir}/rebase" \
|
||
|
"${git_dir}/../.dotest"; do
|
||
|
if [[ -d "$action_dir" ]] ; then
|
||
|
if [[ -f "${action_dir}/rebasing" ]] ; then
|
||
|
action='rebase'
|
||
|
elif [[ -f "${action_dir}/applying" ]] ; then
|
||
|
action='am'
|
||
|
else
|
||
|
action='am/rebase'
|
||
|
fi
|
||
13 years ago
|
print "$action"
|
||
13 years ago
|
return 0
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
for action_dir in \
|
||
|
"${git_dir}/rebase-merge/interactive" \
|
||
|
"${git_dir}/.dotest-merge/interactive"; do
|
||
|
if [[ -f "$action_dir" ]]; then
|
||
13 years ago
|
print 'rebase-i'
|
||
13 years ago
|
return 0
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
for action_dir in \
|
||
|
"${git_dir}/rebase-merge" \
|
||
|
"${git_dir}/.dotest-merge"; do
|
||
|
if [[ -d "$action_dir" ]]; then
|
||
13 years ago
|
print 'rebase-m'
|
||
13 years ago
|
return 0
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
if [[ -f "${git_dir}/MERGE_HEAD" ]]; then
|
||
13 years ago
|
print 'merge'
|
||
13 years ago
|
return 0
|
||
|
fi
|
||
|
|
||
|
if [[ -f "${git_dir}/CHERRY_PICK_HEAD" ]]; then
|
||
13 years ago
|
print 'cherry-pick'
|
||
13 years ago
|
return 0
|
||
|
fi
|
||
|
|
||
|
if [[ -f "${git_dir}/BISECT_LOG" ]]; then
|
||
13 years ago
|
print 'bisect'
|
||
13 years ago
|
return 0
|
||
|
fi
|
||
|
|
||
|
return 1
|
||
|
}
|
||
|
|
||
|
# Turns off git-info for the current repository.
|
||
13 years ago
|
function _git-info-abort {
|
||
13 years ago
|
if ! is-true "$_git_info_executing"; then
|
||
13 years ago
|
return 1
|
||
|
fi
|
||
|
|
||
13 years ago
|
cat >&2 <<EOF
|
||
13 years ago
|
|
||
|
|
||
|
Gathering status for certain repositories is time intensive.
|
||
|
By pressing CTRL + C, you have turned off prompt Git status
|
||
|
for this repository.
|
||
|
|
||
|
To revert, execute:
|
||
|
git-info on
|
||
|
|
||
13 years ago
|
EOF
|
||
13 years ago
|
|
||
|
unset _git_info_executing
|
||
|
git config --bool prompt.showinfo false
|
||
|
git-info
|
||
|
return 0
|
||
|
}
|
||
|
add-zsh-trap INT _git-info-abort
|
||
|
|
||
|
# Gets the Git status information.
|
||
13 years ago
|
function git-info {
|
||
13 years ago
|
# Extended globbing is needed to parse repository status.
|
||
13 years ago
|
setopt LOCAL_OPTIONS
|
||
|
setopt EXTENDED_GLOB
|
||
13 years ago
|
|
||
|
local action
|
||
|
local action_format
|
||
|
local action_formatted
|
||
|
local added=0
|
||
|
local added_format
|
||
|
local added_formatted
|
||
|
local ahead
|
||
13 years ago
|
local ahead_and_behind
|
||
|
local ahead_and_behind_cmd
|
||
13 years ago
|
local ahead_format
|
||
|
local ahead_formatted
|
||
|
local ahead_or_behind
|
||
|
local behind
|
||
|
local behind_format
|
||
|
local behind_formatted
|
||
|
local branch
|
||
|
local branch_format
|
||
|
local branch_formatted
|
||
13 years ago
|
local branch_info
|
||
13 years ago
|
local commit
|
||
|
local commit_format
|
||
13 years ago
|
local commit_formatted
|
||
13 years ago
|
local deleted=0
|
||
|
local deleted_format
|
||
|
local deleted_formatted
|
||
13 years ago
|
local dirty=0
|
||
|
local dirty_format
|
||
13 years ago
|
local dirty_formatted
|
||
13 years ago
|
local ignore_submodule
|
||
|
local ignore_submodule_when
|
||
13 years ago
|
local line_number=0
|
||
|
local modified=0
|
||
|
local modified_format
|
||
|
local modified_formatted
|
||
13 years ago
|
local position
|
||
|
local position_format
|
||
|
local position_formatted
|
||
13 years ago
|
local prompt_format
|
||
13 years ago
|
local remote
|
||
13 years ago
|
local remote_cmd
|
||
13 years ago
|
local remote_format
|
||
|
local remote_formatted
|
||
|
local renamed=0
|
||
|
local renamed_format
|
||
|
local renamed_formatted
|
||
13 years ago
|
local rprompt_format
|
||
13 years ago
|
local stashed=0
|
||
|
local stashed_format
|
||
|
local stashed_formatted
|
||
13 years ago
|
local status_cmd
|
||
13 years ago
|
local unmerged=0
|
||
|
local unmerged_format
|
||
|
local unmerged_formatted
|
||
|
local untracked=0
|
||
|
local untracked_format
|
||
|
local untracked_formatted
|
||
|
local -A git_info_vars
|
||
13 years ago
|
local git_info_var
|
||
13 years ago
|
|
||
13 years ago
|
# Clean up previous git-info.
|
||
13 years ago
|
unset git_prompt_info
|
||
|
unset git_rprompt_info
|
||
|
|
||
|
# Return if not inside a Git repository work tree.
|
||
13 years ago
|
if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then
|
||
13 years ago
|
return 1
|
||
|
fi
|
||
|
|
||
|
if (( $# > 0 )); then
|
||
|
if [[ "$1" == [Oo][Nn] ]]; then
|
||
|
git config --bool prompt.showinfo true
|
||
|
elif [[ "$1" == [Oo][Ff][Ff] ]]; then
|
||
|
git config --bool prompt.showinfo false
|
||
|
else
|
||
13 years ago
|
print "usage: $0 [ on | off ]" >&2
|
||
13 years ago
|
fi
|
||
|
return 0
|
||
|
fi
|
||
|
|
||
|
# Return if git-info is disabled.
|
||
13 years ago
|
if ! is-true "${$(git config --bool prompt.showinfo):-true}"; then
|
||
13 years ago
|
return 1
|
||
|
fi
|
||
|
|
||
|
# Used to abort and turn git-info off on SIGINT.
|
||
|
_git_info_executing=true
|
||
|
|
||
13 years ago
|
# Use porcelain status for easy parsing.
|
||
|
status_cmd='git status --porcelain'
|
||
|
|
||
|
# Gets the remote name.
|
||
|
remote_cmd='git rev-parse --symbolic-full-name --verify HEAD@{upstream}'
|
||
|
|
||
|
# Gets the commit difference counts between local and remote.
|
||
|
ahead_and_behind_cmd='git rev-list --count --left-right HEAD...@{upstream}'
|
||
13 years ago
|
|
||
|
# Ignore submodule status.
|
||
13 years ago
|
zstyle -b \
|
||
13 years ago
|
':omz:module:git:prompt:ignore' submodule 'ignore_submodule'
|
||
13 years ago
|
zstyle -s \
|
||
13 years ago
|
':omz:module:git:prompt:ignore:submodule' when 'ignore_submodule_when'
|
||
13 years ago
|
if is-true "$ignore_submodule"; then
|
||
|
status_cmd+=" --ignore-submodules=${ignore_submodule_when:-all}"
|
||
|
fi
|
||
|
|
||
13 years ago
|
# Format commit.
|
||
13 years ago
|
commit="$(git rev-parse HEAD 2> /dev/null)"
|
||
13 years ago
|
if [[ -n "$commit" ]]; then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' commit 'commit_format'
|
||
13 years ago
|
zformat -f commit_formatted "$commit_format" "c:$commit"
|
||
|
fi
|
||
13 years ago
|
|
||
13 years ago
|
# Format stashed.
|
||
13 years ago
|
if [[ -f "$(_git-dir)/refs/stash" ]]; then
|
||
13 years ago
|
stashed="$(git stash list 2> /dev/null | wc -l)"
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' stashed 'stashed_format'
|
||
13 years ago
|
zformat -f stashed_formatted "$stashed_format" "S:$stashed"
|
||
|
fi
|
||
|
|
||
13 years ago
|
# Format action.
|
||
|
action="$(_git-action)"
|
||
|
if [[ -n "$action" ]]; then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' action 'action_format'
|
||
13 years ago
|
zformat -f action_formatted "$action_format" "s:$action"
|
||
|
fi
|
||
13 years ago
|
|
||
13 years ago
|
# Get current status.
|
||
|
while IFS=$'\n' read line; do
|
||
|
# Count added, deleted, modified, renamed, unmerged, untracked, dirty.
|
||
|
# T (type change) is undocumented, see http://git.io/FnpMGw.
|
||
|
# For a table of scenarii, see http://i.imgur.com/2YLu1.png.
|
||
|
[[ "$line" == ([ACDMT][\ MT]|[ACMT]D)\ * ]] && (( added++ ))
|
||
|
[[ "$line" == [\ ACMRT]D\ * ]] && (( deleted++ ))
|
||
|
[[ "$line" == ?[MT]\ * ]] && (( modified++ ))
|
||
|
[[ "$line" == R?\ * ]] && (( renamed++ ))
|
||
|
[[ "$line" == (AA|DD|U?|?U)\ * ]] && (( unmerged++ ))
|
||
|
[[ "$line" == \?\?\ * ]] && (( untracked++ ))
|
||
|
(( dirty++ ))
|
||
|
done < <(${(z)status_cmd} 2> /dev/null)
|
||
13 years ago
|
|
||
|
# Format branch.
|
||
13 years ago
|
branch="${$(git symbolic-ref -q HEAD)##refs/heads/}"
|
||
13 years ago
|
if [[ -n "$branch" ]]; then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' branch 'branch_format'
|
||
13 years ago
|
zformat -f branch_formatted "$branch_format" "b:$branch"
|
||
13 years ago
|
|
||
13 years ago
|
# Format remote.
|
||
13 years ago
|
remote="${$(${(z)remote_cmd} 2> /dev/null)##refs/remotes/}"
|
||
|
if [[ -n "$remote" ]]; then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' remote 'remote_format'
|
||
13 years ago
|
zformat -f remote_formatted "$remote_format" "R:$remote"
|
||
|
|
||
|
# Get ahead and behind counts.
|
||
|
ahead_and_behind="$(${(z)ahead_and_behind_cmd} 2> /dev/null)"
|
||
|
|
||
|
# Format ahead.
|
||
|
ahead="$ahead_and_behind[(w)1]"
|
||
|
if (( $ahead > 0 )); then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' ahead 'ahead_format'
|
||
13 years ago
|
zformat -f ahead_formatted "$ahead_format" "A:$ahead"
|
||
|
fi
|
||
13 years ago
|
|
||
13 years ago
|
# Format behind.
|
||
|
behind="$ahead_and_behind[(w)2]"
|
||
|
if (( $behind > 0 )); then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' behind 'behind_format'
|
||
13 years ago
|
zformat -f behind_formatted "$behind_format" "B:$behind"
|
||
|
fi
|
||
|
fi
|
||
13 years ago
|
else
|
||
|
# Format position.
|
||
|
position="$(git describe --contains --all HEAD 2> /dev/null)"
|
||
|
if [[ -n "$position" ]]; then
|
||
|
zstyle -s ':omz:plugin:git:prompt' position 'position_format'
|
||
|
zformat -f position_formatted "$position_format" "p:$position"
|
||
|
fi
|
||
13 years ago
|
fi
|
||
|
|
||
|
# Format added.
|
||
|
if (( $added > 0 )); then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' added 'added_format'
|
||
13 years ago
|
zformat -f added_formatted "$added_format" "a:$added_format"
|
||
|
fi
|
||
|
|
||
|
# Format deleted.
|
||
|
if (( $deleted > 0 )); then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' deleted 'deleted_format'
|
||
13 years ago
|
zformat -f deleted_formatted "$deleted_format" "d:$deleted_format"
|
||
|
fi
|
||
|
|
||
|
# Format modified.
|
||
|
if (( $modified > 0 )); then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' modified 'modified_format'
|
||
13 years ago
|
zformat -f modified_formatted "$modified_format" "m:$modified"
|
||
|
fi
|
||
|
|
||
|
# Format renamed.
|
||
|
if (( $renamed > 0 )); then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' renamed 'renamed_format'
|
||
13 years ago
|
zformat -f renamed_formatted "$renamed_format" "r:$renamed"
|
||
|
fi
|
||
|
|
||
|
# Format unmerged.
|
||
|
if (( $unmerged > 0 )); then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' unmerged 'unmerged_format'
|
||
13 years ago
|
zformat -f unmerged_formatted "$unmerged_format" "U:$unmerged"
|
||
|
fi
|
||
|
|
||
|
# Format untracked.
|
||
|
if (( $untracked > 0 )); then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' untracked 'untracked_format'
|
||
13 years ago
|
zformat -f untracked_formatted "$untracked_format" "u:$untracked"
|
||
|
fi
|
||
|
|
||
13 years ago
|
# Format dirty.
|
||
|
if (( $dirty > 0 )); then
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' dirty 'dirty_format'
|
||
13 years ago
|
zformat -f dirty_formatted "$dirty_format" "D:$dirty"
|
||
|
fi
|
||
|
|
||
13 years ago
|
# Format prompts.
|
||
13 years ago
|
zstyle -s ':omz:module:git:prompt' prompt 'prompt_format'
|
||
|
zstyle -s ':omz:module:git:prompt' rprompt 'rprompt_format'
|
||
13 years ago
|
|
||
|
git_info_vars=(
|
||
|
git_prompt_info "$prompt_format"
|
||
|
git_rprompt_info "$rprompt_format"
|
||
|
)
|
||
|
|
||
|
for git_info_var in ${(k)git_info_vars}; do
|
||
13 years ago
|
zformat -f "$git_info_var" "$git_info_vars[$git_info_var]" \
|
||
13 years ago
|
"A:$ahead_formatted" \
|
||
|
"B:$behind_formatted" \
|
||
13 years ago
|
"D:$dirty_formatted" \
|
||
|
"R:$remote_formatted" \
|
||
|
"S:$stashed_formatted" \
|
||
|
"U:$unmerged_formatted" \
|
||
|
"a:$added_formatted" \
|
||
13 years ago
|
"b:$branch_formatted" \
|
||
|
"c:$commit_formatted" \
|
||
|
"d:$deleted_formatted" \
|
||
|
"m:$modified_formatted" \
|
||
13 years ago
|
"p:$position_formatted" \
|
||
13 years ago
|
"r:$renamed_formatted" \
|
||
13 years ago
|
"s:$action_formatted" \
|
||
13 years ago
|
"u:$untracked_formatted"
|
||
|
done
|
||
|
|
||
|
unset _git_info_executing
|
||
|
return 0
|
||
|
}
|
||
|
|
||
13 years ago
|
git-info "$@"
|
||
|
|