Skip to content
Snippets Groups Projects
Commit 1520da38 authored by Jonas Haag's avatar Jonas Haag
Browse files

Refactored the 'get_branch_or_commit' into the RepoWrapper class.

parent b48ea431
No related branches found
No related tags found
No related merge requests found
......@@ -87,12 +87,6 @@ def get_commit(repo, id):
except KeyError:
raise HttpError(404, '"%s" has no commit "%s"' % (repo.name, id))
def get_branch_or_commit(repo, id):
try:
return repo.get_branch(id)
except KeyError:
return get_commit(repo, id)
def get_tree_or_blob_url(repo, commit_id, tree_entry):
if tree_entry.mode & stat.S_IFDIR:
view = 'view_tree'
......@@ -113,7 +107,7 @@ def view_repo(env, repo):
@app.route('/:repo:/tree/:commit_id:/(?P<path>.*)')
def view_tree(env, repo, commit_id, path):
repo = get_repo(repo)
commit = get_branch_or_commit(repo, commit_id)
commit = repo.get_branch_or_commit(commit_id)
files = ((name, get_tree_or_blob_url(repo, commit_id, entry))
for name, entry in repo.listdir(commit, path))
return {'repo' : repo, 'commit_id' : commit_id,
......@@ -122,7 +116,7 @@ def view_tree(env, repo, commit_id, path):
@app.route('/:repo:/blob/:commit_id:/(?P<path>.*)')
def view_blob(env, repo, commit_id, path):
repo = get_repo(repo)
commit = get_branch_or_commit(repo, commit_id)
commit = repo.get_branch_or_commit(commit_id)
directory, filename = os.path.split(path)
blob = repo[repo.get_tree(commit, directory)[filename][1]]
return {'repo' : repo, 'blob' : blob, 'path' : path, 'commit_id' : commit_id}
......
......@@ -6,18 +6,26 @@ import difflib
import dulwich, dulwich.patch
class RepoWrapper(dulwich.repo.Repo):
def get_branch(self, name=None):
if name is None:
name = 'master'
def get_branch_or_commit(self, id):
try:
return self[id]
except KeyError:
return self.get_branch(id)
def get_branch(self, name):
return self['refs/heads/'+name]
def history(self, branch=None, max_commits=None):
def get_default_branch(self):
return self.get_branch('master')
def history(self, commit=None, max_commits=None):
if commit is None:
commit = self.get_default_branch()
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]]
while max_commits and commit.parents:
yield commit
commit = self[commit.parents[0]]
max_commits -= 1
def get_tree(self, commit, path):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment