mercurial/hgweb/hgweb_mod.py
changeset 3406 03e7e8958a27
parent 3405 970b2d6de3b3
child 3409 1ae738bacf74
equal deleted inserted replaced
3405:970b2d6de3b3 3406:03e7e8958a27
    25         p = p[:-1]
    25         p = p[:-1]
    26     up = os.path.dirname(p)
    26     up = os.path.dirname(p)
    27     if up == "/":
    27     if up == "/":
    28         return "/"
    28         return "/"
    29     return up + "/"
    29     return up + "/"
       
    30 
       
    31 def revnavgen(pos, pagelen, limit):
       
    32     def seq(factor, limit=None):
       
    33         if limit:
       
    34             yield limit
       
    35             if limit >= 20 and limit <= 40:
       
    36                 yield 50
       
    37         else:
       
    38             yield 1 * factor
       
    39             yield 3 * factor
       
    40         for f in seq(factor * 10):
       
    41             yield f
       
    42 
       
    43     def nav(**map):
       
    44         l = []
       
    45         last = 0
       
    46         for f in seq(1, pagelen):
       
    47             if f < pagelen or f <= last:
       
    48                 continue
       
    49             if f > limit:
       
    50                 break
       
    51             last = f
       
    52             if pos + f < limit:
       
    53                 l.append(("+%d" % f, pos + f))
       
    54             if pos - f >= 0:
       
    55                 l.insert(0, ("-%d" % f, pos - f))
       
    56 
       
    57         yield {"label": "(0)", "rev": 0}
       
    58 
       
    59         for label, rev in l:
       
    60             yield {"label": label, "rev": rev}
       
    61 
       
    62         yield {"label": "tip", "rev": "tip"}
       
    63 
       
    64     return nav
    30 
    65 
    31 class hgweb(object):
    66 class hgweb(object):
    32     def __init__(self, repo, name=None):
    67     def __init__(self, repo, name=None):
    33         if type(repo) == type(""):
    68         if type(repo) == type(""):
    34             self.repo = hg.repository(ui.ui(), repo)
    69             self.repo = hg.repository(ui.ui(), repo)
   147             tn = None
   182             tn = None
   148             yield diffblock(mdiff.unidiff(to, date1, tn, date2, f,
   183             yield diffblock(mdiff.unidiff(to, date1, tn, date2, f,
   149                                           opts=diffopts), f, tn)
   184                                           opts=diffopts), f, tn)
   150 
   185 
   151     def changelog(self, ctx, shortlog=False):
   186     def changelog(self, ctx, shortlog=False):
   152         pos = ctx.rev()
       
   153         def changenav(**map):
       
   154             def seq(factor, maxchanges=None):
       
   155                 if maxchanges:
       
   156                     yield maxchanges
       
   157                     if maxchanges >= 20 and maxchanges <= 40:
       
   158                         yield 50
       
   159                 else:
       
   160                     yield 1 * factor
       
   161                     yield 3 * factor
       
   162                 for f in seq(factor * 10):
       
   163                     yield f
       
   164 
       
   165             l = []
       
   166             last = 0
       
   167             maxchanges = shortlog and self.maxshortchanges or self.maxchanges
       
   168             for f in seq(1, maxchanges):
       
   169                 if f < maxchanges or f <= last:
       
   170                     continue
       
   171                 if f > count:
       
   172                     break
       
   173                 last = f
       
   174                 r = "%d" % f
       
   175                 if pos + f < count:
       
   176                     l.append(("+" + r, pos + f))
       
   177                 if pos - f >= 0:
       
   178                     l.insert(0, ("-" + r, pos - f))
       
   179 
       
   180             yield {"rev": 0, "label": "(0)"}
       
   181 
       
   182             for label, rev in l:
       
   183                 yield {"label": label, "rev": rev}
       
   184 
       
   185             yield {"label": "tip", "rev": "tip"}
       
   186 
       
   187         def changelist(**map):
   187         def changelist(**map):
   188             parity = (start - end) & 1
   188             parity = (start - end) & 1
   189             cl = self.repo.changelog
   189             cl = self.repo.changelog
   190             l = [] # build a list in forward order for efficiency
   190             l = [] # build a list in forward order for efficiency
   191             for i in range(start, end):
   191             for i in range(start, end):
   208                 yield e
   208                 yield e
   209 
   209 
   210         maxchanges = shortlog and self.maxshortchanges or self.maxchanges
   210         maxchanges = shortlog and self.maxshortchanges or self.maxchanges
   211         cl = self.repo.changelog
   211         cl = self.repo.changelog
   212         count = cl.count()
   212         count = cl.count()
       
   213         pos = ctx.rev()
   213         start = max(0, pos - maxchanges + 1)
   214         start = max(0, pos - maxchanges + 1)
   214         end = min(count, start + maxchanges)
   215         end = min(count, start + maxchanges)
   215         pos = end - 1
   216         pos = end - 1
       
   217 
       
   218         changenav = revnavgen(pos, maxchanges, count)
   216 
   219 
   217         yield self.t(shortlog and 'shortlog' or 'changelog',
   220         yield self.t(shortlog and 'shortlog' or 'changelog',
   218                      changenav=changenav,
   221                      changenav=changenav,
   219                      node=hex(cl.tip()),
   222                      node=hex(cl.tip()),
   220                      rev=pos, changesets=count, entries=changelist,
   223                      rev=pos, changesets=count, entries=changelist,
   304 
   307 
   305     def filelog(self, fctx):
   308     def filelog(self, fctx):
   306         f = fctx.path()
   309         f = fctx.path()
   307         fl = fctx.filelog()
   310         fl = fctx.filelog()
   308         count = fl.count()
   311         count = fl.count()
       
   312         pagelen = self.maxshortchanges
   309 
   313 
   310         def entries(**map):
   314         def entries(**map):
       
   315             pos = fctx.filerev()
       
   316             start = max(0, pos - pagelen + 1) 
       
   317             end = min(count, start + pagelen)
       
   318             pos = end - 1
   311             l = []
   319             l = []
   312             parity = (count - 1) & 1
   320             parity = (count - 1) & 1
   313 
   321 
   314             for i in range(count):
   322             for i in range(start, end):
   315                 ctx = fctx.filectx(i)
   323                 ctx = fctx.filectx(i)
   316                 n = fl.node(i)
   324                 n = fl.node(i)
   317 
   325 
   318                 l.insert(0, {"parity": parity,
   326                 l.insert(0, {"parity": parity,
   319                              "filerev": i,
   327                              "filerev": i,
   328                 parity = 1 - parity
   336                 parity = 1 - parity
   329 
   337 
   330             for e in l:
   338             for e in l:
   331                 yield e
   339                 yield e
   332 
   340 
   333         yield self.t("filelog", file=f, node=hex(fctx.node()), entries=entries)
   341         nav = revnavgen(fctx.filerev(), self.maxshortchanges, count)
       
   342         yield self.t("filelog", file=f, node=hex(fctx.node()), nav=nav,
       
   343                      entries=entries)
   334 
   344 
   335     def filerevision(self, fctx):
   345     def filerevision(self, fctx):
   336         f = fctx.path()
   346         f = fctx.path()
   337         text = fctx.data()
   347         text = fctx.data()
   338         fl = fctx.filelog()
   348         fl = fctx.filelog()