mercurial/hgweb/webutil.py
changeset 7310 bd522d09d5e3
parent 7294 f933076a19fc
child 7311 de9c87fe1620
equal deleted inserted replaced
7309:e74a9173c2d7 7310:bd522d09d5e3
     5 #
     5 #
     6 # This software may be used and distributed according to the terms
     6 # This software may be used and distributed according to the terms
     7 # of the GNU General Public License, incorporated herein by reference.
     7 # of the GNU General Public License, incorporated herein by reference.
     8 
     8 
     9 import os
     9 import os
       
    10 from mercurial import match, patch
    10 from mercurial.node import hex, nullid
    11 from mercurial.node import hex, nullid
    11 from mercurial.repo import RepoError
    12 from mercurial.repo import RepoError
    12 from mercurial import util
    13 from mercurial import util
    13 
    14 
    14 def up(p):
    15 def up(p):
   139         fctx = repo[changeid][path]
   140         fctx = repo[changeid][path]
   140     except RepoError:
   141     except RepoError:
   141         fctx = repo.filectx(path, fileid=changeid)
   142         fctx = repo.filectx(path, fileid=changeid)
   142 
   143 
   143     return fctx
   144     return fctx
       
   145 
       
   146 def diffs(repo, tmpl, ctx, files, parity):
       
   147 
       
   148     def countgen():
       
   149         start = 1
       
   150         while True:
       
   151             yield start
       
   152             start += 1
       
   153 
       
   154     blockcount = countgen()
       
   155     def prettyprintlines(diff):
       
   156         blockno = blockcount.next()
       
   157         for lineno, l in enumerate(diff.splitlines(True)):
       
   158             lineno = "%d.%d" % (blockno, lineno + 1)
       
   159             if l.startswith('+'):
       
   160                 ltype = "difflineplus"
       
   161             elif l.startswith('-'):
       
   162                 ltype = "difflineminus"
       
   163             elif l.startswith('@'):
       
   164                 ltype = "difflineat"
       
   165             else:
       
   166                 ltype = "diffline"
       
   167             yield tmpl(ltype,
       
   168                        line=l,
       
   169                        lineid="l%s" % lineno,
       
   170                        linenumber="% 8s" % lineno)
       
   171 
       
   172     if files:
       
   173         m = match.exact(repo.root, repo.getcwd(), files)
       
   174     else:
       
   175         m = match.always(repo.root, repo.getcwd())
       
   176 
       
   177     diffopts = patch.diffopts(repo.ui, untrusted=True)
       
   178     parents = ctx.parents()
       
   179     node1 = parents and parents[0].node() or nullid
       
   180     node2 = ctx.node()
       
   181 
       
   182     block = []
       
   183     for chunk in patch.diff(repo, node1, node2, m, opts=diffopts):
       
   184         if chunk.startswith('diff') and block:
       
   185             yield tmpl('diffblock', parity=parity.next(),
       
   186                        lines=prettyprintlines(''.join(block)))
       
   187             block = []
       
   188         if chunk.startswith('diff'):
       
   189             chunk = ''.join(chunk.splitlines(True)[1:])
       
   190         block.append(chunk)
       
   191     yield tmpl('diffblock', parity=parity.next(),
       
   192                lines=prettyprintlines(''.join(block)))