[PATCH 3 of 3] filectx.annotate: return filectx for each line instead of rev

Matt Mackall mpm at selenic.com
Fri Sep 22 04:27:36 UTC 2006


On Thu, Sep 21, 2006 at 07:23:11PM -0700, Brendan Cully wrote:
> +    def getname(fctx):
> +        return trimuser(ui, fctx.user(), fctx.rev(), ucache)

I think this can just be ui.shortname(fctx.user()). ucache can go.
>  
>      dcache = {}
> -    def getdate(rev):
> -        datestr = dcache.get(rev)
> +    def getdate(fctx):
> +        datestr = dcache.get(fctx)
>          if datestr is None:
> -            datestr = dcache[rev] = util.datestr(repo.changectx(rev).date())
> +            datestr = dcache[fctx] = util.datestr(fctx.date())

And here, I think we can skip the dcache too.

> -    opmap = [['user', getname], ['number', str], ['changeset', getnode],
> +    opmap = [['user', getname], ['number', lambda x: str(x.rev())],
> +             ['changeset', lambda x: short(x.node())],
>               ['date', getdate]]

Which means they can be more lambdas here.

>      if not opts['user'] and not opts['changeset'] and not opts['date']:
>          opts['number'] = 1
> diff -r 0b94209691a4 -r 9470e405efd6 mercurial/context.py
> --- a/mercurial/context.py	Thu Sep 21 19:18:17 2006 -0700
> +++ b/mercurial/context.py	Thu Sep 21 19:21:01 2006 -0700
> @@ -138,4 +138,15 @@ class filectx(object):
>                           filelog=self._filelog) for x in c ]
>  
>      def annotate(self):
> -        return self._filelog.annotate(self._filenode)
> +        fcache = {}
> +        def getctx(rev):
> +            ctx = fcache.get(rev)
> +            if not ctx:
> +                ctx = filectx(self._repo, self._path, changeid=rev,
> +                              filelog=self._filelog)
> +                fcache[rev] = ctx
> +            return ctx

I think the best pattern here is:

if x not in cache:
  cache[x] = func(x)
y = cache[x]

Another pattern is:

try:
  y = cache[x]
except:
  cache[x] = func(x)
  y = cache[x]

..which may win if x is usually in cache.

-- 
Mathematics is the supreme nostalgia of our time.



More information about the Mercurial mailing list