# # Displays Git repository information. # # Authors: # Sorin Ionescu # # Gets the path to the Git directory. function _git-dir() { local git_root="$(git-root)" local git_dir_or_file="${git_root}/.git" local git_dir if [[ ! -d "$git_root" ]]; then return 1 fi if [[ -f "$git_dir_or_file" ]]; then git_dir="${${${$(<"$git_dir_or_file")}[(fr)gitdir:*]}#gitdir: }" else git_dir="$git_dir_or_file" fi if [[ -d "$git_dir" ]]; then print "$git_dir" return 0 fi return 1 } # Gets the Git special action (am, merge, rebase, etc.). # Borrowed from vcs_info and edited. function _git-action() { local action='' local action_dir local git_dir="$(_git-dir)" for action_dir in \ "${git_dir}/rebase-apply" \ "${git_dir}/rebase" \ "${git_dir}/../.dotest"; do if [[ -d "$action_dir" ]] ; then if [[ -f "${action_dir}/rebasing" ]] ; then action='rebase' elif [[ -f "${action_dir}/applying" ]] ; then action='am' else action='am/rebase' fi print "$action" return 0 fi done for action_dir in \ "${git_dir}/rebase-merge/interactive" \ "${git_dir}/.dotest-merge/interactive"; do if [[ -f "$action_dir" ]]; then print 'rebase-i' return 0 fi done for action_dir in \ "${git_dir}/rebase-merge" \ "${git_dir}/.dotest-merge"; do if [[ -d "$action_dir" ]]; then print 'rebase-m' return 0 fi done if [[ -f "${git_dir}/MERGE_HEAD" ]]; then print 'merge' return 0 fi if [[ -f "${git_dir}/CHERRY_PICK_HEAD" ]]; then print 'cherry-pick' return 0 fi if [[ -f "${git_dir}/BISECT_LOG" ]]; then print 'bisect' return 0 fi return 1 } # Turns off git-info for the current repository. function _git-info-abort() { if ! is-true "$_git_info_executing"; then return 1 fi cat >&2 </dev/null)"; then return 1 fi if (( $# > 0 )); then if [[ "$1" == [Oo][Nn] ]]; then git config --bool prompt.showinfo true elif [[ "$1" == [Oo][Ff][Ff] ]]; then git config --bool prompt.showinfo false else print "usage: $0 [ on | off ]" >&2 fi return 0 fi # Return if git-info is disabled. if ! is-true "${$(git config --bool prompt.showinfo):-true}"; then return 1 fi # Used to abort and turn git-info off on SIGINT. _git_info_executing=true # Use short status for easy parsing. 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' 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)" # Format commit (short). commit_short="$commit[1,7]" zstyle -s ':omz:plugin:git:prompt' commit 'commit_format' zformat -f commit_formatted "$commit_format" "c:$commit_short" # Stashed if [[ -f "$(_git-dir)/refs/stash" ]]; then stashed="$(git stash list 2>/dev/null | wc -l)" 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' while IFS=$'\n' read line; do (( line_number++ )) 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' zformat -f action_formatted "$action_format" "s:$action" fi elif (( line_number == 1 )) \ && [[ "$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 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 # T (type change) is undocumented, see https://raw.github.com/gitster/git/master/Documentation/RelNotes/1.7.8.2.txt [[ "$line" == [AMDCT][MD\ ]\ * ]] && (( added++ )) [[ "$line" == [\ MARC]D\ * ]] && (( deleted++ )) [[ "$line" == ?[MT]\ * ]] && (( modified++ )) [[ "$line" == R?\ * ]] && (( renamed++ )) [[ "$line" == (?U|U?|AA|DD)\ * ]] && (( unmerged++ )) [[ "$line" == \?\?\ * ]] && (( untracked++ )) (( dirty++ )) fi done < <("${(z)status_cmd}" 2>/dev/null) # Format branch. zstyle -s ':omz:plugin:git:prompt' branch 'branch_format' zformat -f branch_formatted "$branch_format" "b:$branch" # Format remote. if [[ "$branch" != "$commit" ]]; then 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' zformat -f ahead_formatted "$ahead_format" "A:$ahead" fi # Format behind. if [[ -n "$behind" ]]; then 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' 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' 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' zformat -f modified_formatted "$modified_format" "m:$modified" fi # Format renamed. if (( $renamed > 0 )); then 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' zformat -f unmerged_formatted "$unmerged_format" "U:$unmerged" fi # Format untracked. if (( $untracked > 0 )); then zstyle -s ':omz:plugin:git:prompt' untracked 'untracked_format' zformat -f untracked_formatted "$untracked_format" "u:$untracked" fi # Format dirty. if (( $dirty > 0 )); then zstyle -s ':omz:plugin:git:prompt' dirty 'dirty_format' zformat -f dirty_formatted "$dirty_format" "D:$dirty" unset clean_formatted fi # Format prompts. 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" git_rprompt_info "$rprompt_format" ) for git_info_var in ${(k)git_info_vars}; do zformat -f "$git_info_var" "$git_info_vars[$git_info_var]" \ "s:$action_formatted" \ "a:$added_formatted" \ "A:$ahead_formatted" \ "B:$behind_formatted" \ "b:$branch_formatted" \ "C:$clean_formatted" \ "c:$commit_formatted" \ "d:$deleted_formatted" \ "D:$dirty_formatted" \ "m:$modified_formatted" \ "R:$remote_formatted" \ "r:$renamed_formatted" \ "S:$stashed_formatted" \ "U:$unmerged_formatted" \ "u:$untracked_formatted" done unset _git_info_executing return 0 } git-info "$@"