[Fix #40] Use Git porcelain commands

main
Colin Hebert 13 years ago committed by Sorin Ionescu
parent 1f00f1db94
commit 11f3d2c5ae

@ -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,65 +224,29 @@ 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
(( line_number++ ))
if (( line_number == 1 )) && [[ "$line" == *'(no branch)'* ]]; then
# Get action.
action="$(_git-action)"
# Format action. # Format action.
action="$(_git-action)"
if [[ -n "$action" ]]; then if [[ -n "$action" ]]; then
zstyle -s ':omz:plugin:git:prompt' action 'action_format' zstyle -s ':omz:plugin:git:prompt' action 'action_format'
zformat -f action_formatted "$action_format" "s:$action" zformat -f action_formatted "$action_format" "s:$action"
fi fi
elif (( line_number == 1 )) \
&& [[ "$line" == (#b)'## Initial commit on '(?##) ]]; # Get current status.
then while IFS=$'\n' read line; do
branch="$match[1]"
elif (( line_number == 1 )); then
# Split the line into an array for parsing.
branch_info=(${(s: :)line})
# 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. # Count added, deleted, modified, renamed, unmerged, untracked, dirty.
# T (type change) is undocumented, see http://git.io/FnpMGw. # T (type change) is undocumented, see http://git.io/FnpMGw.
# For a table of scenarii, see http://i.imgur.com/2YLu1.png. # For a table of scenarii, see http://i.imgur.com/2YLu1.png.
@ -284,36 +257,38 @@ function git-info {
[[ "$line" == (AA|DD|U?|?U)\ * ]] && (( unmerged++ )) [[ "$line" == (AA|DD|U?|?U)\ * ]] && (( unmerged++ ))
[[ "$line" == \?\?\ * ]] && (( untracked++ )) [[ "$line" == \?\?\ * ]] && (( untracked++ ))
(( dirty++ )) (( dirty++ ))
fi done < <(${(z)status_cmd} 2> /dev/null)
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 \
--verify ${branch}@{upstream} \
--symbolic-full-name 2>/dev/null)#refs/remotes/}"
fi
zstyle -s ':omz:plugin:git:prompt' remote 'remote_format' zstyle -s ':omz:plugin:git:prompt' remote 'remote_format'
zformat -f remote_formatted "$remote_format" "R:$remote" zformat -f remote_formatted "$remote_format" "R:$remote"
fi
# Get ahead and behind counts.
ahead_and_behind="$(${(z)ahead_and_behind_cmd} 2> /dev/null)"
# Format ahead. # Format ahead.
if [[ -n "$ahead" ]]; then ahead="$ahead_and_behind[(w)1]"
if (( $ahead > 0 )); 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]"
if (( $behind > 0 )); then
zstyle -s ':omz:plugin:git:prompt' behind 'behind_format' zstyle -s ':omz:plugin:git:prompt' behind 'behind_format'
zformat -f behind_formatted "$behind_format" "B:$behind" zformat -f behind_formatted "$behind_format" "B:$behind"
fi fi
fi
fi
# Format added. # Format added.
if (( $added > 0 )); then if (( $added > 0 )); then

Loading…
Cancel
Save