All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
2 # Git shell integration
4 # Copyright © 2018 martin f. krafft <madduck@madduck.net>
5 # Released under the terms of the Artistic Licence 2.0
7 # Source repository: http://git.madduck.net/v/etc/zsh.git
13 git rev-parse --is-inside-work-tree >/dev/null 2>&1
19 # return the full path to the root of the current git repository
20 [ -d "$GIT_DIR" ] && echo "$GIT_DIR" && return 0
21 local dir; dir="$PWD/$(git rev-parse --show-cdup)"
22 # do not use --show-toplevel because it resolves symlinks
29 # use oh-my-zsh prompt info function if it exists
30 $(command -v git_prompt_info) && return
32 # return the name of the git branch we're on
34 gitdir="$(git rev-parse --git-dir)"
35 ref=$(git --git-dir="$gitdir" symbolic-ref -q HEAD 2>/dev/null \
36 || git --git-dir="$gitdir" name-rev --name-only HEAD 2>/dev/null) || return 1
37 echo "${ref#refs/heads/}"
40 __git_set_prompt_variable() {
41 __is_git_repo || return
43 local reporoot="$(__git_get_reporoot)" ||
44 { zerror "could not determine git repository root"; return 1 }
45 local branch="$(__git_get_branch)" ||
46 { zerror "could not determine git branch"; return 1 }
47 if [ -n "$VCSH_REPO_NAME" ]; then
48 # if vcsh is used to get a subshell, then the repo root is the home
49 # directory, but we want to indicate the vcsh context too:
50 eval set -- $(__vcs_get_prompt_path_components "$HOME" "$branch")
51 set -- "vcsh:$VCSH_REPO_NAME" "$2" "$1${3:+/$3}"
53 eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch")
54 if [ -d "$GIT_DIR" ]; then
55 set -- "${(D)GIT_DIR}" "$2" "${${1#$_D}%/}"
58 psvar[4]="$1${2:+|%B$2%b|}${3:-}"
60 add-zsh-hook chpwd __git_set_prompt_variable
61 __git_set_prompt_variable
63 __update_git_prompt_vars_if_git_ran() {
64 case "$(history $(($HISTCMD - 1)))" in
65 *git*) __git_set_prompt_variable
68 add-zsh-hook precmd __update_git_prompt_vars_if_git_ran
70 __git_print_preprompt()
72 [[ $? -eq 0 ]] || return
74 __is_git_repo || return
75 [ "$(git config --get core.bare)" = false ] || return
76 __on_networkfs && return
78 local COLUMNS=${COLUMNS:-80}
79 local LINES=${LINES:-25}
84 local output; output=(${(f)"$(cat)"})
86 [[ ${#output} -ge 1 ]] || return
88 local statl="$(echo ${output[-1]} | sed -re 's@^\s*([0-9]+)[^,]+(, ([0-9]+) [^(]+\(([-+])\))(, ([0-9]+) [^(]+\(([-+])\))?@\1/\4\3/\7\6@')"
90 if [[ ${output[-2]## } = '...' ]]; then
91 print "${title} (${statl%/}, abbrev.):"
92 print "${(F)output[1,-3]}"
95 print "${title} (${statl%/}):"
96 print "${(F)output[1,-2]}"
100 function gitdiffstat() {
102 local common_options="--stat=$((COLUMNS/2-1)),$((COLUMNS/4-2)),$(($LINES/3)) --relative"
103 eval git diff $common_options "$@" 2>/dev/null
106 local cached; cached=(${(f)"$(gitdiffstat --cached | output cached)"})
107 local changed; changed=(${(f)"$(gitdiffstat | output changed)"})
109 local max=${#changed}
110 [[ $max -lt ${#cached} ]] && max=${#cached}
112 ((max == 0)) && return
114 local width=$(((COLUMNS-3)/2))
116 if (( ${#cached} > 0 && ${#changed} > 0 )); then
118 for (( i=1 ; i <= max ; i++ )) do
119 printf "%-${width}s │ %-${width}s\n" "${cached[$i]}" "${changed[$i]}"
122 print ${(F)cached}${(F)changed}
125 add-zsh-hook precmd __git_print_preprompt