diff --git a/klaus.py b/klaus.py index f4624c14fd894de02f2f2dea5ebd6dee0a229249..e7096931f0cfeb24d26e1ebcfdb2fef3789dfa85 100644 --- a/klaus.py +++ b/klaus.py @@ -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} diff --git a/repo.py b/repo.py index 7534d4e21604002e6c4369034280f14c303a4d73..1f795f849a49052e7075edbb467406143634728f 100644 --- a/repo.py +++ b/repo.py @@ -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):