Newer
Older
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
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import difflib
import dulwich, dulwich.patch
class RepoWrapper(dulwich.repo.Repo):
def get_branch(self, name=None):
if name is None:
name = 'master'
return self['refs/heads/'+name]
def history(self, branch=None, max_commits=None):
if max_commits is None:
max_commits = float('inf')
head = self.get_branch(branch)
while max_commits and head.parents:
yield head
head = self[head.parents[0]]
max_commits -= 1
def listdir(self, branch=None, root=None):
branch = self.get_branch(branch)
tree = self[branch.tree]
if root is not None:
for directory in root.split('/'):
tree = self[tree[directory].sha]
return tree.iteritems()
def commit_diff(self, commit):
parent = self[commit.parents[0]]
stringio = StringIO()
dulwich.patch.write_tree_diff(stringio, self.object_store,
parent.tree, commit.tree)
return stringio.getvalue()
class ChangeWrapper(dulwich.diff_tree.TreeChange):
def as_udiff(self):
with open(self.old.path) as f1, open(self.new.path) as f2:
return ''.join(difflib.unified_diff(f1, f2))
def Repo(name, path, _cache={}):
repo = _cache.get(path)
if repo is None:
repo = _cache[path] = RepoWrapper(path)
repo.name = name
return repo