Skip to content

Instantly share code, notes, and snippets.

@keis
Created November 25, 2014 15:00
Show Gist options
  • Save keis/ab22f0c8729720119025 to your computer and use it in GitHub Desktop.
Save keis/ab22f0c8729720119025 to your computer and use it in GitHub Desktop.
dulwich find commit
#!/usr/bin/env python2
from dulwich.repo import Repo
from collections import defaultdict, deque
def changes_file(key):
def matches(commit, entry):
changes = entry.changes()
if len(commit.parents) < 2:
changes = [changes]
for base in changes:
for ch in base:
if ch.new.path == key:
return True
return False
return matches
def find_merge(repo, children, id):
queue = deque(children[id])
while len(queue) > 0:
commit = repo[queue.popleft()]
if len(commit.parents) > 1:
return commit
queue.append(commit.parents[0])
def find_commits(repo, key):
children = defaultdict(list)
for entry in repo.get_walker():
commit = entry.commit
if key(commit, entry):
yield commit, find_merge(repo, children, commit.id)
for parent in commit.parents:
children[parent].append(commit.id)
repo = Repo('/home/keis/git/data/.git')
key = 'somefile.txt'
for commit, merge in find_commits(repo, changes_file(key)):
print '**'
print merge
print '--'
print commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment