-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.py
More file actions
116 lines (95 loc) · 3.81 KB
/
git.py
File metadata and controls
116 lines (95 loc) · 3.81 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
class Branch:
def __init__(self, name, remote=True):
self.name = name
self.remote = remote
def __repr__(self):
return 'Branch' + str({
'name': self.name,
'remote': self.remote
})
class GIT:
def __init__(self, repositoryPath, gitExecutable='git.exe'):
self.repositoryPath = repositoryPath
self.gitExecutable = gitExecutable
def fetch(self):
return self.runGit('fetch')
def status(self, short=True):
params = ['status']
if short:
params.append('--short')
return self.runGit(params)
def checkout(self, id, updateSubmodules=True, force=False):
params = ['checkout', self.parseId(id)]
if force:
params.append('--force')
self.runGit(params)
if updateSubmodules:
return self.runGit(['submodule', 'update', '--init', '--recursive'])
def merge(self, id):
return self.runGit(['merge', self.parseId(id)])
def revParse(self, id):
return self.runGit(['rev-parse', self.parseId(id)])
def mergeBase(self, id1, id2):
return self.runGit(['merge-base', self.parseId(id1), self.parseId(id2)])
def getDistance(self, olderId, newerId):
return self.runGit(['rev-list', '--count', '--left-only', self.parseId(newerId) + '...' + self.parseId(olderId)])
def reset(self, hard=True):
params = ['reset']
if hard:
params.append('--hard')
return self.runGit(params)
def diff(self, id=None, path=None):
params = ['diff']
if id:
params.append(self.parseId(id))
if path:
params.append('--')
params.append(path)
return self.runGit(params)
def getBranches(self, id, remoteOnly=True):
branches = []
refListStr = self.runGit(['log', '--format="%d"', '--max-count=1', id])
refList = refListStr.strip('"').strip().lstrip('(').rstrip(')').split(', ')
for item in refList:
if not 'tag:' in item:
remote = item.startswith('origin/')
name = item[len('origin/'):] if remote else item
if remote or (not remoteOnly):
branches.append(Branch(name, remote))
return branches
def getInfo(self, id):
return self.runGit(['log', '--max-count=1', self.parseId(id)])
def conflicts(self):
conflicts = []
for line in self.status().splitlines():
fields = line.split(' ', 1)
if 'U' in fields[0]:
conflicts.append({
'file': fields[1].strip(),
'diff': self.diff(path=fields[1])
})
return conflicts
def parseId(self, id):
commit = id
if type(id) is Branch:
if id.remote:
commit = 'origin/' + id.name
return commit
class GitError(Exception):
def __init__(self, result_code, text):
self.result_code = result_code
self.text = text
def __str__(self):
return str(self.result_code) + ': ' + self.text
def runGit(self, args):
params = args
if not type(args) is list:
params = [args]
process = subprocess.Popen([self.gitExecutable] + params, cwd=self.repositoryPath, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
response = process.communicate()
if process.returncode != 0:
raise self.GitError(process.returncode, response[1].decode('UTF-8').rstrip())
return response[0].decode('UTF-8').rstrip()