forked from FabriceSalvaire/CodeReview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGit.py
More file actions
136 lines (96 loc) · 4.36 KB
/
Copy pathGit.py
File metadata and controls
136 lines (96 loc) · 4.36 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
####################################################################################################
#
# CodeReview - A Code Review GUI
# Copyright (C) 2015 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program. If
# not, see <http://www.gnu.org/licenses/>.
#
####################################################################################################
####################################################################################################
import logging
import os
import pygit2 as git
####################################################################################################
_module_logger = logging.getLogger(__name__)
####################################################################################################
class RepositoryNotFound(Exception):
pass
####################################################################################################
class GitRepository(object):
_logger = _module_logger.getChild('GitRepository')
##############################################
def __init__(self, path):
path = os.path.realpath(path)
try:
repository_path = git.discover_repository(path)
self._repository = git.Repository(repository_path)
workdir = self._repository.workdir
if os.path.isdir(path) and not path.endswith(os.sep):
path += os.sep
self._path_filter = path.replace(workdir, '')
self._logger.info('\nPath %s\nWork Dir: %s\nPath Filter: %s',
path, workdir, self._path_filter)
except KeyError:
raise RepositoryNotFound
##############################################
@property
def workdir(self):
return self._repository.workdir
##############################################
def commits(self):
head = self._repository.head
head_commit = self._repository[head.target]
commits = [commit
for commit in self._repository.walk(head_commit.id, git.GIT_SORT_TIME)]
return commits
##############################################
def diff(self, a=None, b=None, cached=False, path_filter=None):
if path_filter is None:
path_filter = self._path_filter
patches = []
# GIT_DIFF_PATIENCE
diff = self._repository.diff(a=a, b=b, cached=cached)
diff.find_similar()
# flags, rename_threshold, copy_threshold, rename_from_rewrite_threshold, break_rewrite_threshold, rename_limit
for patch in diff:
delta = patch.delta
if path_filter and not delta.old_file.path.startswith(path_filter):
# self._logger.info('Skip ' + delta.old_file.path)
continue
patches.append(patch)
return Diff(diff, patches)
##############################################
def file_content(self, oid):
try:
return self._repository[oid].data.decode('utf-8')
except KeyError:
return None
####################################################################################################
class Diff(object):
##############################################
def __init__(self, diff, patches):
self.diff = diff
self._patches = patches
##############################################
def __len__(self):
return len(self._patches)
##############################################
def __iter__(self):
return iter(self._patches)
##############################################
def __getitem__(self, i):
return self._patches[i]
####################################################################################################
#
# End
#
####################################################################################################