Changeset - 8df1bc51aa9c
[Not reviewed]
beta
0 1 0
Mads Kiilerich - 13 years ago 2013-04-10 03:00:20
madski@unity3d.com
diff parser: prefer git headers over old unified diff headers

Makes the diff show both rename and chmod info and the diff.
1 file changed with 28 insertions and 31 deletions:
0 comments (0 inline, 0 general)
rhodecode/lib/diffs.py
Show inline comments
 
@@ -339,83 +339,80 @@ class DiffProcessor(object):
 
            match = self._hg_header_re.match(diff_chunk)
 
            diff = diff_chunk[match.end():]
 
            return match.groupdict(), imap(self._escaper, diff.splitlines(1))
 
        else:
 
            raise Exception('VCS type %s is not supported' % self.vcs)
 

	
 
    def _clean_line(self, line, command):
 
        if command in ['+', '-', ' ']:
 
            #only modify the line if it's actually a diff thing
 
            line = line[1:]
 
        return line
 

	
 
    def _parse_gitdiff(self, inline_diff=True):
 
        _files = []
 
        diff_container = lambda arg: arg
 

	
 
        ##split the diff in chunks of separate --git a/file b/file chunks
 
        for raw_diff in ('\n' + self._diff).split('\ndiff --git')[1:]:
 
            head, diff = self._get_header(raw_diff)
 

	
 
            op = None
 
            stats = None
 
            msgs = []
 

	
 
            if not head['a_file'] and head['b_file']:
 
                op = 'A'
 
                stats = ['b', NEW_FILENODE]
 
                msgs.append('new file')
 
            elif head['a_file'] and head['b_file']:
 
                op = 'M'
 
                stats = ['b', MOD_FILENODE]
 
            elif head['a_file'] and not head['b_file']:
 
            if head['deleted_file_mode']:
 
                op = 'D'
 
                stats = ['b', DEL_FILENODE]
 
                msgs.append('deleted file')
 
            elif head['new_file_mode']:
 
                op = 'A'
 
                stats = ['b', NEW_FILENODE]
 
                msgs.append('new file %s' % head['new_file_mode'])
 
            else:
 
                if head['deleted_file_mode']:
 
                    op = 'D'
 
                    stats = ['b', DEL_FILENODE]
 
                    msgs.append('deleted file')
 
                elif head['new_file_mode']:
 
                    op = 'A'
 
                    stats = ['b', NEW_FILENODE]
 
                    msgs.append('new file %s' % head['new_file_mode'])
 
                else:
 
                    if head['new_mode'] and head['old_mode']:
 
                        op = 'M'
 
                        stats = ['b', CHMOD_FILENODE]
 
                        msgs.append('modified file chmod %s => %s'
 
                                      % (head['old_mode'], head['new_mode']))
 
                    if (head['rename_from'] and head['rename_to']
 
                          and head['rename_from'] != head['rename_to']):
 
                        op = 'M'
 
                        stats = ['b', RENAMED_FILENODE] # might overwrite CHMOD_FILENODE
 
                        msgs.append('file renamed from %s to %s'
 
                                      % (head['rename_from'], head['rename_to']))
 
                    if op is None:
 
                        op = 'M'
 
                        stats = ['b', MOD_FILENODE]
 
                if head['new_mode'] and head['old_mode']:
 
                    op = 'M'
 
                    stats = ['b', CHMOD_FILENODE]
 
                    msgs.append('modified file chmod %s => %s'
 
                                  % (head['old_mode'], head['new_mode']))
 
                if (head['rename_from'] and head['rename_to']
 
                      and head['rename_from'] != head['rename_to']):
 
                    op = 'M'
 
                    stats = ['b', RENAMED_FILENODE] # might overwrite CHMOD_FILENODE
 
                    msgs.append('file renamed from %s to %s'
 
                                  % (head['rename_from'], head['rename_to']))
 
                if op is None: # fall back: detect missed old style add or remove
 
                    if not head['a_file'] and head['b_file']:
 
                        op = 'A'
 
                        stats = ['b', NEW_FILENODE]
 
                        msgs.append('new file')
 
                    elif head['a_file'] and not head['b_file']:
 
                        op = 'D'
 
                        stats = ['b', DEL_FILENODE]
 
                        msgs.append('deleted file')
 
                if op is None:
 
                    op = 'M'
 
                    stats = ['b', MOD_FILENODE]
 

	
 
            if head['a_file'] or head['b_file']: # a real diff
 
                try:
 
                    chunks, stats = self._parse_lines(diff)
 
                except DiffLimitExceeded:
 
                    diff_container = lambda _diff: LimitedDiffContainer(
 
                                                self.diff_limit,
 
                                                self.cur_diff_size,
 
                                                _diff)
 
                    break
 
            else: # GIT binary patch (or empty diff)
 
                chunks = []
 
                msgs.append('binary diff not shown') # or no diff because it was a rename or chmod or add/remove of empty file
 

	
 
            if msgs:
 
                chunks.insert(0, [{
 
                    'old_lineno': '',
 
                    'new_lineno': '',
 
                    'action':     'binary',
 
                    'line':       msg,
 
                    } for msg in msgs])
 

	
 
            _files.append({
 
                'filename':         head['b_path'],
0 comments (0 inline, 0 general)