def diff_highlight_buffer(self, difftext): buf = gtk.TextBuffer() if self.colorstyle == 'background': buf.create_tag('removed', paragraph_background=gtklib.PRED) buf.create_tag('added', paragraph_background=gtklib.PGREEN) elif self.colorstyle == 'none': buf.create_tag('removed') buf.create_tag('added') else: buf.create_tag('removed', foreground=gtklib.DRED) buf.create_tag('added', foreground=gtklib.DGREEN) buf.create_tag('position', foreground=gtklib.DORANGE) buf.create_tag('header', foreground=gtklib.DBLUE) bufiter = buf.get_start_iter() for line in difftext: line = hglib.toutf(line) if line.startswith('---') or line.startswith('+++'): buf.insert_with_tags_by_name(bufiter, line, 'header') elif line.startswith('-'): line = hglib.diffexpand(line) buf.insert_with_tags_by_name(bufiter, line, 'removed') elif line.startswith('+'): line = hglib.diffexpand(line) buf.insert_with_tags_by_name(bufiter, line, 'added') elif line.startswith('@@'): buf.insert_with_tags_by_name(bufiter, line, 'position') else: line = hglib.diffexpand(line) buf.insert(bufiter, line) return buf
def prepare_diff(self, difflines, offset, fname): 'Borrowed from hgview; parses changeset diffs' def addtag( name, offset, length ): if tags and tags[-1][0] == name and tags[-1][2]==offset: tags[-1][2] += length else: tags.append( [name, offset, offset+length] ) add, rem = 0, 0 for l in difflines[1:]: if l.startswith('+'): add += 1 elif l.startswith('-'): rem += 1 outlines = [] tags = [] txt = hglib.toutf('=== (+%d,-%d) %s ===\n' % (add, rem, fname)) addtag( 'greybg', offset, len(txt) ) outlines.append(txt) offset += len(txt.decode('utf-8')) for l1 in difflines[1:]: l = hglib.toutf(l1) if l.startswith('--- '): continue if l.startswith('+++ '): continue if l.startswith('@@'): tag = 'blue' elif l.startswith('+'): tag = 'green' l = hglib.diffexpand(l) elif l.startswith('-'): tag = 'red' l = hglib.diffexpand(l) else: tag = 'normal' l = hglib.diffexpand(l) l = l+"\n" length = len(l.decode('utf-8')) addtag( tag, offset, length ) outlines.append( l ) offset += length return tags, outlines
def show_diff(self, selection): 'User selected a row in the candidate tree' hglib.invalidaterepo(self.repo) model, cpaths = selection.get_selected_rows() sensitive = cpaths and True or False self.acceptbtn.set_sensitive(sensitive) self.buf.set_text('') bufiter = self.buf.get_start_iter() for path in cpaths: row = model[path] src, usrc, dest, udest, percent, sensitive = row if not sensitive: continue ctx = self.repo['.'] aa = self.repo.wread(dest) rr = ctx.filectx(src).data() opts = mdiff.defaultopts difftext = mdiff.unidiff(rr, '', aa, '', src, dest, None, opts=opts) if not difftext: l = _('== %s and %s have identical contents ==\n\n') % (src, dest) self.buf.insert(bufiter, l) continue difflines = difftext.splitlines(True) for line in difflines: line = hglib.toutf(line) if line.startswith('---') or line.startswith('+++'): self.buf.insert_with_tags_by_name(bufiter, line, 'header') elif line.startswith('-'): line = hglib.diffexpand(line) self.buf.insert_with_tags_by_name(bufiter, line, 'removed') elif line.startswith('+'): line = hglib.diffexpand(line) self.buf.insert_with_tags_by_name(bufiter, line, 'added') elif line.startswith('@@'): self.buf.insert_with_tags_by_name(bufiter, line, 'position') else: line = hglib.diffexpand(line) self.buf.insert(bufiter, line)