comparison mercurial/templatefuncs.py @ 37210:2a2ce93e12f4

templatefuncs: add mailmap template function This commit adds a template function to support the .mailmap file in Mercurial repositories. The .mailmap file comes from git, and can be used to map new emails and names for old commits. The general use case is that someone may change their name or author commits under different emails and aliases, which would make these commits appear as though they came from different persons. The file allows you to specify the correct name that should be used in place of the author field specified in the commit. The mailmap file has 4 possible formats used to map old "commit" names to new "proper" names: 1. <proper@email.com> <commit@email.com> 2. Proper Name <commit@email.com> 3. Proper Name <proper@email.com> <commit@email.com> 4. Proper Name <proper@email.com> Commit Name <commit@email.com> Essentially there is a commit email present in each mailmap entry, that maps to either an updated name, email, or both. The final possible format allows commits authored by a person who used both an old name and an old email to map to a new name and email. To parse the file, we split by spaces and build a name out of every element that does not start with "<". Once we find an element that does start with "<" we concatenate all the name elements that preceded and add that as a parsed name. We then add the email as the first parsed email. We repeat the process until the end of the line, or a comment is found. We will be left with all parsed names in a list, and all parsed emails in a list, with the 0 index being the proper values and the 1 index being the commit values (if they were specified in the entry). The commit values are added as the keys to a dict, and with the proper fields as the values. The mapname function takes the mapping object and the commit author field and attempts to look for a corresponding entry. To do so we try (commit name, commit email) first, and if no results are returned then (None, commit email) is also looked up. This is due to format 4 from above, where someone may have a mailmap entry with both name and email, and if they don't it is possible they have an entry that uses only the commit email. Differential Revision: https://phab.mercurial-scm.org/D2904
author Connor Sheehan <sheehan@mozilla.com>
date Mon, 19 Mar 2018 11:16:21 -0400
parents 0fb28899e81a
children 67efce231633
comparison
equal deleted inserted replaced
37209:2208149c4b8e 37210:2a2ce93e12f4
24 templatefilters, 24 templatefilters,
25 templatekw, 25 templatekw,
26 templateutil, 26 templateutil,
27 util, 27 util,
28 ) 28 )
29 from .utils import dateutil 29 from .utils import (
30 dateutil,
31 stringutil,
32 )
30 33
31 evalrawexp = templateutil.evalrawexp 34 evalrawexp = templateutil.evalrawexp
32 evalfuncarg = templateutil.evalfuncarg 35 evalfuncarg = templateutil.evalfuncarg
33 evalboolean = templateutil.evalboolean 36 evalboolean = templateutil.evalboolean
34 evalinteger = templateutil.evalinteger 37 evalinteger = templateutil.evalinteger
163 ui = context.resource(mapping, 'ui') 166 ui = context.resource(mapping, 'ui')
164 node = evalstring(context, mapping, args[0]) 167 node = evalstring(context, mapping, args[0])
165 if ui.debugflag: 168 if ui.debugflag:
166 return node 169 return node
167 return templatefilters.short(node) 170 return templatefilters.short(node)
171
172 @templatefunc('mailmap(author)')
173 def mailmap(context, mapping, args):
174 """Return the author, updated according to the value
175 set in the .mailmap file"""
176 if len(args) != 1:
177 raise error.ParseError(_("mailmap expects one argument"))
178
179 author = evalfuncarg(context, mapping, args[0])
180
181 cache = context.resource(mapping, 'cache')
182 repo = context.resource(mapping, 'repo')
183
184 if 'mailmap' not in cache:
185 data = repo.wvfs.tryread('.mailmap')
186 cache['mailmap'] = stringutil.parsemailmap(data)
187
188 return stringutil.mapname(cache['mailmap'], author) or author
168 189
169 @templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])', 190 @templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])',
170 argspec='text width fillchar left') 191 argspec='text width fillchar left')
171 def pad(context, mapping, args): 192 def pad(context, mapping, args):
172 """Pad text with a 193 """Pad text with a