-
Notifications
You must be signed in to change notification settings - Fork 182
/
_Git_Windows.py
76 lines (62 loc) · 2.47 KB
/
_Git_Windows.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
# coding=utf-8
import shlex
import subprocess as sp
import platform
# We need to not import any other files from SD, because this is used in nocrash.py.
if 'windows' not in platform.platform().lower():
raise NotImplementedError("Use the `sh` module's `git` from PyPI instead!")
GitError = sp.CalledProcessError
def _call_process(execcmd, _ok_code=None, return_data=True, return_tuple=False):
execcmd = ('git',) + execcmd
proc = sp.Popen(execcmd, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
(stdout, stderr) = proc.communicate()
retcode = proc.returncode
if retcode != 0:
if _ok_code and retcode in _ok_code:
pass
else:
raise GitError(retcode, execcmd, stdout, stderr)
if return_tuple:
to_return = (stdout, stderr, retcode)
return to_return
if return_data:
to_return = stdout.decode("utf-8")
return to_return
class Git(object):
# git
def __init__(self, *args, **kwargs):
if len(args) > 0:
return _call_process(args, **kwargs)
def __getattribute__(self, name):
def interceptor(*args, **kwargs):
adjusted_name = name.replace('_', '-')
return _call_process((adjusted_name,) + args, **kwargs)
try:
method_in_class = object.__getattribute__(self, name)
except AttributeError:
return interceptor
else:
return method_in_class
# git
@staticmethod
def __call__(*args, **kwargs):
return _call_process(args, **kwargs)
# remote.update
class remote: # noqa: N801
@staticmethod
def update(*args, **kwargs):
return _call_process(('remote', 'update',) + args, **kwargs)
# status with colours stripped
@staticmethod
def status_stripped(*args, **kwargs):
return _call_process(('-c', 'color.status=false', 'status',) + args, **kwargs)
# diff with colours stripped, filenames only
@staticmethod
def diff_filenames(*args, **kwargs):
return _call_process(('-c', 'color.diff=false', 'diff', '--name-only',) + args, **kwargs)
git = Git()
git_version = git.version(return_data=True).strip()
if ('indows' not in git_version):
raise NotImplementedError('The git program being used, ' + git_version + ', is not a Windows based version.'
' Be sure you installed Git for Windows and that it is in your path before any'
' other versions (e.g. before Cygwin).')