Browse Source

Improved the coding style of git-info.

master
Sorin Ionescu 13 years ago
parent
commit
60b15d0035
  1. 67
      plugins/git/functions/git-info

67
plugins/git/functions/git-info

@ -183,8 +183,10 @@ function git-info() { @@ -183,8 +183,10 @@ function git-info() {
status_cmd='git status --short --branch'
# Ignore submodule status.
zstyle -b ':omz:plugin:git:prompt:ignore' submodule ignore_submodule
zstyle -s ':omz:plugin:git:prompt:ignore:submodule' when ignore_submodule_when
zstyle -b \
':omz:plugin:git:prompt:ignore' submodule 'ignore_submodule'
zstyle -s \
':omz:plugin:git:prompt:ignore:submodule' when 'ignore_submodule_when'
if is-true "$ignore_submodule"; then
status_cmd+=" --ignore-submodules=${ignore_submodule_when:-all}"
fi
@ -194,18 +196,18 @@ function git-info() { @@ -194,18 +196,18 @@ function git-info() {
# Format commit (short).
commit_short="$commit[1,7]"
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_short"
# Stashed
if [[ -f "$(git-root)/.git/refs/stash" ]]; then
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"
fi
# Assume that the working copy is clean.
zstyle -s ':omz:plugin:git:prompt' clean clean_formatted
zstyle -s ':omz:plugin:git:prompt' clean 'clean_formatted'
while IFS=$'\n' read line; do
(( line_number++ ))
@ -217,23 +219,24 @@ function git-info() { @@ -217,23 +219,24 @@ function git-info() {
# Get action.
action="$(_git-action)"
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"
fi
elif (( line_number == 1 )) \
&& [[ "$line" == (#b)'## Initial commit on '(?##) ]]; then
&& [[ "$line" == (#b)'## Initial commit on '(?##) ]];
then
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
if [[ "$branch_info[2]" == (#b)(?##)...(?##/?##) ]]; then
branch="$match[1]"
remote="$match[2]"
# Match: [ahead or [behind
if [[ $branch_info[3] == (#b)\[(ahead|behind) ]]; then
if [[ "$branch_info[3]" == (#b)\[(ahead|behind) ]]; then
ahead_or_behind="$match[1]"
if [[ "$ahead_or_behind" == 'behind' ]]; then
# Extract digits: 10]
@ -246,12 +249,17 @@ function git-info() { @@ -246,12 +249,17 @@ function git-info() {
fi
fi
# Match: master
elif [[ $branch_info[2] == (#b)(?##) ]]; then
branch=$match[1]
elif [[ "$branch_info[2]" == (#b)(?##) ]]; then
branch="$match[1]"
fi
else
# Format dirty.
[[ -z "$dirty" ]] && zstyle -s ':omz:plugin:git:prompt' dirty dirty_formatted && unset clean_formatted
if [[ -z "$dirty" ]]; then
zstyle -s ':omz:plugin:git:prompt' dirty 'dirty_formatted'
if [[ -z "$dirty_formatted" ]]; then
unset clean_formatted
fi
fi
# Count: added/deleted/modified/renamed/unmerged/untracked
[[ "$line" == (((A|M|D|T) )|(AD|AM|AT|MM))\ * ]] && (( added++ ))
@ -264,69 +272,72 @@ function git-info() { @@ -264,69 +272,72 @@ function git-info() {
done < <("${(z)status_cmd}" 2>/dev/null)
# Format branch.
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"
# Format remote.
if [[ "$branch" != "$commit" ]]; then
[[ -z $remote ]] \
&& remote=${$(git rev-parse --verify ${branch}@{upstream} \
--symbolic-full-name 2> /dev/null)#refs/remotes/}
zstyle -s ':omz:plugin:git:prompt' remote remote_format
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'
zformat -f remote_formatted "$remote_format" "R:$remote"
fi
# Format ahead.
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"
fi
# Format behind.
if [[ -n "$behind" ]]; 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"
fi
# Format added.
if (( $added > 0 )); then
zstyle -s ':omz:plugin:git:prompt' added added_format
zstyle -s ':omz:plugin:git:prompt' added 'added_format'
zformat -f added_formatted "$added_format" "a:$added_format"
fi
# Format deleted.
if (( $deleted > 0 )); then
zstyle -s ':omz:plugin:git:prompt' deleted deleted_format
zstyle -s ':omz:plugin:git:prompt' deleted 'deleted_format'
zformat -f deleted_formatted "$deleted_format" "d:$deleted_format"
fi
# Format modified.
if (( $modified > 0 )); then
zstyle -s ':omz:plugin:git:prompt' modified modified_format
zstyle -s ':omz:plugin:git:prompt' modified 'modified_format'
zformat -f modified_formatted "$modified_format" "m:$modified"
fi
# Format renamed.
if (( $renamed > 0 )); then
zstyle -s ':omz:plugin:git:prompt' renamed renamed_format
zstyle -s ':omz:plugin:git:prompt' renamed 'renamed_format'
zformat -f renamed_formatted "$renamed_format" "r:$renamed"
fi
# Format unmerged.
if (( $unmerged > 0 )); then
zstyle -s ':omz:plugin:git:prompt' unmerged unmerged_format
zstyle -s ':omz:plugin:git:prompt' unmerged 'unmerged_format'
zformat -f unmerged_formatted "$unmerged_format" "U:$unmerged"
fi
# Format untracked.
if (( $untracked > 0 )); then
zstyle -s ':omz:plugin:git:prompt' untracked untracked_format
zstyle -s ':omz:plugin:git:prompt' untracked 'untracked_format'
zformat -f untracked_formatted "$untracked_format" "u:$untracked"
fi
# Format prompts.
zstyle -s ':omz:plugin:git:prompt' prompt prompt_format
zstyle -s ':omz:plugin:git:prompt' rprompt rprompt_format
zstyle -s ':omz:plugin:git:prompt' prompt 'prompt_format'
zstyle -s ':omz:plugin:git:prompt' rprompt 'rprompt_format'
git_info_vars=(
git_prompt_info "$prompt_format"
@ -334,7 +345,7 @@ function git-info() { @@ -334,7 +345,7 @@ function git-info() {
)
for git_info_var in ${(k)git_info_vars}; do
zformat -f $git_info_var $git_info_vars[$git_info_var] \
zformat -f "$git_info_var" "$git_info_vars[$git_info_var]" \
"s:$action_formatted" \
"a:$added_formatted" \
"A:$ahead_formatted" \

Loading…
Cancel
Save