Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 23afc8e

Browse files
committed
Add support for abbreviated wiki sha ids
This introduces a new option `wiki_shortrev_len`, which defines the minimum length for which hex-strings are interpreted as sha ids.
1 parent 3551f77 commit 23afc8e

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

tracext/git/git_fs.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,46 +87,44 @@ def __init__(self):
8787
self.log.error("GIT version %s installed not compatible (need >= %s)" %
8888
(self._version['v_str'], self._version['v_min_str']))
8989

90-
def _format_sha_link(self, formatter, ns, sha, label, context=None):
90+
#######################
91+
# IWikiSyntaxProvider
92+
93+
def _format_sha_link(self, formatter, sha, label):
9194
# FIXME: this function needs serious rethinking...
9295

9396
reponame = ''
94-
if context is None:
95-
context = formatter.context
96-
if formatter is None:
97-
formatter = context # hack
97+
98+
context = formatter.context
9899
while context:
99100
if context.resource.realm in ('source', 'changeset'):
100101
reponame = context.resource.parent.id
101102
break
102103
context = context.parent
103-
repos = self.env.get_repository(reponame)
104-
if repos:
105-
try:
106-
changeset = repos.get_changeset(sha)
107-
return tag.a(label, class_="changeset",
108-
title=shorten_line(changeset.message),
109-
href=formatter.href.changeset(sha, reponame))
110-
except Exception, e:
111-
errmsg = to_unicode(e)
112-
else:
113-
errmsg = "Repository '%s' not found" % reponame
114104

115-
return tag.a(label, class_="missing changeset",
116-
#href=formatter.href.changeset(sha, reponame),
117-
title=to_unicode(errmsg), rel="nofollow")
105+
try:
106+
repos = self.env.get_repository(reponame)
118107

108+
if not repos:
109+
raise Exception("Repository '%s' not found" % reponame)
119110

120-
#######################
121-
# IWikiSyntaxProvider
111+
sha = repos.normalize_rev(sha) # in case it was abbreviated
112+
changeset = repos.get_changeset(sha)
113+
return tag.a(label, class_="changeset",
114+
title=shorten_line(changeset.message),
115+
href=formatter.href.changeset(sha, repos.reponame))
116+
except Exception, e:
117+
errmsg = to_unicode(e)
118+
119+
return tag.a(label, class_="missing changeset",
120+
title=to_unicode(errmsg), rel="nofollow")
122121

123122
def get_wiki_syntax(self):
124-
yield (r'(?:\b|!)[0-9a-fA-F]{40,40}\b',
125-
lambda fmt, sha, match:
126-
self._format_sha_link(fmt, 'changeset', sha, sha))
123+
yield (r'(?:\b|!)r?[0-9a-fA-F]{%d,40}\b' % self._wiki_shortrev_len,
124+
lambda fmt, sha, match: self._format_sha_link(fmt, sha.startswith('r') and sha[1:] or sha, sha))
127125

128126
def get_link_resolvers(self):
129-
yield ('sha', self._format_sha_link)
127+
yield 'sha', lambda fmt, _, sha, label, match=None: self._format_sha_link(fmt, sha, label)
130128

131129
#######################
132130
# IRepositoryConnector
@@ -141,6 +139,10 @@ def get_link_resolvers(self):
141139
"length rev sha sums should be tried to be abbreviated to"
142140
" (must be >= 4 and <= 40)")
143141

142+
_wiki_shortrev_len = IntOption('git', 'wiki_shortrev_len', 40,
143+
"minimum length of hex-string for which auto-detection as sha id is performed"
144+
" (must be >= 4 and <= 40)")
145+
144146
_trac_user_rlookup = BoolOption('git', 'trac_user_rlookup', 'false',
145147
"enable reverse mapping of git email addresses to trac user ids")
146148

@@ -162,6 +164,12 @@ def get_repository(self, type, dir, params):
162164
"""GitRepository factory method"""
163165
assert type == "git"
164166

167+
if not (4 <= self._shortrev_len <= 40):
168+
raise TracError("shortrev_len must be withing [4..40]")
169+
170+
if not (4 <= self._wiki_shortrev_len <= 40):
171+
raise TracError("wiki_shortrev_len must be withing [4..40]")
172+
165173
if not self._version:
166174
raise TracError("GIT backend not available")
167175
elif not self._version['v_compatible']:
@@ -227,6 +235,7 @@ def match_property(self, name, mode):
227235
def render_property(self, name, mode, context, props):
228236

229237
def sha_link(sha):
238+
# sha is assumed to be a non-abbreviated 40-chars sha id
230239
try:
231240
reponame = context.resource.parent.id
232241
repos = self.env.get_repository(reponame)

0 commit comments

Comments
 (0)