-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.py
51 lines (43 loc) · 1.25 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
from subprocess import Popen, PIPE
class commit:
def __init__(self, data):
self.files = []
self.comment = ''
self.parse(data)
def parse(self, data):
first = False
second = False
for item in data:
if item[:6]=='commit': self.commit = item
elif item[:7]=='Author:': self.author = item
elif item[:5]=='Date:': self.date = item
elif item[:2]=='A\t' or item[:2]=='M\t': self.files.append(item.split('\t'))
if item == '':
if first == False: first = True
elif first == True: second = True
if first == True and second == False: self.comment += item + ' '
class git:
def __init__(self, **args):
self.path = args['path'] + '/.git'
def cut(self, data):
l = []
lCur = []
for x in data:
if x[:6]=='commit':
if len(lCur)>0:
l.append(lCur)
lCur = []
lCur.append(x)
if len(lCur)>0: l.append(lCur)
return l if len(l)>0 else None
def getCommits(self):
command = "git -C %s log -n 100 --name-status --all" % (self.path)
res = Popen(command, shell=True, stdin=PIPE, stdout=PIPE).stdout.read()
res = res.decode('utf-8').split('\n')
try:
return [commit(x) for x in self.cut(res)]
except:
return None
if __name__ == "__main__":
g = git(path='~/work/python/git2ftp')
print(g.getCommits())