def diff(self, commit, fmt=None): try: path, filename = os.path.split(self._blob.path()) a_ci = c.app.repo.commit(commit) a = a_ci.get_path(self._blob.path()) apath = a.path() except: a = [] apath = '' b = self._blob if not self._blob.has_html_view: diff = "Cannot display: file marked as a binary type." return dict(a=a, b=b, diff=diff) la = list(a) lb = list(b) adesc = (u'a' + h.really_unicode(apath)).encode('utf-8') bdesc = (u'b' + h.really_unicode(b.path())).encode('utf-8') if not fmt: fmt = web_session.get('diformat', '') else: web_session['diformat'] = fmt web_session.save() if fmt == 'sidebyside': hd = HtmlSideBySideDiff() diff = hd.make_table(la, lb, adesc, bdesc) else: diff = ''.join(difflib.unified_diff(la, lb, adesc, bdesc)) return dict(a=a, b=b, diff=diff)
def test_preprocess(self): d = self.diff self.assertEquals(d._preprocess(None), None) self.assertEquals(d._preprocess('<br> '), '<br>&nbsp;') self.assertEquals(d._preprocess('\ttabbed'), ' tabbed') # test non default tab size d = HtmlSideBySideDiff(2) self.assertEquals(d._preprocess('\ttabbed'), ' tabbed')
def diff(self, prev_commit, fmt=None, prev_file=None, **kw): ''' :param prev_commit: previous commit to compare against :param fmt: "sidebyside", or anything else for "unified" :param prev_file: previous filename, if different :return: ''' try: path, filename = os.path.split(self._blob.path()) a_ci = c.app.repo.commit(prev_commit) a = a_ci.get_path(prev_file or self._blob.path()) apath = a.path() except Exception: # prev commit doesn't have the file a = M.repository.EmptyBlob() apath = '' b = self._blob if not self._blob.has_html_view: diff = "Cannot display: file marked as a binary type." return dict(a=a, b=b, diff=diff) # could consider making Blob.__iter__ do unicode conversion? # py2 unified_diff can handle some unicode but not consistently, so best to do ensure_str (can drop it on py3) la = [six.ensure_str(h.really_unicode(line)) for line in a] lb = [six.ensure_str(h.really_unicode(line)) for line in b] adesc = 'a' + h.really_unicode(apath) bdesc = 'b' + h.really_unicode(b.path()) if not fmt: fmt = web_session.get('diformat', '') else: web_session['diformat'] = fmt web_session.save() if fmt == 'sidebyside': if max(a.size, b.size) > asint( tg.config.get('scm.view.max_syntax_highlight_bytes', 500000)): # have to check the original file size, not diff size, because difflib._mdiff inside HtmlSideBySideDiff # can take an extremely long time on large files (and its even a generator) diff = '<em>File too large for side-by-side view</em>' else: hd = HtmlSideBySideDiff() diff = hd.make_table(la, lb, adesc, bdesc) else: # py2 unified_diff can handle some unicode but not consistently, so best to do str() and ensure_str() # (can drop it on py3) diff = str('').join( difflib.unified_diff(la, lb, six.ensure_str(adesc), six.ensure_str(bdesc))) return dict(a=a, b=b, diff=diff)
def diff(self, prev_commit, fmt=None, prev_file=None, **kw): ''' :param prev_commit: previous commit to compare against :param fmt: "sidebyside", or anything else for "unified" :param prev_file: previous filename, if different :return: ''' try: path, filename = os.path.split(self._blob.path()) a_ci = c.app.repo.commit(prev_commit) a = a_ci.get_path(prev_file or self._blob.path()) apath = a.path() except: # prev commit doesn't have the file a = [] apath = '' b = self._blob if not self._blob.has_html_view: diff = "Cannot display: file marked as a binary type." return dict(a=a, b=b, diff=diff) la = list(a) lb = list(b) adesc = (u'a' + h.really_unicode(apath)).encode('utf-8') bdesc = (u'b' + h.really_unicode(b.path())).encode('utf-8') if not fmt: fmt = web_session.get('diformat', '') else: web_session['diformat'] = fmt web_session.save() if fmt == 'sidebyside': hd = HtmlSideBySideDiff() diff = hd.make_table(la, lb, adesc, bdesc) else: diff = ''.join(difflib.unified_diff(la, lb, adesc, bdesc)) return dict(a=a, b=b, diff=diff)
def setUp(self): self.diff = HtmlSideBySideDiff()