3d6f9d78c5a2335751d2980919ef1a067dbda9e2

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge pull request #306 from jtriley/git-prompt

add git-prompt plugin from olivierverdier/zsh-git-prompt

Diff

  1diff --git a/plugins/git-prompt/git-prompt.plugin.zsh b/plugins/git-prompt/git-prompt.plugin.zsh
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..01b8a88d921247bc43d5e003956ffb4fefa2dd53
  4--- /dev/null
  5+++ b/plugins/git-prompt/git-prompt.plugin.zsh
  6@@ -0,0 +1,60 @@
  7+# ZSH Git Prompt Plugin from:
  8+# http://github.com/olivierverdier/zsh-git-prompt
  9+#
 10+export __GIT_PROMPT_DIR=$ZSH/plugins/git-prompt
 11+# Initialize colors.
 12+autoload -U colors
 13+colors
 14+
 15+# Allow for functions in the prompt.
 16+setopt PROMPT_SUBST
 17+
 18+## Enable auto-execution of functions.
 19+typeset -ga preexec_functions
 20+typeset -ga precmd_functions
 21+typeset -ga chpwd_functions
 22+
 23+# Append git functions needed for prompt.
 24+preexec_functions+='preexec_update_git_vars'
 25+precmd_functions+='precmd_update_git_vars'
 26+chpwd_functions+='chpwd_update_git_vars'
 27+
 28+## Function definitions
 29+function preexec_update_git_vars() {
 30+    case "$2" in
 31+        git*)
 32+        __EXECUTED_GIT_COMMAND=1
 33+        ;;
 34+    esac
 35+}
 36+
 37+function precmd_update_git_vars() {
 38+    if [ -n "$__EXECUTED_GIT_COMMAND" ]; then
 39+        update_current_git_vars
 40+        unset __EXECUTED_GIT_COMMAND
 41+    fi
 42+}
 43+
 44+function chpwd_update_git_vars() {
 45+    update_current_git_vars
 46+}
 47+
 48+function update_current_git_vars() {
 49+    unset __CURRENT_GIT_STATUS
 50+
 51+    local gitstatus="$__GIT_PROMPT_DIR/gitstatus.py"
 52+    _GIT_STATUS=`python ${gitstatus}`
 53+    __CURRENT_GIT_STATUS=("${(f)_GIT_STATUS}")
 54+}
 55+
 56+function prompt_git_info() {
 57+    if [ -n "$__CURRENT_GIT_STATUS" ]; then
 58+        echo "(%{${fg[red]}%}$__CURRENT_GIT_STATUS[1]%{${fg[default]}%}$__CURRENT_GIT_STATUS[2]%{${fg[magenta]}%}$__CURRENT_GIT_STATUS[3]%{${fg[default]}%})"
 59+    fi
 60+}
 61+
 62+# Set the prompt.
 63+#PROMPT='%B%m%~%b$(prompt_git_info) %# '
 64+# for a right prompt:
 65+#RPROMPT='%b$(prompt_git_info)'
 66+RPROMPT='$(prompt_git_info)'
 67diff --git a/plugins/git-prompt/gitstatus.py b/plugins/git-prompt/gitstatus.py
 68new file mode 100644
 69index 0000000000000000000000000000000000000000..ee6fab9bd64a2201d47a66767fb4a40988f4aa87
 70--- /dev/null
 71+++ b/plugins/git-prompt/gitstatus.py
 72@@ -0,0 +1,68 @@
 73+#!/usr/bin/env python
 74+# -*- coding: UTF-8 -*-
 75+
 76+# change those symbols to whatever you prefer
 77+symbols = {'ahead of': '↑', 'behind': '↓', 'staged':'♦', 'changed':'‣', 'untracked':'…', 'clean':'⚡', 'unmerged':'≠', 'sha1':':'}
 78+
 79+from subprocess import Popen, PIPE
 80+
 81+output,error = Popen(['git','status'], stdout=PIPE, stderr=PIPE).communicate()
 82+
 83+if error:
 84+	import sys
 85+	sys.exit(0)
 86+lines = output.splitlines()
 87+
 88+import re
 89+behead_re = re.compile(r"^# Your branch is (ahead of|behind) '(.*)' by (\d+) commit")
 90+diverge_re = re.compile(r"^# and have (\d+) and (\d+) different")
 91+
 92+status = ''
 93+staged = re.compile(r'^# Changes to be committed:$', re.MULTILINE)
 94+changed = re.compile(r'^# Changed but not updated:$', re.MULTILINE)
 95+untracked = re.compile(r'^# Untracked files:$', re.MULTILINE)
 96+unmerged = re.compile(r'^# Unmerged paths:$', re.MULTILINE)
 97+
 98+def execute(*command):
 99+	out, err = Popen(stdout=PIPE, stderr=PIPE, *command).communicate()
100+	if not err:
101+		nb = len(out.splitlines())
102+	else:
103+		nb = '?'
104+	return nb
105+
106+if staged.search(output):
107+	nb = execute(['git','diff','--staged','--name-only','--diff-filter=ACDMRT'])
108+	status += '%s%s' % (symbols['staged'], nb)
109+if unmerged.search(output):
110+	nb = execute(['git','diff', '--staged','--name-only', '--diff-filter=U'])
111+	status += '%s%s' % (symbols['unmerged'], nb)
112+if changed.search(output):
113+	nb = execute(['git','diff','--name-only', '--diff-filter=ACDMRT'])
114+	status += '%s%s' % (symbols['changed'], nb)
115+if untracked.search(output):
116+## 		nb = len(Popen(['git','ls-files','--others','--exclude-standard'],stdout=PIPE).communicate()[0].splitlines())
117+## 		status += "%s" % (symbols['untracked']*(nb//3 + 1), )
118+	status += symbols['untracked']
119+if status == '':
120+	status = symbols['clean']
121+
122+remote = ''
123+
124+bline = lines[0]
125+if bline.find('Not currently on any branch') != -1:
126+	branch = symbols['sha1']+ Popen(['git','rev-parse','--short','HEAD'], stdout=PIPE).communicate()[0][:-1]
127+else:
128+	branch = bline.split(' ')[3]
129+	bstatusline = lines[1]
130+	match = behead_re.match(bstatusline)
131+	if match:
132+		remote = symbols[match.groups()[0]]
133+		remote += match.groups()[2]
134+	elif lines[2:]:
135+		div_match = diverge_re.match(lines[2])
136+	 	if div_match:
137+			remote = "{behind}{1}{ahead of}{0}".format(*div_match.groups(), **symbols)
138+
139+print '\n'.join([branch,remote,status])
140+