From 60b15d0035346449f4a800e6f233305b1cfda16f Mon Sep 17 00:00:00 2001 From: Sorin Ionescu Date: Fri, 3 Feb 2012 22:32:14 -0500 Subject: [PATCH] Improved the coding style of git-info. --- plugins/git/functions/git-info | 75 +++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/plugins/git/functions/git-info b/plugins/git/functions/git-info index 197cd2a..5d0e7d3 100644 --- a/plugins/git/functions/git-info +++ b/plugins/git/functions/git-info @@ -156,7 +156,7 @@ function git-info() { unset git_rprompt_info # 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 fi @@ -183,57 +183,60 @@ 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 # Get commit. - commit="$(git rev-parse HEAD 2> /dev/null)" + commit="$(git rev-parse HEAD 2>/dev/null)" # 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++ )) - 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. branch="$commit_short" # 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() { 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++ )) @@ -261,72 +269,75 @@ function git-info() { [[ "$line" == UU\ * ]] && (( unmerged++ )) [[ "$line" == \?\?\ * ]] && (( untracked++ )) fi - done < <("${(z)status_cmd}" 2> /dev/null) + 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() { ) 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" \