Changeset - 0c00fbaff55a
[Not reviewed]
default
0 3 0
Marcin Kuzminski - 16 years ago 2010-05-15 19:53:23
marcin@python-works.com
Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
3 files changed with 21 insertions and 10 deletions:
0 comments (0 inline, 0 general)
pylons_app/controllers/files.py
Show inline comments
 
@@ -38,71 +38,75 @@ class FilesController(BaseController):
 
        
 
        try:
 
            cur_rev = repo.get_changeset(revision).revision
 
            prev_rev = repo.get_changeset(get_prev_rev(cur_rev)).raw_id
 
            next_rev = repo.get_changeset(get_next_rev(cur_rev)).raw_id
 
                    
 
            c.url_prev = url('files_home', repo_name=c.repo_name,
 
                             revision=prev_rev, f_path=f_path) 
 
            c.url_next = url('files_home', repo_name=c.repo_name,
 
                             revision=next_rev, f_path=f_path)   
 
                    
 
            c.changeset = repo.get_changeset(revision)
 
            try:
 
                c.file_msg = c.changeset.get_file_message(f_path)
 
            except:
 
                c.file_msg = None
 
                        
 
            c.cur_rev = c.changeset.raw_id
 
            c.rev_nr = c.changeset.revision
 
            c.files_list = c.changeset.get_node(f_path)
 
            c.file_history = self._get_history(repo, c.files_list, f_path)
 
            
 
        except (RepositoryError, ChangesetError):
 
            c.files_list = None
 
        
 
        return render('files/files.html')
 

	
 
    def rawfile(self, repo_name, revision, f_path):
 
        hg_model = HgModel()
 
        c.repo = hg_model.get_repo(c.repo_name)
 
        file_node = c.repo.get_changeset(revision).get_node(f_path)
 
        response.headers['Content-type'] = file_node.mimetype
 
        response.headers['Content-disposition'] = 'attachment; filename=%s' \
 
                                                    % f_path.split('/')[-1] 
 
        return file_node.content
 
    
 
    def archivefile(self, repo_name, revision, fileformat):
 
        return '%s %s %s' % (repo_name, revision, fileformat)
 
    
 
    def diff(self, repo_name, f_path):
 
        hg_model = HgModel()
 
        diff1 = request.GET.get('diff1')
 
        diff2 = request.GET.get('diff2')
 
        c.no_changes = diff1 == diff2
 
        c.f_path = f_path
 
        c.repo = hg_model.get_repo(c.repo_name)
 
        c.changeset_1 = c.repo.get_changeset(diff1)
 
        c.changeset_2 = c.repo.get_changeset(diff2)
 
        f1 = c.changeset_1.get_node(f_path)
 
        f2 = c.changeset_2.get_node(f_path)
 
        
 
        c.file_1 = c.changeset_1.get_file_content(f_path)
 
        c.file_2 = c.changeset_2.get_file_content(f_path)
 
        c.diff1 = 'r%s:%s' % (c.changeset_1.revision, c.changeset_1._short)
 
        c.diff2 = 'r%s:%s' % (c.changeset_2.revision, c.changeset_2._short)
 

	
 
        d2 = unified_diff(c.file_1.splitlines(1), c.file_2.splitlines(1))
 
        c.diff_files = render_udiff(udiff=d2)
 
        f_udiff = unified_diff(f1.content.splitlines(True),
 
                               f2.content.splitlines(True),
 
                               f1.name,
 
                               f2.name)
 
        
 
        c.diff_files = render_udiff(udiff=f_udiff, differ='difflib')
 
        print c.diff_files
 
        if len(c.diff_files) < 1:
 
            c.no_changes = True
 
        return render('files/file_diff.html')
 
    
 
    def _get_history(self, repo, node, f_path):
 
        from vcs.nodes import NodeKind
 
        if not node.kind is NodeKind.FILE:
 
            return []
 
        changesets = node.history
 
        hist_l = []
 
        for chs in changesets:
 
            n_desc = 'r%s:%s' % (chs.revision, chs._short)
 
            hist_l.append((chs._short, n_desc,))
 
        return hist_l
pylons_app/lib/differ.py
Show inline comments
 
# -*- coding: utf-8 -*-
 
# original copyright: 2007-2008 by Armin Ronacher
 
# licensed under the BSD license.
 

	
 
import re, difflib
 

	
 
def render_udiff(udiff, differ='udiff'):
 
    """Renders the udiff into multiple chunks of nice looking tables.
 
    The return value is a list of those tables.
 
    """
 
    return DiffProcessor(udiff, differ).prepare()
 

	
 
