forked from holatuwol/liferay-faster-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.py
75 lines (53 loc) · 1.73 KB
/
git.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
#!/usr/bin/env python
import os
from subprocess import Popen, PIPE
try:
from subprocess import DEVNULL
except ImportError:
import os
DEVNULL = open(os.devnull, 'wb')
global git_root
git_root = None
def _git(cmd, args, stderr=PIPE):
global git_root
cwd = os.getcwd() if git_root is None else git_root
pipe = Popen(['git', cmd] + list(args), cwd=cwd, stdout=PIPE, stderr=stderr)
out, err = pipe.communicate()
return out.decode('UTF-8', 'replace').strip()
def config(*args, **kwargs):
return _git('config', args, **kwargs)
def describe(*args, **kwargs):
return _git('describe', args, **kwargs)
def for_each_ref(*args, **kwargs):
return _git('for-each-ref', args, **kwargs)
def is_ancestor(*args, **kwargs):
global git_root
cwd = os.getcwd() if git_root is None else git_root
pipe = Popen(['git', 'merge-base', '--is-ancestor'] + list(args), cwd=cwd, stdout=DEVNULL, stderr=DEVNULL)
out, err = pipe.communicate()
return pipe.returncode == 0
def log(*args, **kwargs):
return _git('log', args, **kwargs)
def ls_files(*args, **kwargs):
return _git('ls-files', args, **kwargs)
def ls_tree(*args, **kwargs):
return _git('ls-tree', args, **kwargs)
def merge_base(*args, **kwargs):
return _git('merge-base', args, **kwargs)
def remote(*args, **kwargs):
return _git('remote', args, **kwargs)
def rev_parse(*args, **kwargs):
return _git('rev-parse', args, **kwargs)
def show(*args, **kwargs):
return _git('show', args, **kwargs)
def tag(*args, **kwargs):
return _git('tag', args, **kwargs)
git_root = rev_parse('--show-toplevel', stderr=DEVNULL)
if git_root is None or git_root == '':
git_root = None
current_branch = None
else:
try:
current_branch = rev_parse('--abbrev-ref', 'HEAD', stderr=DEVNULL)
except:
current_branch = 'HEAD'