@@ -87,46 +87,44 @@ def __init__(self):
87
87
self .log .error ("GIT version %s installed not compatible (need >= %s)" %
88
88
(self ._version ['v_str' ], self ._version ['v_min_str' ]))
89
89
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 ):
91
94
# FIXME: this function needs serious rethinking...
92
95
93
96
reponame = ''
94
- if context is None :
95
- context = formatter .context
96
- if formatter is None :
97
- formatter = context # hack
97
+
98
+ context = formatter .context
98
99
while context :
99
100
if context .resource .realm in ('source' , 'changeset' ):
100
101
reponame = context .resource .parent .id
101
102
break
102
103
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
114
104
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 )
118
107
108
+ if not repos :
109
+ raise Exception ("Repository '%s' not found" % reponame )
119
110
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" )
122
121
123
122
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 ))
127
125
128
126
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 )
130
128
131
129
#######################
132
130
# IRepositoryConnector
@@ -141,6 +139,10 @@ def get_link_resolvers(self):
141
139
"length rev sha sums should be tried to be abbreviated to"
142
140
" (must be >= 4 and <= 40)" )
143
141
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
+
144
146
_trac_user_rlookup = BoolOption ('git' , 'trac_user_rlookup' , 'false' ,
145
147
"enable reverse mapping of git email addresses to trac user ids" )
146
148
@@ -162,6 +164,12 @@ def get_repository(self, type, dir, params):
162
164
"""GitRepository factory method"""
163
165
assert type == "git"
164
166
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
+
165
173
if not self ._version :
166
174
raise TracError ("GIT backend not available" )
167
175
elif not self ._version ['v_compatible' ]:
@@ -227,6 +235,7 @@ def match_property(self, name, mode):
227
235
def render_property (self , name , mode , context , props ):
228
236
229
237
def sha_link (sha ):
238
+ # sha is assumed to be a non-abbreviated 40-chars sha id
230
239
try :
231
240
reponame = context .resource .parent .id
232
241
repos = self .env .get_repository (reponame )
0 commit comments