-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscmstatus.py
207 lines (189 loc) · 6.72 KB
/
scmstatus.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from __future__ import print_function
# change those symbols to whatever you prefer
symbols = {'ahead of': '↑·', 'behind': '↓·', 'prehash':':'}
from subprocess import Popen, PIPE
from os.path import expanduser
import sys
import os.path
import os
is_debugging = os.getenv('SCMPROMPT_DEBUG', 'false') == 'true'
home = expanduser("~")
lib_dir = os.path.dirname(sys.argv[0])
current_dir = os.path.realpath(os.path.curdir)
def debuglog(label, obj):
if is_debugging:
sys.stderr.write("Debug[%s]: %s\n" % (label, repr(obj)))
def errorlog(label, obj):
sys.stderr.write("Error[%s]: %s\n" % (label, repr(obj)))
def get_distance(search, actual_dir):
distance = 0
searched = {}
breakin = 0
for i in search:
searched[i] = -1
for i in search:
path = os.path.join(actual_dir, i)
if os.path.exists(path):
searched[i] = 0
while breakin < len(search) and actual_dir != os.path.dirname(actual_dir):
actual_dir = os.path.dirname(actual_dir)
distance += 1
for i in search:
path = os.path.join(actual_dir, i)
if os.path.exists(path):
searched[i] = distance
breakin += 1
return searched
def get_git():
gitsym = Popen(['git', '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:
return None
branch = branch.decode('utf-8').strip()[11:]
res, err = Popen(['git','diff','--name-status'], stdout=PIPE, stderr=PIPE).communicate()
err_string = err.decode('utf-8')
if 'fatal' in err_string:
return None
changed_files = [namestat[0] for namestat in res.splitlines()]
staged_files = [namestat[0] for namestat in Popen(['git','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(Popen(['git','ls-files','--others','--exclude-standard'],stdout=PIPE).communicate()[0].splitlines())
untracked = str(nb_untracked)
if not nb_changed and not nb_staged and not nb_U and not nb_untracked:
clean = '1'
else:
clean = '0'
remote = ''
if not branch: # not on any branch
branch = symbols['prehash']+ Popen(['git','rev-parse','--short','HEAD'], stdout=PIPE).communicate()[0][:-1]
else:
remote_name = Popen(['git','config','branch.%s.remote' % branch], stdout=PIPE).communicate()[0].strip()
if remote_name:
merge_name = Popen(['git','config','branch.%s.merge' % branch], stdout=PIPE).communicate()[0].strip()
else:
remote_name = "origin"
merge_name = "refs/heads/%s" % branch
if remote_name == '.': # local
remote_ref = merge_name
else:
remote_ref = 'refs/remotes/%s/%s' % (remote_name, merge_name[11:])
revgit = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % remote_ref],stdout=PIPE, stderr=PIPE)
revlist = revgit.communicate()[0]
if revgit.poll(): # fallback to local
revlist = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % merge_name],stdout=PIPE, stderr=PIPE).communicate()[0]
behead = revlist.splitlines()
ahead = len([x for x in behead if x[0]=='>'])
behind = len(behead) - ahead
if behind:
remote += '%s%s' % (symbols['behind'], behind)
if ahead:
remote += '%s%s' % (symbols['ahead of'], ahead)
if remote == "":
remote = '.'
out = [
'git',
str(branch),
str(remote),
staged,
conflicts,
changed,
untracked,
clean
]
return out
def get_hg():
hg_rev, error = Popen('hg id -b -n', stdout=PIPE, stderr=PIPE, shell=True).communicate()
if error:
errorlog('error@115', error)
return None
hg_rev, hg_branch = hg_rev.strip().split(" ")
debuglog('branch', hg_branch)
hg_rev = hg_rev.replace('+', '←').strip('←')
try:
hg_rev = int(hg_rev)
if hg_rev < 0:
return None
hg_rev = str(hg_rev)
except:
pass
hg_counts_cmd = 'hg status | grep -v \'.orig\'| awk \'{arr[$1]+=1} END {for (i in arr) {print i,arr[i]}}\''
hg_counts = {
'S': 0, #staged,
'X': 0, #conflicts,
'M': 0, #changed,
'?': 0, #untracked,
'C': 1 #clean
}
if hg_rev.find('←') >= 0:
hg_counts['X'] = int(Popen('hg resolve --list | wc -l', stdout=PIPE, shell=True).communicate()[0].strip())
for line in Popen(hg_counts_cmd, stdout=PIPE, shell=True).communicate()[0].strip().split("\n"):
if line == '':
continue
hgst, count = line.split()
hgst = hgst.replace('!', '?')
hgst = hgst.replace('A', 'S')
hgst = hgst.replace('R', 'S')
hg_counts[hgst] += int(count)
hg_counts['C'] = 0
if (hg_counts['X']) > 0:
hg_counts['M'] -= hg_counts['X']
ahead, error = Popen('hg phase -r 0::tip | grep "draft" | wc -l', stdout=PIPE, stderr=PIPE, shell=True).communicate()
if error:
errorlog('error@151', error)
ahead = 0
else:
ahead = int(ahead)
cmdline = 'hg log -r \'%s::branch("%s")\' -b \'%s\' | grep changeset | wc -l' % (hg_rev, hg_branch, hg_branch)
debuglog('hg behind cmdline', cmdline)
behind, error = Popen(cmdline, stdout=PIPE, stderr=PIPE, shell=True).communicate()
if error:
errorlog('error@156', error)
behind = 0
else:
behind = int(behind) - 1
remote = ''
if behind:
remote += '%s%s' % (symbols['behind'], behind)
if ahead:
remote += '%s%s' % (symbols['ahead of'], ahead)
if remote == "":
remote = '.'
out = [
'hg',
'%s:%s' % (hg_branch, hg_rev),
remote,
str(hg_counts['S']), #staged,
str(hg_counts['X']), #conflicts,
str(hg_counts['M']), #changed,
str(hg_counts['?']), #untracked,
str(hg_counts['C']) #clean
]
return out
gitpath = os.path.join('.git')
hgpath = os.path.join('.hg', 'dirstate')
ints = get_distance((gitpath, hgpath), current_dir + '')
gint = ints[gitpath]
hint = ints[hgpath]
out = None
if gint == -1 and hint == -1:
sys.exit(0)
if gint == -1 and hint != -1:
out = get_hg()
if gint != -1 and hint == -1:
out = get_git()
if gint != -1 and hint != -1:
if gint < hint:
out = get_git()
else:
out = get_hg()
if not out:
out = []
print("\n".join(out))