forked from fossasia/open-event-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.py
More file actions
59 lines (42 loc) · 1.45 KB
/
git.py
File metadata and controls
59 lines (42 loc) · 1.45 KB
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
import logging
from command import execute
logger = logging.getLogger(__name__)
class GitError(Exception):
def __init__(self, message, errors):
super().__init__(message)
self.message = message
self.errors = errors
def __str__(self):
return '{}:\n {}'.format(self.message, self.errors)
def _git(cwd, *cmd):
retcode, out, err = execute(cwd, '/usr/bin/git', *cmd)
if retcode == 0:
return out
raise GitError('git exited with a non-zero exit code', err)
class Git:
def __init__(self, repo, cwd, branch='master'):
self.repo = repo
self.cwd = cwd
self.branch = branch
def clone_if_necessary(self):
try:
self.status()
except GitError:
logger.info('cloning %s', self.repo)
return _git('.', 'clone', '-b', self.branch, self.repo, self.cwd)
def status(self):
return _git(self.cwd, 'status', '-sb')
def fetch(self):
return _git(self.cwd, 'fetch', 'origin', self.branch)
def pull(self):
return _git(self.cwd, 'pull', '--rebase')
def last_commit_date(self):
return _git(self.cwd, 'log', '-1', '--format=%cd')
def changed_files(self):
self.fetch()
res = _git(self.cwd, 'diff', '--stat', 'origin/{}'.format(self.branch))
lines = res.splitlines()
if lines:
last_line = lines[-1]
return int(last_line.split()[0])
return 0