def _differ_split(row, guide): """Break row into segments using guide line""" line, left_number, right_number, gap = row if left_number and right_number: type = "" elif left_number: type = "remove" elif right_number: type = "add" segments = [] pos = 2 if guide: assert guide.startswith("? ") for m in _re_differ.finditer(guide, pos): if m.start() > pos: segments.append(_item(text=sapi.escape(line[pos:m.start()]), type=None)) segments.append(_item(text=sapi.escape(line[m.start():m.end()]), type="change")) pos = m.end() segments.append(_item(text=sapi.escape(line[pos:]), type=None)) return _item(gap=ezt.boolean(gap), type=type, segments=segments, left_number=left_number, right_number=right_number)
def _differ_split(row, guide): """Break row into segments using guide line""" line, left_number, right_number, gap = row if left_number and right_number: type = "" elif left_number: type = "remove" elif right_number: type = "add" segments = [] pos = 2 if guide: assert guide.startswith("? ") for m in _re_differ.finditer(guide, pos): if m.start() > pos: segments.append( _item(text=sapi.escape(line[pos:m.start()]), type=None)) segments.append( _item(text=sapi.escape(line[m.start():m.end()]), type="change")) pos = m.end() segments.append(_item(text=sapi.escape(line[pos:]), type=None)) return _item(gap=ezt.boolean(gap), type=type, segments=segments, left_number=left_number, right_number=right_number)
def sidebyside(fromlines, tolines, context): """Generate side by side diff""" # for some reason mdiff chokes on \n's in input lines def _stripnl(line): return line.rstrip("\n") fromlines = list(map(_stripnl, fromlines)) tolines = list(map(_stripnl, tolines)) had_changes = 0 gap = False for fromdata, todata, flag in difflib._mdiff(fromlines, tolines, context): if fromdata is None and todata is None and flag is None: gap = True else: from_item = _mdiff_split(flag, fromdata) to_item = _mdiff_split(flag, todata) had_changes = 1 yield _item(gap=ezt.boolean(gap), columns=(from_item, to_item), type="intraline") gap = False if not had_changes: yield _item(type=_RCSDIFF_NO_CHANGES)
def __getitem__(self, idx): item = self.annotation.__getitem__(idx) diff_url = None if item.prev_rev: diff_url = '%sr1=%s&r2=%s' % (self.diff_url, item.prev_rev, item.rev) thisline = link_includes(sapi.escape(item.text), self.repos, self.path_parts, self.include_url) return _item(text=thisline, line_number=item.line_number, rev=item.rev, prev_rev=item.prev_rev, diff_url=diff_url, date=item.date, author=item.author)
def sidebyside(fromlines, tolines, context): """Generate side by side diff""" ### for some reason mdiff chokes on \n's in input lines line_strip = lambda line: line.rstrip("\n") fromlines = map(line_strip, fromlines) tolines = map(line_strip, tolines) had_changes = 0 gap = False for fromdata, todata, flag in difflib._mdiff(fromlines, tolines, context): if fromdata is None and todata is None and flag is None: gap = True else: from_item = _mdiff_split(flag, fromdata) to_item = _mdiff_split(flag, todata) had_changes = 1 yield _item(gap=ezt.boolean(gap), columns=(from_item, to_item), type="intraline") gap = False if not had_changes: yield _item(type=_RCSDIFF_NO_CHANGES)
def _mdiff_split(flag, line_number_text): """Break up row from mdiff output into segments""" line_number, text = line_number_text segments = [] pos = 0 while True: m = _re_mdiff.search(text, pos) if not m: segments.append(_item(text=sapi.escape(text[pos:]), type=None)) break if m.start() > pos: segments.append( _item(text=sapi.escape(text[pos:m.start()]), type=None)) if m.group(1) == "+": segments.append(_item(text=sapi.escape(m.group(2)), type="add")) elif m.group(1) == "-": segments.append(_item(text=sapi.escape(m.group(2)), type="remove")) elif m.group(1) == "^": segments.append(_item(text=sapi.escape(m.group(2)), type="change")) pos = m.end() return _item(segments=segments, line_number=line_number)
def unified(fromlines, tolines, context): """Generate unified diff""" diff = difflib.Differ().compare(fromlines, tolines) lastrow = None had_changes = 0 for row in _trim_context(diff, context): if row[0].startswith("? "): had_changes = 1 yield _differ_split(lastrow, row[0]) lastrow = None else: if lastrow: had_changes = 1 yield _differ_split(lastrow, None) lastrow = row if lastrow: had_changes = 1 yield _differ_split(lastrow, None) if not had_changes: yield _item(type=_RCSDIFF_NO_CHANGES)
def build_commit(server, cfg, desc, files, cvsroots, viewvc_link): ob = _item(num_files=len(files), files=[]) ob.log = desc and server.escape(desc).replace('\n', '<br />') or '' for commit in files: repository = commit.GetRepository() directory = commit.GetDirectory() cvsroot_name = cvsroots.get(repository) ## find the module name (if any) try: module = filter(None, directory.split('/'))[0] except IndexError: module = None ## skip commits we aren't supposed to show if module and ((module == 'CVSROOT' and cfg.options.hide_cvsroot) \ or is_forbidden(cfg, cvsroot_name, module)): continue ctime = commit.GetTime() if not ctime: ctime = " " else: if (cfg.options.use_localtime): ctime = time.strftime("%y/%m/%d %H:%M %Z", time.localtime(ctime)) else: ctime = time.strftime("%y/%m/%d %H:%M", time.gmtime(ctime)) \ + ' UTC' ## make the file link try: file = (directory and directory + "/") + commit.GetFile() except: raise Exception, str([directory, commit.GetFile()]) ## If we couldn't find the cvsroot path configured in the ## viewvc.conf file, or we don't have a VIEWVC_LINK, then ## don't make the link. if cvsroot_name and viewvc_link: flink = '[%s] <a href="%s/%s?root=%s">%s</a>' % ( cvsroot_name, viewvc_link, urllib.quote(file), cvsroot_name, file) if commit.GetType() == commit.CHANGE: dlink = '%s/%s?root=%s&view=diff&r1=%s&r2=%s' % ( viewvc_link, urllib.quote(file), cvsroot_name, prev_rev(commit.GetRevision()), commit.GetRevision()) else: dlink = None else: flink = '[%s] %s' % (repository, file) dlink = None ob.files.append(_item(date=ctime, author=commit.GetAuthor(), link=flink, rev=commit.GetRevision(), branch=commit.GetBranch(), plus=int(commit.GetPlusCount()), minus=int(commit.GetMinusCount()), type=commit.GetTypeString(), difflink=dlink, )) return ob
def build_commit(server, cfg, desc, files, cvsroots, viewvc_link): ob = _item(num_files=len(files), files=[]) ob.log = desc and server.escape(desc).replace('\n', '<br />') or '' for commit in files: repository = commit.GetRepository() directory = commit.GetDirectory() cvsroot_name = cvsroots.get(repository) ## find the module name (if any) try: module = filter(None, directory.split('/'))[0] except IndexError: module = None ## skip commits we aren't supposed to show if module and ((module == 'CVSROOT' and cfg.options.hide_cvsroot) \ or is_forbidden(cfg, cvsroot_name, module)): continue ctime = commit.GetTime() if not ctime: ctime = " " else: if (cfg.options.use_localtime): ctime = time.strftime("%y/%m/%d %H:%M %Z", time.localtime(ctime)) else: ctime = time.strftime("%y/%m/%d %H:%M", time.gmtime(ctime)) \ + ' UTC' ## make the file link try: file = (directory and directory + "/") + commit.GetFile() except: raise Exception, str([directory, commit.GetFile()]) ## If we couldn't find the cvsroot path configured in the ## viewvc.conf file, or we don't have a VIEWVC_LINK, then ## don't make the link. if cvsroot_name and viewvc_link: flink = '[%s] <a href="%s/%s?root=%s">%s</a>' % ( cvsroot_name, viewvc_link, urllib.quote(file), cvsroot_name, file) if commit.GetType() == commit.CHANGE: dlink = '%s/%s?root=%s&view=diff&r1=%s&r2=%s' % ( viewvc_link, urllib.quote(file), cvsroot_name, prev_rev(commit.GetRevision()), commit.GetRevision()) else: dlink = None else: flink = '[%s] %s' % (repository, file) dlink = None ob.files.append( _item( date=ctime, author=commit.GetAuthor(), link=flink, rev=commit.GetRevision(), branch=commit.GetBranch(), plus=int(commit.GetPlusCount()), minus=int(commit.GetMinusCount()), type=commit.GetTypeString(), difflink=dlink, )) return ob
had_changes = 1 yield _item(gap=ezt.boolean(gap), columns=(from_item, to_item), type="intraline") gap = False if not had_changes: yield _item(type=_RCSDIFF_NO_CHANGES) _re_mdiff = re.compile("\0([+-^])(.*?)\1") def _mdiff_split(flag, (line_number, text)): """Break up row from mdiff output into segments""" segments = [] pos = 0 while True: m = _re_mdiff.search(text, pos) if not m: segments.append(_item(text=sapi.escape(text[pos:]), type=None)) break if m.start() > pos: segments.append(_item(text=sapi.escape(text[pos:m.start()]), type=None)) if m.group(1) == "+": segments.append(_item(text=sapi.escape(m.group(2)), type="add")) elif m.group(1) == "-": segments.append(_item(text=sapi.escape(m.group(2)), type="remove")) elif m.group(1) == "^": segments.append(_item(text=sapi.escape(m.group(2)), type="change")) pos = m.end() return _item(segments=segments, line_number=line_number)
gap = False if not had_changes: yield _item(type=_RCSDIFF_NO_CHANGES) _re_mdiff = re.compile("\0([+-^])(.*?)\1") def _mdiff_split(flag, (line_number, text)): """Break up row from mdiff output into segments""" segments = [] pos = 0 while True: m = _re_mdiff.search(text, pos) if not m: segments.append(_item(text=sapi.escape(text[pos:]), type=None)) break if m.start() > pos: segments.append( _item(text=sapi.escape(text[pos:m.start()]), type=None)) if m.group(1) == "+": segments.append(_item(text=sapi.escape(m.group(2)), type="add")) elif m.group(1) == "-": segments.append(_item(text=sapi.escape(m.group(2)), type="remove")) elif m.group(1) == "^": segments.append(_item(text=sapi.escape(m.group(2)), type="change")) pos = m.end()