class DiffProcessor(object):
 
    """Give it a unified diff and it returns a list of the files that were
 
    mentioned in the diff together with a dict of meta information that
 
    can be used to render it in a HTML template.
 
    """
 
    _chunk_re = re.compile(r'@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)')
 

	
 
    def __init__(self, udiff, differ):
 
        """
 
        :param udiff:   a text in udiff format
 
        """
 
        if isinstance(udiff, basestring):
 
            udiff = udiff.splitlines(1)
 
        
 
        self.lines = map(self.escaper, udiff)
 
        
 
        # Select a differ.
 
        if differ == 'difflib':
 
            self.differ = self._highlight_line_difflib
 
        else:
 
            self.differ = self._highlight_line_udiff
 
            
 

	
 
    def escaper(self, string):
 
        return string.replace('<', '&lt;').replace('>', '&gt;')
 

	
 
    def _extract_rev(self, line1, line2):
 
        """Extract the filename and revision hint from a line."""
 
        try:
 
            if line1.startswith('--- ') and line2.startswith('+++ '):
 
                filename, old_rev = line1[4:].split(None, 1)
 
                new_rev = line2[4:].split(None, 1)[1]
 
                return filename, 'old', 'new'
 
                l1 = line1[4:].split(None, 1)
 
                old_filename = l1[0] if len(l1) >= 1 else None
 
                old_rev = l1[1] if len(l1) == 2 else 'old'
 
                
 
                l2 = line1[4:].split(None, 1)
 
                new_filename = l2[0] if len(l2) >= 1 else None
 
                new_rev = l2[1] if len(l2) == 2 else 'new'
 
                                 
 
                return old_filename, new_rev, old_rev
 
        except (ValueError, IndexError):
 
            pass
 
        
 
        return None, None, None
 

	
 
    def _highlight_line_difflib(self, line, next):
 
        """Highlight inline changes in both lines."""
 
        
 
        if line['action'] == 'del':
 
            old, new = line, next
 
        else:
 
            old, new = next, line
 
        
 
        oldwords = re.split(r'(\W)', old['line'])
 
        newwords = re.split(r'(\W)', new['line'])
 
        
 
        sequence = difflib.SequenceMatcher(None, oldwords, newwords)
 
        
 
        oldfragments, newfragments = [], []
 
        for tag, i1, i2, j1, j2 in sequence.get_opcodes():
 
            oldfrag = ''.join(oldwords[i1:i2])
 
            newfrag = ''.join(newwords[j1:j2])
 
            if tag != 'equal':
 
                if oldfrag:
 
                    oldfrag = '<del>%s</del>' % oldfrag
 
                if newfrag:
 
                    newfrag = '<ins>%s</ins>' % newfrag
 
            oldfragments.append(oldfrag)
 
            newfragments.append(newfrag)
 
        
 
        old['line'] = "".join(oldfragments)
 
        new['line'] = "".join(newfragments)
 

	
 
    def _highlight_line_udiff(self, line, next):
 
        """Highlight inline changes in both lines."""
 
        start = 0
 
        limit = min(len(line['line']), len(next['line']))
 
        while start < limit and line['line'][start] == next['line'][start]:
 
            start += 1
 
        end = -1
 
        limit -= start
 
        while - end <= limit and line['line'][end] == next['line'][end]:
 
            end -= 1
 
        end += 1
 
        if start or end:
 
            def do(l):
 
                last = end + len(l['line'])
 
                if l['action'] == 'add':
 
                    tag = 'ins'
 
                else:
 
                    tag = 'del'
pylons_app/templates/files/file_diff.html
Show inline comments
 
<%inherit file="/base/base.html"/>
 

	
 
<%def name="title()">
 
    ${_('Repository managment')}
 
</%def>
 
<%def name="breadcrumbs()">
 
    ${h.link_to(u'Home',h.url('/'))}
 
    / 
 
    ${h.link_to(c.repo_name,h.url('files_home',repo_name=c.repo_name))}
 
    /
 
    ${_('files')}
 
</%def>
 
<%def name="page_nav()">
 
        <form action="log">
 
            <dl class="search">
 
                <dt><label>Search: </label></dt>
 
                <dd><input type="text" name="rev" /></dd>
 
            </dl>
 
        </form>
 

	
 
		${self.menu('files')}     
 
</%def>
 
<%def name="css()">
 
<link rel="stylesheet" href="/css/monoblue_custom.css" type="text/css" />
 
<link rel="stylesheet" href="/css/diff.css" type="text/css" />
 
</%def>
 
<%def name="main()">
 
    <h2 class="no-link no-border">${'%s:  %s %s %s' % (_('File diff'),c.diff2,'&rarr;',c.diff1)|n}</h2>
 
<div id="body" class="diffblock">
 
	<div class="code-header">
 
		<span>${h.link_to(c.f_path,h.url('files_home',repo_name=c.repo_name,revision=c.diff2.split(':')[1],f_path=c.f_path))}</span>
 
	</div>
 
	<div class="code-body">
 
 			%if c.no_changes:
 
            	${_('No changes')}
 
            %else:        
 
	            <table class='code-difftable'>
 
	            %for diff in c.diff_files:
 
		            %for x in diff['chunks']:
 
		                %for y in x:
 
		                    <tr class="line ${y['action']}">
 
		                        <td id="#${diff['filename']}_O${y['old_lineno']}" class="lineno old">
 
		                              <pre><a href="#${diff['filename']}_O${y['old_lineno']}">${y['old_lineno']}</a></pre>
 
		                        </td>		                    
 
		                        <td id="#${diff['filename']}_N${y['new_lineno']}"class="lineno new">
 
		                              <pre><a href="#${diff['filename']}_N${y['new_lineno']}">${y['new_lineno']}</a></pre>
 
		                        </td>
 
		                        <td id="#${diff['filename']}_O${y['old_lineno']}" class="lineno old">
 
		                              <pre><a href="#${diff['filename']}_O${y['old_lineno']}">${y['old_lineno']}</a></pre>
 
		                        </td>                        
 
		                       <td class="code">
 
		                           <pre>${y['line']|n}</pre>
 
		                       </td>
 
		                    </tr>
 
		                %endfor$
 
		            %endfor
 
		        %endfor
 
	            </table>
 
            %endif
 
	</div>
 
</div>
 
</%def>    
 
\ No newline at end of file
0 comments (0 inline, 0 general)