Permalink
Browse files

Add `Branches` property to changeset views.

The `Branches` property is rendered as links to the reachable
branch tips labeled by the branch name.

While at it, refactor joining of rendered property list items with new
`intersperse()` helper function.
  • Loading branch information...
1 parent fc6bf2c commit a6c16ead09fbc7cd8af492dcf8627b885dd26cf0 @hvr committed Oct 17, 2010
Showing with 47 additions and 17 deletions.
  1. +47 −17 tracext/git/git_fs.py
View
@@ -57,6 +57,18 @@ def _last_iterable(iterable):
v = nextv
yield True, v
+def intersperse(sep, iterable):
+ """
+ The 'intersperse' generator takes an element and an iterable and
+ intersperses that element between the elements of the iterable.
+
+ inspired by Haskell's Data.List.intersperse
+ """
+
+ for i, item in enumerate(iterable):
+ if i: yield sep
+ yield item
+
# helper
def _parse_user_time(s):
"""
@@ -228,19 +240,23 @@ def match_property(self, name, mode):
# default renderer has priority 1
return (name in ('Parents',
'Children',
+ 'Branches',
'git-committer',
'git-author',
) and mode == 'revprop') and 4 or 0
def render_property(self, name, mode, context, props):
- def sha_link(sha):
+ def sha_link(sha, label=None):
# sha is assumed to be a non-abbreviated 40-chars sha id
try:
reponame = context.resource.parent.id
repos = self.env.get_repository(reponame)
cset = repos.get_changeset(sha)
- return tag.a(repos.display_rev(sha), class_="changeset",
+ if label is None:
+ label = repos.display_rev(sha)
+
+ return tag.a(label, class_="changeset",
title=shorten_line(cset.message),
href=context.href.changeset(sha, repos.reponame))
@@ -249,24 +265,30 @@ def sha_link(sha):
return tag.a(sha, class_="missing changeset",
title=to_unicode(errmsg), rel="nofollow")
- if name in ('Parents', 'Children'):
+ if name == 'Branches':
+ branches = props[name]
+
+ # simple non-merge commit
+ return tag(*intersperse(', ', (sha_link(rev, label) for label, rev in branches)))
+
+ elif name in ('Parents', 'Children'):
revs = props[name] # list of commit ids
if name == 'Parents' and len(revs) > 1:
# we got a merge...
current_sha = context.resource.id
reponame = context.resource.parent.id
- parent_links = [(sha_link(rev),
- ' (',
- tag.a('diff',
- title="Diff against this parent (show the changes merged from the other parents)",
- href=context.href.changeset(current_sha, reponame, old=rev)),
- ')')
- for rev in revs]
+ parent_links = intersperse(', ', \
+ ((sha_link(rev),
+ ' (',
+ tag.a('diff',
+ title="Diff against this parent (show the changes merged from the other parents)",
+ href=context.href.changeset(current_sha, reponame, old=rev)),
+ ')')
+ for rev in revs))
- return tag([(parent, ', ') for parent in parent_links[:-1]],
- parent_links[-1],
+ return tag(list(parent_links),
tag.br(),
tag.span(tag("Note: this is a ", tag.strong('merge'), " changeset, "
"the changes displayed below correspond "
@@ -278,10 +300,9 @@ def sha_link(sha):
class_='hint'))
# simple non-merge commit
- return tag([tag(sha_link(rev), ', ') for rev in revs[:-1]],
- sha_link(revs[-1]))
+ return tag(*intersperse(', ', map(sha_link, revs)))
- if name in ('git-committer', 'git-author'):
+ elif name in ('git-committer', 'git-author'):
user_, time_ = props[name]
_str = "%s (%s)" % (Chrome(self.env).format_author(context.req, user_),
format_datetime(time_, tzinfo=context.req.tz))
@@ -520,6 +541,7 @@ def get_last_modified(self):
return ts
+
class GitChangeset(Changeset):
"""
A Git changeset in the Git repository.
@@ -567,16 +589,24 @@ def __init__(self, repos, sha):
def get_properties(self):
properties = {}
+
if 'parent' in self.props:
properties['Parents'] = self.props['parent']
+
if 'children' in self.props:
properties['Children'] = self.props['children']
+
if 'committer' in self.props:
properties['git-committer'] = \
- _parse_user_time(self.props['committer'][0])
+ _parse_user_time(self.props['committer'][0])
+
if 'author' in self.props:
properties['git-author'] = \
- _parse_user_time(self.props['author'][0])
+ _parse_user_time(self.props['author'][0])
+
+ branches = list(self.repos.git.get_branch_contains(self.rev, resolve=True))
+ if branches:
+ properties['Branches'] = branches
return properties

0 comments on commit a6c16ea

Please sign in to comment.