本文整理汇总了Python中pygit2.Repository.diff方法的典型用法代码示例。如果您正苦于以下问题:Python Repository.diff方法的具体用法?Python Repository.diff怎么用?Python Repository.diff使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygit2.Repository
的用法示例。
在下文中一共展示了Repository.diff方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_and_update_repo_cache
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import diff [as 别名]
def get_and_update_repo_cache(repo_path):
cache_filename = '%s-stats.cache' % repo_path
if os.path.exists(cache_filename):
with open(cache_filename) as f:
data = load(f)
else:
data = {
'author_to_month_to_additions': defaultdict(defaultdict_int),
'author_to_month_to_deletions': defaultdict(defaultdict_int),
'author_to_month_to_commits': defaultdict(defaultdict_int),
'day_to_count': defaultdict(defaultdict_int),
'latest_sha': None,
}
repo = Repository(repo_path)
count = 0
for commit in repo.walk(repo.head.target, GIT_SORT_TOPOLOGICAL):
count += 1
if commit.type == GIT_OBJ_COMMIT:
if data['latest_sha'] == commit.hex:
break
if not commit.message.lower().startswith('merge'):
try:
d = repo.diff('%s^' % commit.hex, commit)
except KeyError:
# First commit!
break
patches = list(d)
additions = sum([p.additions for p in patches])
deletions = sum([p.deletions for p in patches])
author = author_aliases.get(commit.author.email, commit.author.email)
day = date.fromtimestamp(commit.commit_time)
data['day_to_count']['Lines'][day] += additions
data['day_to_count']['Lines'][day] -= deletions
if additions > 1000 and deletions < 5 and commit.hex not in whitelist_commits:
if commit.hex not in blacklist_commits:
print 'WARNING: ignored %s looks like an embedding of a lib (message: %s)' % (commit.hex, commit.message)
continue
if (additions > 3000 or deletions > 3000) and commit.hex not in whitelist_commits:
if commit.hex not in blacklist_commits and additions != deletions: # Guess that if additions == deletions it's a big rename of files
print 'WARNING: ignored %s because it is bigger than 3k lines. Put this commit in the whitelist or the blacklist (message: %s)' % (commit.hex, commit.message)
continue
month = date(day.year, day.month, 1)
data['author_to_month_to_additions'][author][month] += additions
data['author_to_month_to_deletions'][author][month] += deletions
data['author_to_month_to_commits'][author][month] += 1
if data['latest_sha'] is None:
data['latest_sha'] = commit.hex
with open(cache_filename, 'w') as f:
dump(data, f)
return data
示例2: processGitDiff
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import diff [as 别名]
def processGitDiff(commitsNum):
counter = commitsNum;
repositoryName = "../git-repos/postgres"
repo = Repository(repositoryName +"/"+ ".git")
childCommitNumber = ""
for commit in repo.walk(repo.head.target, GIT_SORT_TIME):
counter-=1;
if counter<0:
break
currentCommitNumber = commit.oid.hex
if(childCommitNumber!=""):
diff = repo.diff(currentCommitNumber, childCommitNumber)
fileChanges = 0;
for p in diff:
print(p.old_file_path)
#print(p.old_oid)
print(p.new_file_path)
#print(p.new_oid)
#print(p.additions)
addLines = 0;
deleteLines = 0;
for hunk in p.hunks:
#print(hunk.old_start)
#print(hunk.old_lines)
#print(hunk.new_start)
#print(hunk.new_lines)
for line in hunk.lines:
if line[0] == "+":
addLines+=1;
if line[0] == "-":
deleteLines+=1;
print("lines added" + str(addLines));
print("lines deleted" + str(deleteLines));
fileChanges+=1
print("file changed" + str(fileChanges));
childCommitNumber = commit.oid.hex;
示例3: len
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import diff [as 别名]
# print commit.commit_time
# print commit.commit_time_offset
git = sh.git.bake(_cwd='/home/heather/research/spoon-knife')
for point in history:
git.checkout(point)
# print subprocess.check_output(['ohcount', 'spoon-knife'])
git.checkout(history[0])
i = 0
while i < len(history) - 2:
t0 = base.revparse_single(history[i])
t1 = base.revparse_single(history[i+1])
diff = base.diff(t0,t1)
patches = [p for p in diff]
for patch in patches:
print 'NUM HUNKS: ' + str(len(patch.hunks))
for hunk in patch.hunks:
# print hunk.lines
totesLines = 0
totesMods = 0
for line in hunk.lines:
totesLines += 1
if line[0] == '-' or line[0] == '+':
totesMods += 1
print line
print 'TOTAL LINES: ' + str(totesLines)
print 'TOTAL MODS: ' + str(totesMods)
# print 'OLD: ' + str(hunk.old_lines)
示例4: init
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import diff [as 别名]
class prototype:
repo = "" # Path to a given repository
name = "" # Name of a repository
base = "" # Repository as defined in pygit2
# Initialization. Clones the given repository, placing it in the current directory,
# and changes to the repository directory.
def init(self, repository):
self.repo = repository
# Use regular expressions to match the last instance of a forward slash
# followed by the name of the repository, which we wish to extract, followed
# by ".git".
m = re.search('/([^/]+).git$', repository)
if m:
self.name = m.group(1)
if not os.path.isdir(self.name):
os.system('git clone ' + self.repo) # Get the repository from GitHub
self.base = Repository(self.name)
self.base.checkout('HEAD')
# Destruction. Remove the given repository from memory.
def destroy(self):
os.system('cd ' + self.name)
os.system('rm -rf ' + self.name)
# Get total LOC by given repository.
def totalRepoLOC(self):
loc = countDirLOC(self.name)
return loc
# Get total commits by a given repository
def totalRepoCommits(self):
commits = 0
for commit in self.base.walk(self.base.head.target, GIT_SORT_TOPOLOGICAL):
commits = commits + 1
return commits
# Get a list of LOC changed per commit
def locPerCommit(self):
loc = []
oldPath = os.popen('pwd')
os.chdir(self.name)
sha1 = 0
sha2 = 0
start = 1
total = self.totalRepoCommits()
# For each commit within the repository
for commit in self.base.walk(self.base.head.target, GIT_SORT_TOPOLOGICAL):
print '\r', start, '/', total,
start += 1
# Based on the SHA, use git to show the patch for that commit
sha1 = sha2
sha2 = commit.hex
if sha1 != 0:
p = os.popen('git diff --shortstat ' + sha1 + ' ' + sha2)
line = p.readline()
# line contains "# file changed, # insertions(+), # deletions(-)
# Use regular expressions to find the number of additions and deletions.
# Additions are found after ", " and before " insertion". Deletions are
# found after "(+), " and before " deletion".
m = re.search(', (.*) insertion', line)
additions = 0
deletions = 0
if m:
additions = m.group(1)
m = re.search('\(\+\), (.*) deletion', line)
if m:
deletions = m.group(1)
# Get the total and append to array
modifications = int(additions) + int(deletions)
loc.append(modifications)
os.chdir('..')
return loc
# Get a list containing the total number of line additions and deletions (including
# whitespace and comments) contained within each hunk that was changed over t
def locPerHunk(self):
loc = []
history = []
# Get the hex number for each commit within the repository
for commit in self.base.walk(self.base.head.target, GIT_SORT_TOPOLOGICAL):
sha = commit.hex
history.append(sha)
# Compare each revision in the history of the repository with the previous rev.
i = 0
while i < len(history) - 1:
t0 = self.base.revparse_single(history[i])
#.........这里部分代码省略.........
示例5: get_and_update_repo_cache
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import diff [as 别名]
def get_and_update_repo_cache(repo_path, repo_name):
cache_filename = '%s-stats.cache' % repo_name
if os.path.exists(cache_filename):
with open(cache_filename) as f:
data = load(f)
else:
data = {
'author_to_month_to_additions': defaultdict(defaultdict_int),
'author_to_month_to_deletions': defaultdict(defaultdict_int),
'author_to_month_to_changes': defaultdict(defaultdict_int),
'author_to_month_to_commits': defaultdict(defaultdict_int),
'day_to_count': defaultdict(defaultdict_int),
'change_count_by_file': defaultdict(int),
'latest_sha': None,
}
repo = Repository(repo_path)
ignored_commits = []
count = 0
for commit in repo.walk(repo.head.target, GIT_SORT_TOPOLOGICAL):
count += 1
if commit.type == GIT_OBJ_COMMIT:
if data['latest_sha'] == commit.hex:
break
try:
d = repo.diff('%s^' % commit.hex, commit)
except KeyError:
print "Commits without parent: ", commit.hex
continue
additions = d.stats.insertions
deletions = d.stats.deletions
author = author_aliases.get(commit.author.email, commit.author.email)
day = date.fromtimestamp(commit.commit_time)
data['day_to_count']['Lines'][day] += additions
data['day_to_count']['Lines'][day] -= deletions
if additions > 1000 and deletions < 5 and commit.hex not in whitelist_commits:
if commit.hex not in blacklist_commits:
ignored_commits.append(commit.hex)
# print 'WARNING: ignored %s looks like an embedding of a lib (message: %s)' % (commit.hex, commit.message)
continue
if (additions > 3000 or deletions > 3000) and commit.hex not in whitelist_commits:
if commit.hex not in blacklist_commits:
ignored_commits.append(commit.hex)
# print 'WARNING: ignored %s because it is bigger than 3k lines. Put this commit in the whitelist or the blacklist (message: %s)' % (commit.hex, commit.message)
continue
month = date(day.year, day.month, 1)
data['author_to_month_to_additions'][author][month] += additions
data['author_to_month_to_deletions'][author][month] += deletions
data['author_to_month_to_changes'][author][month] += additions + deletions
data['author_to_month_to_commits'][author][month] += 1
if data['latest_sha'] is None:
data['latest_sha'] = commit.hex
if d.patch:
for changed_path in [x for x in d.patch.split('\n') if x.startswith('+++ ') and '/dev/null' not in x]:
data['change_count_by_file'][changed_path[len('+++ ') + 1:]] += 1
with open(cache_filename, 'w') as f:
dump(data, f)
with open(repo_name + '-ignored-commits.txt', 'w') as f:
f.writelines('%s\n' % x for x in ignored_commits)
return data
示例6: print
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import diff [as 别名]
prev_commit = None
consolidate = True
if consolidate is True:
matrix = {}
else:
matrix = []
for commit in repo.walk(repo.head.target):
print(commit.message)
if prev_commit is not None:
# get the diff info
diff = repo.diff(commit, prev_commit)
# Get the string with changed info and split it
changes = diff.patch.split('\n')[5:]
try:
a = changes[3].split()
b = changes[4].split()
except:
print("last one? and I am too lasy to dump that one so here we go")
Exception
else:
try:
person_a = a[0][1:]
person_b = b[0][1:]
except:
print("new person")
示例7: GitRepo
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import diff [as 别名]
#.........这里部分代码省略.........
def get_current_root(self):
tree = self.get_current_commit()
if tree:
return self.get_tree(tree['tree'])
return None
def get_whole_tree(self, oid):
''' tree w/ json '''
if not self.__repo:
return None
result = tree_walker(self.__repo, oid)
return result
def get_blob(self, oid):
''' blob w/ json '''
if not self.__repo or not oid:
return None
try:
blob = self.__repo.get(oid)
if blob and blob.type == GIT_OBJ_BLOB:
content = blob.is_binary and None or blob.data.decode(
'utf8', 'ignore')
result = {
'id': str(blob.id),
'content': content,
'size': blob.size,
}
return result
except Exception as e:
print(e)
return None
def get_blob_by_commit(self, commit, path=None):
try:
tree = self.get_tree_by_commit(commit, path[:-1])
oid = tree[path[-1]]['id']
result = self.get_blob(oid)
return result
except Exception as e:
print(e)
return None
def get_tag(self, oid):
''' blob w/ json '''
if not self.__repo or not oid:
return None
try:
tag = self.__repo.get(oid)
if tag and tag.type == GIT_OBJ_TAG:
result = {
'id': str(oid),
'name': tag.name,
'target': str(tag.target.id),
'tagger': tag.tagger,
'message': tag.message,
}
return result
except Exception as e:
print(e)
return None
def get_patches(self, a=None, b=None):
try:
if not a:
a = 'HEAD'
if not b:
b = a + '^'
t1 = self.__repo.revparse_single(a)
t2 = self.__repo.revparse_single(b)
patches = self.__repo.diff(t1, t2)
result = []
for patch in patches:
p = {
'old_file_path': patch.old_file_path,
'new_file_path': patch.new_file_path,
'old_oid': str(patch.old_oid),
'new_oid': str(patch.new_oid),
'status': patch.status,
'similarity': patch.similarity,
'additions': patch.additions,
'deletions': patch.deletions,
'binary': patch.is_binary,
'hunks': [],
}
for hunk in patch.hunks:
h = {
'old_start': hunk.old_start,
'old_lines': hunk.old_lines,
'new_start': hunk.new_start,
'new_lines': hunk.new_lines,
'lines': hunk.lines,
}
p['hunks'].append(h)
result.append(p)
return result
except Exception as e:
print(e)
return None
示例8: process
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import diff [as 别名]
def process(repo, history):
# GET A REPO ON DISK
base = Repository(repo)
base.checkout('HEAD')
file_xsmall = 0
file_small = 0
file_medium = 0
file_large = 0
file_xlarge = 0
hunk_xsmall = 0
hunk_small = 0
hunk_medium = 0
hunk_large = 0
hunk_xlarge = 0
line_xsmall = 0
line_small = 0
line_medium = 0
line_large = 0
line_xlarge = 0
i = 0
while i < len(history) - 1:
print '\rDiff#: ' + str(i + 1) + ' of ' + str(len(history)-1),
t0 = base.revparse_single(history[i].hex)
t1 = base.revparse_single(history[i+1].hex)
try:
diff = base.diff(t0,t1)
except ValueError:
print ''
print 'Value Error'
print ''
i += 1
continue
files = [p for p in diff]
if len(files) == 1:
file_xsmall += 1
if len(files) >= 2 and len(files) <= 4:
file_small += 1
if len(files) >= 5 and len(files) <= 7:
file_medium += 1
if len(files) >= 8 and len(files) <= 10:
file_large += 1
if len(files) >= 11:
file_xlarge += 1
hunksInCommit = 0
linesInCommit = 0
for modfile in files:
hunksInCommit += len(modfile.hunks)
for hunk in modfile.hunks:
for line in hunk.lines:
if line[0] == '-' or line[0] == '+':
linesInCommit += 1
if hunksInCommit <= 1:
hunk_xsmall += 1
if hunksInCommit >= 2 and hunksInCommit <= 8:
hunk_small += 1
if hunksInCommit >= 9 and hunksInCommit <= 17:
hunk_medium += 1
if hunksInCommit >= 18 and hunksInCommit <= 26:
hunk_large += 1
if hunksInCommit >= 27:
hunk_xlarge += 1
if linesInCommit <= 5:
line_xsmall += 1
if linesInCommit >= 6 and linesInCommit <= 46:
line_small += 1
if linesInCommit >= 47 and linesInCommit <= 106:
line_medium += 1
if linesInCommit >= 107 and linesInCommit <= 166:
line_large += 1
if linesInCommit >= 167:
line_xlarge += 1
i += 1
print ''
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('-%Y-%m-%d.%H.%M.%S')
name = repo.replace('/.git', '') + st + '.txt'
output = open(name,'w')
output.write('--------- ' + repo + ' ----------' + '\n')
output.write('Number of Lines Modified:' + '\n')
output.write('x-small: ' + str( + line_xsmall) + '\n')
output.write('small: ' + str(line_small) + '\n')
output.write('medium: ' + str(line_medium) + '\n')
output.write('large: ' + str(line_large) + '\n')
output.write('x-large: ' + str(line_xlarge) + '\n')
#.........这里部分代码省略.........
示例9: GitMixin
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import diff [as 别名]
class GitMixin(object):
tag_or_remote_regex = re.compile('^refs/(tags|remotes)/(.*)')
def __init__(self):
where = GitOperations.get_repository_location(self.user, self.name)
self.ondisk = Repository(where)
def refresh(self):
creds = GitOperations.get_credentials(self.git_user, self.user)
for remote in self.ondisk.remotes:
remote.credentials = creds
remote.fetch()
# update current reference
master_ref = self.ondisk.lookup_reference('refs/heads/master')
remote_ref = self.ondisk.lookup_reference('refs/remotes/origin/master')
master_ref.set_target(remote_ref.target)
def filter_references(self, regex):
return [ref for ref in self.ondisk.listall_references()
if regex.match(ref)]
def get_commit_time(self, name):
ref = self.ondisk.revparse_single(name)
if isinstance(ref, Tag):
return ref.get_object().commit_time
if isinstance(ref, Commit):
return ref.commit_time
raise GitException('invalid reference: commit time could not be found.') # pragma: no cover
def get_latest_refs(self, count=None):
info = self.filter_references(GitMixin.tag_or_remote_regex)
refs = list(zip(info, map(self.get_commit_time, info)))
refs.sort(key=itemgetter(1), reverse=True)
def ref_info(info):
(ref, commit_time) = info
what, name = GitMixin.tag_or_remote_regex.findall(ref)[0]
return (what, name, commit_time)
refs = map(ref_info, refs)
if not count:
return refs
return islice(refs, count)
def filter_commits(self, flags=0):
all_commits = self.ondisk.walk(self.ondisk.head.target, flags)
emails = [ue.email for ue in self.user.emails.all()]
return filter(lambda commit: commit.author.email in emails, all_commits)
def get_commits(self, count=None):
all_commits = self.filter_commits(GIT_SORT_TOPOLOGICAL)
if not count:
return all_commits
return islice(all_commits, count)
def get_commit_count(self):
return len(list(self.filter_commits()))
def get_shorthand_of_branch(self, branch):
commit = self.ondisk.lookup_branch(branch)
if commit:
return commit.shorthand
return '(none)'
def get_sha1_of_branch(self, branch):
commit = self.ondisk.lookup_branch(branch)
if commit:
return str(commit.get_object().id)[:6]
return '(none)'
def get_numstat(self, commit):
diff = None
try:
previous_commit = self.ondisk.revparse_single(str(commit.id) + '^')
diff = self.ondisk.diff(previous_commit, commit)
except KeyError:
# likely we hit the very first commit.
diff = commit.tree.diff_to_tree(swap=True)
additions, deletions = 0, 0
for patch in diff:
additions += patch.additions
deletions += patch.deletions
return (len(diff), additions, deletions)
def get_first_updated(self):
all_commits = self.ondisk.walk(self.ondisk.head.target,
GIT_SORT_TIME | GIT_SORT_REVERSE)
first_commit = next(all_commits)
return first_commit.commit_time
def get_last_updated(self):
all_commits = self.ondisk.walk(self.ondisk.head.target,
GIT_SORT_TIME)
last_commit = next(all_commits)
return last_commit.commit_time
def get_file_count(self):
diff = self.ondisk.head.get_object().tree.diff_to_tree()
return len([patch.old_file_path for patch in diff])
def get_line_count(self):
#.........这里部分代码省略.........