forked from lakshmankumar12/zsh-git-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitstatus.py
executable file
·125 lines (107 loc) · 3.99 KB
/
gitstatus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
from __future__ import print_function
# change this symbol to whatever you prefer
prehash = ':'
from subprocess import Popen, PIPE
gitbinary='git'
import sys
import os
gitsym = Popen([gitbinary, 'symbolic-ref', 'HEAD'], stdout=PIPE, stderr=PIPE)
branch, error = gitsym.communicate()
error_string = error.decode('utf-8')
if 'fatal: Not a git repository' in error_string:
sys.exit(0)
branch = branch.decode("utf-8").strip()[11:]
res, err = Popen([gitbinary,'diff','--name-status'], stdout=PIPE, stderr=PIPE).communicate()
err_string = err.decode('utf-8')
if 'fatal' in err_string:
sys.exit(0)
changed_files = [namestat[0] for namestat in res.decode("utf-8").splitlines()]
staged_files = [namestat[0] for namestat in Popen([gitbinary,'diff', '--staged','--name-status'], stdout=PIPE).communicate()[0].splitlines()]
nb_changed = len(changed_files) - changed_files.count('U')
nb_U = staged_files.count('U')
nb_staged = len(staged_files) - nb_U
staged = str(nb_staged)
conflicts = str(nb_U)
changed = str(nb_changed)
nb_untracked = len([0 for status in Popen([gitbinary,'status','--porcelain',],stdout=PIPE).communicate()[0].decode("utf-8").splitlines() if status.startswith('??')])
untracked = str(nb_untracked)
def get_stash():
cmd = Popen([gitbinary, 'rev-parse', '--git-dir'], stdout=PIPE, stderr=PIPE)
so, se = cmd.communicate()
stash_file = '%s%s' % (so.decode('utf-8').rstrip(), '/logs/refs/stash')
try:
with open(stash_file) as f:
return sum(1 for _ in f)
except IOError:
return 0
nb_stash = get_stash()
stash=str(nb_stash)
ahead, behind = 0,0
if not branch: # not on any branch
branch = prehash + Popen([gitbinary,'rev-parse','--short','HEAD'], stdout=PIPE).communicate()[0].decode("utf-8")[:-1]
else:
remote_name = Popen([gitbinary,'config','branch.%s.remote' % branch], stdout=PIPE).communicate()[0].decode("utf-8").strip()
if remote_name:
merge_name = Popen([gitbinary,'config','branch.%s.merge' % branch], stdout=PIPE).communicate()[0].decode("utf-8").strip()
if remote_name == '.': # local
remote_ref = merge_name
else:
remote_ref = 'refs/remotes/%s/%s' % (remote_name, merge_name[11:])
revgit = Popen([gitbinary, 'rev-list', '--left-right', '%s...HEAD' % remote_ref],stdout=PIPE, stderr=PIPE)
revlist = revgit.communicate()[0]
if revgit.poll(): # fallback to local
revlist = Popen([gitbinary, 'rev-list', '--left-right', '%s...HEAD' % merge_name],stdout=PIPE, stderr=PIPE).communicate()[0]
behead = revlist.decode("utf-8").splitlines()
ahead = len([x for x in behead if x[0]=='>'])
behind = len(behead) - ahead
def get_value_from_file(filename):
try:
with open(filename,'r') as f:
value=f.read().strip()
except:
value=0
return value
merge_activity=""
step=0
total=0
gitdir = Popen([gitbinary,'rev-parse','--git-dir'], stdout=PIPE).communicate()[0].decode("utf-8")[:-1]
if os.path.isdir(gitdir+'/rebase-merge'):
step = get_value_from_file(gitdir+'/rebase-merge/msgnum')
total = get_value_from_file(gitdir+'/rebase-merge/end')
if os.path.exists(gitdir+'/rebase-merge/interactive'):
merge_activity="|REBASE-i"
else:
merge_activity="|REBASE-m"
else:
if os.path.isdir(gitdir+'/rebase-apply'):
step = get_value_from_file(gitdir+'/rebase-apply/next')
total = get_value_from_file(gitdir+'/rebase-apply/last')
if os.path.exists(gitdir+'/rebase-apply/rebasing'):
merge_activity="|REBASE"
elif os.path.exists(gitdir+'/rebase-apply/applying'):
merge_activity="|AM"
else:
merge_activity="|AM/REBASE"
elif os.path.exists(gitdir+'/MERGE_HEAD'):
merge_activity="|MERGING"
elif os.path.exists(gitdir+'/CHERRY_PICK_HEAD'):
merge_activity="|CHERRY-PICKING"
elif os.path.exists(gitdir+'/REVERT_HEAD'):
merge_activity="|REVERTING"
elif os.path.exists(gitdir+'/BISECT_LOG'):
merge_activity="|BISECTING"
if step != 0 and total != 0:
merge_activity="%s-(%s/%s)"%(merge_activity,step,total)
out = ' '.join([
branch,
str(ahead),
str(behind),
staged,
conflicts,
changed,
untracked,
stash,
merge_activity,
])
print(out, end='')