Browse Source

[Fix #40] Use Git porcelain commands

master
Colin Hebert 13 years ago committed by Sorin Ionescu
parent
commit
11f3d2c5ae
  1. 155
      plugins/git/functions/git-info

155
plugins/git/functions/git-info

@ -127,6 +127,8 @@ function git-info {
local added_format local added_format
local added_formatted local added_formatted
local ahead local ahead
local ahead_and_behind
local ahead_and_behind_cmd
local ahead_format local ahead_format
local ahead_formatted local ahead_formatted
local ahead_or_behind local ahead_or_behind
@ -134,53 +136,54 @@ function git-info {
local behind_format local behind_format
local behind_formatted local behind_formatted
local branch local branch
local branch_info
local branch_format local branch_format
local branch_formatted local branch_formatted
local branch_info
local clean local clean
local clean_formatted local clean_formatted
local commit local commit
local commit_format local commit_format
local commit_formatted
local deleted=0 local deleted=0
local deleted_format local deleted_format
local deleted_formatted local deleted_formatted
local dirty=0 local dirty=0
local dirty_format local dirty_format
local dirty_formatted local dirty_formatted
local ignore_submodule
local ignore_submodule_when
local line_number=0 local line_number=0
local modified=0 local modified=0
local modified_format local modified_format
local modified_formatted local modified_formatted
local prompt_format
local remote local remote
local remote_cmd
local remote_format local remote_format
local remote_formatted local remote_formatted
local renamed=0 local renamed=0
local renamed_format local renamed_format
local renamed_formatted local renamed_formatted
local commit_formatted local rprompt_format
local stashed=0 local stashed=0
local stashed_format local stashed_format
local stashed_formatted local stashed_formatted
local status_cmd
local unmerged=0 local unmerged=0
local unmerged_format local unmerged_format
local unmerged_formatted local unmerged_formatted
local untracked=0 local untracked=0
local untracked_format local untracked_format
local untracked_formatted local untracked_formatted
local prompt
local rprompt
local git_info_var
local -A git_info_vars local -A git_info_vars
local status_cmd local git_info_var
local ignore_submodule
local ignore_submodule_when
# Clean up previous Git info. # Clean up previous git-info.
unset git_prompt_info unset git_prompt_info
unset git_rprompt_info unset git_rprompt_info
# Return if not inside a Git repository work tree. # Return if not inside a Git repository work tree.
if ! is-true "$(git rev-parse --is-inside-work-tree 2>/dev/null)"; then if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then
return 1 return 1
fi fi
@ -203,8 +206,14 @@ function git-info {
# Used to abort and turn git-info off on SIGINT. # Used to abort and turn git-info off on SIGINT.
_git_info_executing=true _git_info_executing=true
# Use short status for easy parsing. # Use porcelain status for easy parsing.
status_cmd='git status --short --branch' 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}'
# Ignore submodule status. # Ignore submodule status.
zstyle -b \ zstyle -b \
@ -215,104 +224,70 @@ function git-info {
status_cmd+=" --ignore-submodules=${ignore_submodule_when:-all}" status_cmd+=" --ignore-submodules=${ignore_submodule_when:-all}"
fi fi
# Get commit.
commit="$(git rev-parse HEAD 2>/dev/null)"
# Format commit. # Format commit.
commit="$(git rev-parse HEAD 2> /dev/null)"
if [[ -n "$commit" ]]; then if [[ -n "$commit" ]]; then
zstyle -s ':omz:plugin:git:prompt' commit 'commit_format' zstyle -s ':omz:plugin:git:prompt' commit 'commit_format'
zformat -f commit_formatted "$commit_format" "c:$commit" zformat -f commit_formatted "$commit_format" "c:$commit"
fi fi
# Stashed # Format stashed.
if [[ -f "$(_git-dir)/refs/stash" ]]; then if [[ -f "$(_git-dir)/refs/stash" ]]; then
stashed="$(git stash list 2>/dev/null | wc -l)" stashed="$(git stash list 2> /dev/null | wc -l)"
zstyle -s ':omz:plugin:git:prompt' stashed 'stashed_format' zstyle -s ':omz:plugin:git:prompt' stashed 'stashed_format'
zformat -f stashed_formatted "$stashed_format" "S:$stashed" zformat -f stashed_formatted "$stashed_format" "S:$stashed"
fi fi
while IFS=$'\n' read line; do # Format action.
(( line_number++ )) action="$(_git-action)"
if [[ -n "$action" ]]; then
if (( line_number == 1 )) && [[ "$line" == *'(no branch)'* ]]; then zstyle -s ':omz:plugin:git:prompt' action 'action_format'
# Get action. zformat -f action_formatted "$action_format" "s:$action"
action="$(_git-action)" fi
# Format action. # Get current status.
if [[ -n "$action" ]]; then while IFS=$'\n' read line; do
zstyle -s ':omz:plugin:git:prompt' action 'action_format' # Count added, deleted, modified, renamed, unmerged, untracked, dirty.
zformat -f action_formatted "$action_format" "s:$action" # T (type change) is undocumented, see http://git.io/FnpMGw.
fi # For a table of scenarii, see http://i.imgur.com/2YLu1.png.
elif (( line_number == 1 )) \ [[ "$line" == ([ACDMT][\ MT]|[ACMT]D)\ * ]] && (( added++ ))
&& [[ "$line" == (#b)'## Initial commit on '(?##) ]]; [[ "$line" == [\ ACMRT]D\ * ]] && (( deleted++ ))
then [[ "$line" == ?[MT]\ * ]] && (( modified++ ))
branch="$match[1]" [[ "$line" == R?\ * ]] && (( renamed++ ))
elif (( line_number == 1 )); then [[ "$line" == (AA|DD|U?|?U)\ * ]] && (( unmerged++ ))
# Split the line into an array for parsing. [[ "$line" == \?\?\ * ]] && (( untracked++ ))
branch_info=(${(s: :)line}) (( dirty++ ))
done < <(${(z)status_cmd} 2> /dev/null)
# Match: master...origin/master
if [[ "$branch_info[2]" == (#b)(?##)...(?##/?##) ]]; then
branch="$match[1]"
remote="$match[2]"
# Match: [ahead or [behind
if [[ "$branch_info[3]" == (#b)\[(ahead|behind) ]]; then
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
elif [[ "$branch_info[2]" == (#b)(?##) ]]; then
branch="$match[1]"
fi
else
# 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++ ))
fi
done < <("${(z)status_cmd}" 2>/dev/null)
# Format branch. # Format branch.
branch="${$(git symbolic-ref -q HEAD)##refs/heads/}"
if [[ -n "$branch" ]]; then if [[ -n "$branch" ]]; then
zstyle -s ':omz:plugin:git:prompt' branch 'branch_format' zstyle -s ':omz:plugin:git:prompt' branch 'branch_format'
zformat -f branch_formatted "$branch_format" "b:$branch" zformat -f branch_formatted "$branch_format" "b:$branch"
# Format remote. # Format remote.
if [[ -z "$remote" ]]; then remote="${$(${(z)remote_cmd} 2> /dev/null)##refs/remotes/}"
remote="${$( \ if [[ -n "$remote" ]]; then
git rev-parse \ zstyle -s ':omz:plugin:git:prompt' remote 'remote_format'
--verify ${branch}@{upstream} \ zformat -f remote_formatted "$remote_format" "R:$remote"
--symbolic-full-name 2>/dev/null)#refs/remotes/}"
fi # Get ahead and behind counts.
zstyle -s ':omz:plugin:git:prompt' remote 'remote_format' ahead_and_behind="$(${(z)ahead_and_behind_cmd} 2> /dev/null)"
zformat -f remote_formatted "$remote_format" "R:$remote"
fi # Format ahead.
ahead="$ahead_and_behind[(w)1]"
# Format ahead. if (( $ahead > 0 )); then
if [[ -n "$ahead" ]]; then zstyle -s ':omz:plugin:git:prompt' ahead 'ahead_format'
zstyle -s ':omz:plugin:git:prompt' ahead 'ahead_format' zformat -f ahead_formatted "$ahead_format" "A:$ahead"
zformat -f ahead_formatted "$ahead_format" "A:$ahead" fi
fi
# Format behind. # Format behind.
if [[ -n "$behind" ]]; then behind="$ahead_and_behind[(w)2]"
zstyle -s ':omz:plugin:git:prompt' behind 'behind_format' if (( $behind > 0 )); then
zformat -f behind_formatted "$behind_format" "B:$behind" zstyle -s ':omz:plugin:git:prompt' behind 'behind_format'
zformat -f behind_formatted "$behind_format" "B:$behind"
fi
fi
fi fi
# Format added. # Format added.

Loading…
Cancel
Save