2012-01-31 23:37:51 -05:00
|
|
|
#
|
|
|
|
# Displays Git repository information.
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Sorin Ionescu <sorin.ionescu@gmail.com>
|
|
|
|
#
|
|
|
|
|
2012-03-07 12:03:10 -05:00
|
|
|
# Gets the path to the Git directory.
|
|
|
|
function _git-dir() {
|
|
|
|
local git_root="$(git-root)"
|
|
|
|
local git_dir_or_file="${git_root}/.git"
|
|
|
|
local git_dir
|
|
|
|
|
|
|
|
if [[ ! -d "$git_root" ]]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -f "$git_dir_or_file" ]]; then
|
|
|
|
git_dir="${${${$(<"$git_dir_or_file")}[(fr)gitdir:*]}#gitdir: }"
|
|
|
|
else
|
|
|
|
git_dir="$git_dir_or_file"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -d "$git_dir" ]]; then
|
|
|
|
print "$git_dir"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-09-05 00:37:36 -04:00
|
|
|
# Gets the Git special action (am, merge, rebase, etc.).
|
|
|
|
# Borrowed from vcs_info and edited.
|
|
|
|
function _git-action() {
|
|
|
|
local action=''
|
|
|
|
local action_dir
|
2012-03-07 12:03:10 -05:00
|
|
|
local git_dir="$(_git-dir)"
|
2011-09-05 00:37:36 -04:00
|
|
|
|
|
|
|
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
|
2011-10-11 23:13:58 -04:00
|
|
|
print "$action"
|
2011-09-05 00:37:36 -04:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
for action_dir in \
|
|
|
|
"${git_dir}/rebase-merge/interactive" \
|
|
|
|
"${git_dir}/.dotest-merge/interactive"; do
|
|
|
|
if [[ -f "$action_dir" ]]; then
|
2011-10-11 23:13:58 -04:00
|
|
|
print 'rebase-i'
|
2011-09-05 00:37:36 -04:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
for action_dir in \
|
|
|
|
"${git_dir}/rebase-merge" \
|
|
|
|
"${git_dir}/.dotest-merge"; do
|
|
|
|
if [[ -d "$action_dir" ]]; then
|
2011-10-11 23:13:58 -04:00
|
|
|
print 'rebase-m'
|
2011-09-05 00:37:36 -04:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -f "${git_dir}/MERGE_HEAD" ]]; then
|
2011-10-11 23:13:58 -04:00
|
|
|
print 'merge'
|
2011-09-05 00:37:36 -04:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -f "${git_dir}/CHERRY_PICK_HEAD" ]]; then
|
2011-10-11 23:13:58 -04:00
|
|
|
print 'cherry-pick'
|
2011-09-05 00:37:36 -04:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -f "${git_dir}/BISECT_LOG" ]]; then
|
2011-10-11 23:13:58 -04:00
|
|
|
print 'bisect'
|
2011-09-05 00:37:36 -04:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# Turns off git-info for the current repository.
|
|
|
|
function _git-info-abort() {
|
2012-01-20 22:03:17 -05:00
|
|
|
if ! is-true "$_git_info_executing"; then
|
2011-09-05 00:37:36 -04:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2012-01-19 02:28:01 -05:00
|
|
|
cat >&2 <<EOF
|
2011-09-05 00:37:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-01-19 02:28:01 -05:00
|
|
|
EOF
|
2011-09-05 00:37:36 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
function git-info() {
|
|
|
|
# Extended globbing is needed to parse repository status.
|
2011-10-10 22:16:06 -04:00
|
|
|
setopt LOCAL_OPTIONS
|
|
|
|
setopt EXTENDED_GLOB
|
2011-09-05 00:37:36 -04:00
|
|
|
|
|
|
|
local action
|
|
|
|
local action_format
|
|
|
|
local action_formatted
|
|
|
|
local added=0
|
|
|
|
local added_format
|
|
|
|
local added_formatted
|
|
|
|
local ahead
|
|
|
|
local ahead_format
|
|
|
|
local ahead_formatted
|
|
|
|
local ahead_or_behind
|
|
|
|
local behind
|
|
|
|
local behind_format
|
|
|
|
local behind_formatted
|
|
|
|
local branch
|
|
|
|
local branch_info
|
|
|
|
local branch_format
|
|
|
|
local branch_formatted
|
|
|
|
local clean
|
|
|
|
local clean_formatted
|
|
|
|
local commit
|
|
|
|
local commit_format
|
|
|
|
local deleted=0
|
|
|
|
local deleted_format
|
|
|
|
local deleted_formatted
|
2012-03-14 19:13:38 -04:00
|
|
|
local dirty=0
|
|
|
|
local dirty_format
|
2011-09-05 00:37:36 -04:00
|
|
|
local dirty_formatted
|
|
|
|
local line_number=0
|
|
|
|
local modified=0
|
|
|
|
local modified_format
|
|
|
|
local modified_formatted
|
|
|
|
local remote
|
|
|
|
local remote_format
|
|
|
|
local remote_formatted
|
|
|
|
local renamed=0
|
|
|
|
local renamed_format
|
|
|
|
local renamed_formatted
|
|
|
|
local commit_formatted
|
|
|
|
local stashed=0
|
|
|
|
local stashed_format
|
|
|
|
local stashed_formatted
|
|
|
|
local unmerged=0
|
|
|
|
local unmerged_format
|
|
|
|
local unmerged_formatted
|
|
|
|
local untracked=0
|
|
|
|
local untracked_format
|
|
|
|
local untracked_formatted
|
|
|
|
local prompt
|
|
|
|
local rprompt
|
|
|
|
local git_info_var
|
|
|
|
local -A git_info_vars
|
2012-02-03 19:12:47 -05:00
|
|
|
local status_cmd
|
|
|
|
local ignore_submodule
|
|
|
|
local ignore_submodule_when
|
2011-09-05 00:37:36 -04:00
|
|
|
|
|
|
|
# Clean up previous Git info.
|
|
|
|
unset git_prompt_info
|
|
|
|
unset git_rprompt_info
|
|
|
|
|
|
|
|
# Return if not inside a Git repository work tree.
|
2012-02-03 22:32:14 -05:00
|
|
|
if ! is-true "$(git rev-parse --is-inside-work-tree 2>/dev/null)"; then
|
2011-09-05 00:37:36 -04:00
|
|
|
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
|
2012-01-19 02:28:01 -05:00
|
|
|
print "usage: $0 [ on | off ]" >&2
|
2011-09-05 00:37:36 -04:00
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Return if git-info is disabled.
|
2012-01-20 22:03:17 -05:00
|
|
|
if ! is-true "${$(git config --bool prompt.showinfo):-true}"; then
|
2011-09-05 00:37:36 -04:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Used to abort and turn git-info off on SIGINT.
|
|
|
|
_git_info_executing=true
|
|
|
|
|
2012-02-03 19:12:47 -05:00
|
|
|
# Use short status for easy parsing.
|
|
|
|
status_cmd='git status --short --branch'
|
|
|
|
|
|
|
|
# Ignore submodule status.
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -b \
|
|
|
|
':omz:plugin:git:prompt:ignore' submodule 'ignore_submodule'
|
|
|
|
zstyle -s \
|
|
|
|
':omz:plugin:git:prompt:ignore:submodule' when 'ignore_submodule_when'
|
2012-02-03 19:12:47 -05:00
|
|
|
if is-true "$ignore_submodule"; then
|
|
|
|
status_cmd+=" --ignore-submodules=${ignore_submodule_when:-all}"
|
|
|
|
fi
|
|
|
|
|
2011-09-05 00:37:36 -04:00
|
|
|
# Get commit.
|
2012-02-03 22:32:14 -05:00
|
|
|
commit="$(git rev-parse HEAD 2>/dev/null)"
|
2011-09-05 00:37:36 -04:00
|
|
|
|
2012-03-22 21:26:30 -04:00
|
|
|
# Format commit.
|
|
|
|
if [[ -n "$commit" ]]; then
|
|
|
|
zstyle -s ':omz:plugin:git:prompt' commit 'commit_format'
|
|
|
|
zformat -f commit_formatted "$commit_format" "c:$commit"
|
|
|
|
fi
|
2011-09-05 00:37:36 -04:00
|
|
|
|
|
|
|
# Stashed
|
2012-03-07 12:03:10 -05:00
|
|
|
if [[ -f "$(_git-dir)/refs/stash" ]]; then
|
2011-09-05 00:37:36 -04:00
|
|
|
stashed="$(git stash list 2>/dev/null | wc -l)"
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -s ':omz:plugin:git:prompt' stashed 'stashed_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
zformat -f stashed_formatted "$stashed_format" "S:$stashed"
|
|
|
|
fi
|
|
|
|
|
|
|
|
while IFS=$'\n' read line; do
|
|
|
|
(( line_number++ ))
|
|
|
|
|
2012-02-03 22:32:14 -05:00
|
|
|
if (( line_number == 1 )) && [[ "$line" == *'(no branch)'* ]]; then
|
2011-09-05 00:37:36 -04:00
|
|
|
# Get action.
|
|
|
|
action="$(_git-action)"
|
2012-03-22 21:26:30 -04:00
|
|
|
|
|
|
|
# Format action.
|
2011-09-05 00:37:36 -04:00
|
|
|
if [[ -n "$action" ]]; then
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -s ':omz:plugin:git:prompt' action 'action_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
zformat -f action_formatted "$action_format" "s:$action"
|
|
|
|
fi
|
|
|
|
elif (( line_number == 1 )) \
|
2012-02-03 22:32:14 -05:00
|
|
|
&& [[ "$line" == (#b)'## Initial commit on '(?##) ]];
|
|
|
|
then
|
2011-09-05 00:37:36 -04:00
|
|
|
branch="$match[1]"
|
|
|
|
elif (( line_number == 1 )); then
|
|
|
|
# Split the line into an array for parsing.
|
|
|
|
branch_info=(${(s: :)line})
|
|
|
|
|
|
|
|
# Match: master...origin/master
|
2012-02-03 22:32:14 -05:00
|
|
|
if [[ "$branch_info[2]" == (#b)(?##)...(?##/?##) ]]; then
|
2011-09-05 00:37:36 -04:00
|
|
|
branch="$match[1]"
|
|
|
|
remote="$match[2]"
|
|
|
|
|
|
|
|
# Match: [ahead or [behind
|
2012-02-03 22:32:14 -05:00
|
|
|
if [[ "$branch_info[3]" == (#b)\[(ahead|behind) ]]; then
|
2011-09-05 00:37:36 -04:00
|
|
|
ahead_or_behind="$match[1]"
|
|
|
|
if [[ "$ahead_or_behind" == 'behind' ]]; then
|
|
|
|
# Extract digits: 10]
|
|
|
|
behind="${branch_info[4]%\]}"
|
|
|
|
else
|
|
|
|
# Extract digits: 10] or 10,
|
|
|
|
ahead="${branch_info[4]%[,\]]}"
|
|
|
|
# Extract digits: 10]
|
|
|
|
behind="${branch_info[6]%\]}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
# Match: master
|
2012-02-03 22:32:14 -05:00
|
|
|
elif [[ "$branch_info[2]" == (#b)(?##) ]]; then
|
|
|
|
branch="$match[1]"
|
2011-09-05 00:37:36 -04:00
|
|
|
fi
|
|
|
|
else
|
2012-03-14 19:13:38 -04:00
|
|
|
# Count added, deleted, modified, renamed, unmerged, untracked, dirty.
|
2012-03-08 16:58:03 -05:00
|
|
|
# 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++ ))
|
2011-09-05 00:37:36 -04:00
|
|
|
[[ "$line" == \?\?\ * ]] && (( untracked++ ))
|
2012-03-14 19:13:38 -04:00
|
|
|
(( dirty++ ))
|
2011-09-05 00:37:36 -04:00
|
|
|
fi
|
2012-02-03 22:32:14 -05:00
|
|
|
done < <("${(z)status_cmd}" 2>/dev/null)
|
2011-09-05 00:37:36 -04:00
|
|
|
|
|
|
|
# Format branch.
|
2012-03-22 21:26:30 -04:00
|
|
|
if [[ -n "$branch" ]]; then
|
|
|
|
zstyle -s ':omz:plugin:git:prompt' branch 'branch_format'
|
|
|
|
zformat -f branch_formatted "$branch_format" "b:$branch"
|
2011-09-05 00:37:36 -04:00
|
|
|
|
2012-03-22 21:26:30 -04:00
|
|
|
# Format remote.
|
2012-02-03 22:32:14 -05:00
|
|
|
if [[ -z "$remote" ]]; then
|
|
|
|
remote="${$( \
|
|
|
|
git rev-parse \
|
|
|
|
--verify ${branch}@{upstream} \
|
|
|
|
--symbolic-full-name 2>/dev/null)#refs/remotes/}"
|
|
|
|
fi
|
|
|
|
zstyle -s ':omz:plugin:git:prompt' remote 'remote_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
zformat -f remote_formatted "$remote_format" "R:$remote"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Format ahead.
|
|
|
|
if [[ -n "$ahead" ]]; then
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -s ':omz:plugin:git:prompt' ahead 'ahead_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
zformat -f ahead_formatted "$ahead_format" "A:$ahead"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Format behind.
|
|
|
|
if [[ -n "$behind" ]]; then
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -s ':omz:plugin:git:prompt' behind 'behind_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
zformat -f behind_formatted "$behind_format" "B:$behind"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Format added.
|
|
|
|
if (( $added > 0 )); then
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -s ':omz:plugin:git:prompt' added 'added_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
zformat -f added_formatted "$added_format" "a:$added_format"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Format deleted.
|
|
|
|
if (( $deleted > 0 )); then
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -s ':omz:plugin:git:prompt' deleted 'deleted_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
zformat -f deleted_formatted "$deleted_format" "d:$deleted_format"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Format modified.
|
|
|
|
if (( $modified > 0 )); then
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -s ':omz:plugin:git:prompt' modified 'modified_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
zformat -f modified_formatted "$modified_format" "m:$modified"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Format renamed.
|
|
|
|
if (( $renamed > 0 )); then
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -s ':omz:plugin:git:prompt' renamed 'renamed_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
zformat -f renamed_formatted "$renamed_format" "r:$renamed"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Format unmerged.
|
|
|
|
if (( $unmerged > 0 )); then
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -s ':omz:plugin:git:prompt' unmerged 'unmerged_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
zformat -f unmerged_formatted "$unmerged_format" "U:$unmerged"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Format untracked.
|
|
|
|
if (( $untracked > 0 )); then
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -s ':omz:plugin:git:prompt' untracked 'untracked_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
zformat -f untracked_formatted "$untracked_format" "u:$untracked"
|
|
|
|
fi
|
|
|
|
|
2012-03-14 19:13:38 -04:00
|
|
|
# Format dirty.
|
|
|
|
if (( $dirty > 0 )); then
|
|
|
|
zstyle -s ':omz:plugin:git:prompt' dirty 'dirty_format'
|
|
|
|
zformat -f dirty_formatted "$dirty_format" "D:$dirty"
|
2012-03-23 10:04:30 -04:00
|
|
|
else
|
|
|
|
zstyle -s ':omz:plugin:git:prompt' clean 'clean_formatted'
|
2012-03-14 19:13:38 -04:00
|
|
|
fi
|
|
|
|
|
2011-09-05 00:37:36 -04:00
|
|
|
# Format prompts.
|
2012-02-03 22:32:14 -05:00
|
|
|
zstyle -s ':omz:plugin:git:prompt' prompt 'prompt_format'
|
|
|
|
zstyle -s ':omz:plugin:git:prompt' rprompt 'rprompt_format'
|
2011-09-05 00:37:36 -04:00
|
|
|
|
|
|
|
git_info_vars=(
|
|
|
|
git_prompt_info "$prompt_format"
|
|
|
|
git_rprompt_info "$rprompt_format"
|
|
|
|
)
|
|
|
|
|
|
|
|
for git_info_var in ${(k)git_info_vars}; do
|
2012-02-03 22:32:14 -05:00
|
|
|
zformat -f "$git_info_var" "$git_info_vars[$git_info_var]" \
|
2011-09-05 00:37:36 -04:00
|
|
|
"s:$action_formatted" \
|
|
|
|
"a:$added_formatted" \
|
|
|
|
"A:$ahead_formatted" \
|
|
|
|
"B:$behind_formatted" \
|
|
|
|
"b:$branch_formatted" \
|
|
|
|
"C:$clean_formatted" \
|
|
|
|
"c:$commit_formatted" \
|
|
|
|
"d:$deleted_formatted" \
|
|
|
|
"D:$dirty_formatted" \
|
|
|
|
"m:$modified_formatted" \
|
|
|
|
"R:$remote_formatted" \
|
|
|
|
"r:$renamed_formatted" \
|
|
|
|
"S:$stashed_formatted" \
|
|
|
|
"U:$unmerged_formatted" \
|
|
|
|
"u:$untracked_formatted"
|
|
|
|
done
|
|
|
|
|
|
|
|
unset _git_info_executing
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-10-11 23:13:58 -04:00
|
|
|
git-info "$@"
|
|
|
|
|