Improved the coding style of git-info.

main
Sorin Ionescu 13 years ago
parent 3f3b2328f9
commit 60b15d0035

@ -156,7 +156,7 @@ function git-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
@ -183,57 +183,60 @@ function git-info() {
status_cmd='git status --short --branch' status_cmd='git status --short --branch'
# Ignore submodule status. # Ignore submodule status.
zstyle -b ':omz:plugin:git:prompt:ignore' submodule ignore_submodule zstyle -b \
zstyle -s ':omz:plugin:git:prompt:ignore:submodule' when ignore_submodule_when ':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 if is-true "$ignore_submodule"; then
status_cmd+=" --ignore-submodules=${ignore_submodule_when:-all}" status_cmd+=" --ignore-submodules=${ignore_submodule_when:-all}"
fi fi
# Get commit. # Get commit.
commit="$(git rev-parse HEAD 2> /dev/null)" commit="$(git rev-parse HEAD 2>/dev/null)"
# Format commit (short). # Format commit (short).
commit_short="$commit[1,7]" 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" zformat -f commit_formatted "$commit_format" "c:$commit_short"
# Stashed # Stashed
if [[ -f "$(git-root)/.git/refs/stash" ]]; then if [[ -f "$(git-root)/.git/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
# Assume that the working copy is clean. # 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 while IFS=$'\n' read line; do
(( line_number++ )) (( line_number++ ))
if (( line_number == 1)) && [[ "$line" == *'(no branch)'* ]]; then if (( line_number == 1 )) && [[ "$line" == *'(no branch)'* ]]; then
# Set branch to commit (short) when the branch is not found. # Set branch to commit (short) when the branch is not found.
branch="$commit_short" branch="$commit_short"
# Get action. # Get action.
action="$(_git-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 )) \ elif (( line_number == 1 )) \
&& [[ "$line" == (#b)'## Initial commit on '(?##) ]]; then && [[ "$line" == (#b)'## Initial commit on '(?##) ]];
then
branch="$match[1]" branch="$match[1]"
elif (( line_number == 1 )); then elif (( line_number == 1 )); then
# Split the line into an array for parsing. # Split the line into an array for parsing.
branch_info=(${(s: :)line}) branch_info=(${(s: :)line})
# Match: master...origin/master # Match: master...origin/master
if [[ $branch_info[2] == (#b)(?##)...(?##/?##) ]]; then if [[ "$branch_info[2]" == (#b)(?##)...(?##/?##) ]]; then
branch="$match[1]" branch="$match[1]"
remote="$match[2]" remote="$match[2]"
# Match: [ahead or [behind # 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]" ahead_or_behind="$match[1]"
if [[ "$ahead_or_behind" == 'behind' ]]; then if [[ "$ahead_or_behind" == 'behind' ]]; then
# Extract digits: 10] # Extract digits: 10]
@ -246,12 +249,17 @@ function git-info() {
fi fi
fi fi
# Match: master # Match: master
elif [[ $branch_info[2] == (#b)(?##) ]]; then elif [[ "$branch_info[2]" == (#b)(?##) ]]; then
branch=$match[1] branch="$match[1]"
fi fi
else else
# Format dirty. # 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 # Count: added/deleted/modified/renamed/unmerged/untracked
[[ "$line" == (((A|M|D|T) )|(AD|AM|AT|MM))\ * ]] && (( added++ )) [[ "$line" == (((A|M|D|T) )|(AD|AM|AT|MM))\ * ]] && (( added++ ))
@ -261,72 +269,75 @@ function git-info() {
[[ "$line" == UU\ * ]] && (( unmerged++ )) [[ "$line" == UU\ * ]] && (( unmerged++ ))
[[ "$line" == \?\?\ * ]] && (( untracked++ )) [[ "$line" == \?\?\ * ]] && (( untracked++ ))
fi fi
done < <("${(z)status_cmd}" 2> /dev/null) done < <("${(z)status_cmd}" 2>/dev/null)
# Format branch. # 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" zformat -f branch_formatted "$branch_format" "b:$branch"
# Format remote. # Format remote.
if [[ "$branch" != "$commit" ]]; then if [[ "$branch" != "$commit" ]]; then
[[ -z $remote ]] \ if [[ -z "$remote" ]]; then
&& remote=${$(git rev-parse --verify ${branch}@{upstream} \ remote="${$( \
--symbolic-full-name 2> /dev/null)#refs/remotes/} git rev-parse \
zstyle -s ':omz:plugin:git:prompt' remote remote_format --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" zformat -f remote_formatted "$remote_format" "R:$remote"
fi fi
# Format ahead. # Format ahead.
if [[ -n "$ahead" ]]; 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 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" zformat -f behind_formatted "$behind_format" "B:$behind"
fi fi
# Format added. # Format added.
if (( $added > 0 )); then 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" zformat -f added_formatted "$added_format" "a:$added_format"
fi fi
# Format deleted. # Format deleted.
if (( $deleted > 0 )); then 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" zformat -f deleted_formatted "$deleted_format" "d:$deleted_format"
fi fi
# Format modified. # Format modified.
if (( $modified > 0 )); then 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" zformat -f modified_formatted "$modified_format" "m:$modified"
fi fi
# Format renamed. # Format renamed.
if (( $renamed > 0 )); then 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" zformat -f renamed_formatted "$renamed_format" "r:$renamed"
fi fi
# Format unmerged. # Format unmerged.
if (( $unmerged > 0 )); then 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" zformat -f unmerged_formatted "$unmerged_format" "U:$unmerged"
fi fi
# Format untracked. # Format untracked.
if (( $untracked > 0 )); then 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" zformat -f untracked_formatted "$untracked_format" "u:$untracked"
fi fi
# Format prompts. # Format prompts.
zstyle -s ':omz:plugin:git:prompt' prompt prompt_format zstyle -s ':omz:plugin:git:prompt' prompt 'prompt_format'
zstyle -s ':omz:plugin:git:prompt' rprompt rprompt_format zstyle -s ':omz:plugin:git:prompt' rprompt 'rprompt_format'
git_info_vars=( git_info_vars=(
git_prompt_info "$prompt_format" git_prompt_info "$prompt_format"
@ -334,7 +345,7 @@ function git-info() {
) )
for git_info_var in ${(k)git_info_vars}; do 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" \ "s:$action_formatted" \
"a:$added_formatted" \ "a:$added_formatted" \
"A:$ahead_formatted" \ "A:$ahead_formatted" \

Loading…
Cancel
Save