forked from dnktty/batch-git-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
58 lines (50 loc) · 1.33 KB
/
common.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
from termcolor import colored
from subprocess import Popen, PIPE
from multiprocessing import cpu_count
import re
def get_cpu_count():
return cpu_count()
def git(commands, path=None):
try:
cmd = ['git'] + commands
except:
cmd = ['git'] + [commands]
run = Popen(cmd,
stdout=PIPE,
stderr=PIPE,
cwd=path)
run.wait()
err = [l.strip() for l in run.stderr]
if run.returncode == 0:
out = [l.strip() for l in run.stdout]
else:
out = []
return out, err, run.returncode
def pprint(text, index):
colors = [ 'white', 'blue', 'yellow', 'green']
i = index % len(colors)
print(colored(text, colors[i]))
def filter_output(line):
try:
if (
not line.startswith('Warning: Permanently added')
and not line.startswith('See git-pull(1) for details.')
and not line.startswith('git pull <remote> <branch>')
):
return str(line.strip())
else:
return ''
except:
return str(line.strip())
def git_branch_name(path):
out, err, ret = git(['rev-parse', '--abbrev-ref', 'HEAD'], path)
return out
def git_remotes(path):
out, err, ret = git(['remote', '-v'], path)
if ret == 0:
for r in out:
try:
return re.search('^\w+\s+(.+)\s\(fetch\)$', r.decode("utf-8")).group(1)
except AttributeError:
return None
return None