123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Term based tool to view **colored**, **incremental** diff in *git/svn/hg*
  5. workspace, given patch or two files, or from stdin, with **side by side** and
  6. **auto pager** support. Requires python (>= 2.5.0) and ``less``.
  7. """
  8. META_INFO = {
  9. 'version' : '0.3',
  10. 'license' : 'BSD-3',
  11. 'author' : 'Matthew Wang',
  12. 'email' : 'mattwyl(@)gmail(.)com',
  13. 'url' : 'https://github.com/ymattw/cdiff',
  14. 'keywords' : 'colored incremental side-by-side diff',
  15. 'description' : ('View colored, incremental diff in workspace, given patch '
  16. 'or two files, or from stdin, with side by side and auto '
  17. 'pager support')
  18. }
  19. import sys
  20. if sys.hexversion < 0x02050000:
  21. sys.stderr.write("*** Requires python >= 2.5.0\n")
  22. sys.exit(1)
  23. IS_PY3 = sys.hexversion >= 0x03000000
  24. import os
  25. import re
  26. import subprocess
  27. import errno
  28. import difflib
  29. COLORS = {
  30. 'reset' : '\x1b[0m',
  31. 'underline' : '\x1b[4m',
  32. 'reverse' : '\x1b[7m',
  33. 'red' : '\x1b[31m',
  34. 'green' : '\x1b[32m',
  35. 'yellow' : '\x1b[33m',
  36. 'blue' : '\x1b[34m',
  37. 'magenta' : '\x1b[35m',
  38. 'cyan' : '\x1b[36m',
  39. 'lightred' : '\x1b[1;31m',
  40. 'lightgreen' : '\x1b[1;32m',
  41. 'lightyellow' : '\x1b[1;33m',
  42. 'lightblue' : '\x1b[1;34m',
  43. 'lightmagenta' : '\x1b[1;35m',
  44. 'lightcyan' : '\x1b[1;36m',
  45. }
  46. # Keys for checking and values for diffing.
  47. REVISION_CONTROL = (
  48. (['git', 'rev-parse'], ['git', 'diff']),
  49. (['svn', 'info'], ['svn', 'diff']),
  50. (['hg', 'summary'], ['hg', 'diff'])
  51. )
  52. def ansi_code(color):
  53. return COLORS.get(color, '')
  54. def colorize(text, start_color, end_color='reset'):
  55. return ansi_code(start_color) + text + ansi_code(end_color)
  56. class Hunk(object):
  57. def __init__(self, hunk_header, old_addr, new_addr):
  58. self._hunk_header = hunk_header
  59. self._old_addr = old_addr # tuple (start, offset)
  60. self._new_addr = new_addr # tuple (start, offset)
  61. self._hunk_list = [] # list of tuple (attr, line)
  62. def get_header(self):
  63. return self._hunk_header
  64. def get_old_addr(self):
  65. return self._old_addr
  66. def get_new_addr(self):
  67. return self._new_addr
  68. def append(self, attr, line):
  69. """attr: '-': old, '+': new, ' ': common"""
  70. self._hunk_list.append((attr, line))
  71. def mdiff(self):
  72. r"""The difflib._mdiff() function returns an interator which returns a
  73. tuple: (from line tuple, to line tuple, boolean flag)
  74. from/to line tuple -- (line num, line text)
  75. line num -- integer or None (to indicate a context separation)
  76. line text -- original line text with following markers inserted:
  77. '\0+' -- marks start of added text
  78. '\0-' -- marks start of deleted text
  79. '\0^' -- marks start of changed text
  80. '\1' -- marks end of added/deleted/changed text
  81. boolean flag -- None indicates context separation, True indicates
  82. either "from" or "to" line contains a change, otherwise False.
  83. """
  84. return difflib._mdiff(self._get_old_text(), self._get_new_text())
  85. def _get_old_text(self):
  86. out = []
  87. for (attr, line) in self._hunk_list:
  88. if attr != '+':
  89. out.append(line)
  90. return out
  91. def _get_new_text(self):
  92. out = []
  93. for (attr, line) in self._hunk_list:
  94. if attr != '-':
  95. out.append(line)
  96. return out
  97. def __iter__(self):
  98. for hunk_line in self._hunk_list:
  99. yield hunk_line
  100. class Diff(object):
  101. def __init__(self, headers, old_path, new_path, hunks):
  102. self._headers = headers
  103. self._old_path = old_path
  104. self._new_path = new_path
  105. self._hunks = hunks
  106. # Follow detector and the parse_hunk_header() are suppose to be overwritten
  107. # by derived class
  108. #
  109. def is_old_path(self, line):
  110. return False
  111. def is_new_path(self, line):
  112. return False
  113. def is_hunk_header(self, line):
  114. return False
  115. def parse_hunk_header(self, line):
  116. """Returns a 2-eliment tuple, each of them is a tuple in form of (start,
  117. offset)"""
  118. return False
  119. def is_old(self, line):
  120. return False
  121. def is_new(self, line):
  122. return False
  123. def is_common(self, line):
  124. return False
  125. def is_eof(self, line):
  126. return False
  127. def is_header(self, line):
  128. return False
  129. def markup_traditional(self):
  130. """Returns a generator"""
  131. for line in self._headers:
  132. yield self._markup_header(line)
  133. yield self._markup_old_path(self._old_path)
  134. yield self._markup_new_path(self._new_path)
  135. for hunk in self._hunks:
  136. yield self._markup_hunk_header(hunk.get_header())
  137. for old, new, changed in hunk.mdiff():
  138. if changed:
  139. if not old[0]:
  140. # The '+' char after \x00 is kept
  141. # DEBUG: yield 'NEW: %s %s\n' % (old, new)
  142. line = new[1].strip('\x00\x01')
  143. yield self._markup_new(line)
  144. elif not new[0]:
  145. # The '-' char after \x00 is kept
  146. # DEBUG: yield 'OLD: %s %s\n' % (old, new)
  147. line = old[1].strip('\x00\x01')
  148. yield self._markup_old(line)
  149. else:
  150. # DEBUG: yield 'CHG: %s %s\n' % (old, new)
  151. yield self._markup_old('-') + \
  152. self._markup_old_mix(old[1])
  153. yield self._markup_new('+') + \
  154. self._markup_new_mix(new[1])
  155. else:
  156. yield self._markup_common(' ' + old[1])
  157. def markup_side_by_side(self, width):
  158. """Returns a generator"""
  159. def _normalize(line):
  160. return line.replace('\t', ' '*8).replace('\n', '').replace('\r', '')
  161. def _fit_width(markup, width, pad=False):
  162. """str len does not count correctly if left column contains ansi
  163. color code. Only left side need to set `pad`
  164. """
  165. out = []
  166. count = 0
  167. ansi_color_regex = r'\x1b\[(1;)?\d{1,2}m'
  168. patt = re.compile('^(%s)(.*)' % ansi_color_regex)
  169. repl = re.compile(ansi_color_regex)
  170. while markup and count < width:
  171. if patt.match(markup):
  172. out.append(patt.sub(r'\1', markup))
  173. markup = patt.sub(r'\3', markup)
  174. else:
  175. # FIXME: utf-8 wchar might break the rule here, e.g.
  176. # u'\u554a' takes double width of a single letter, also this
  177. # depends on your terminal font. I guess audience of this
  178. # tool never put that kind of symbol in their code :-)
  179. #
  180. out.append(markup[0])
  181. count += 1
  182. markup = markup[1:]
  183. if count == width and repl.sub('', markup):
  184. # stripped: output fulfil and still have ascii in markup
  185. out[-1] = ansi_code('reset') + colorize('>', 'lightmagenta')
  186. elif count < width and pad:
  187. pad_len = width - count
  188. out.append('%*s' % (pad_len, ''))
  189. return ''.join(out)
  190. # Setup line width and number width
  191. if width <= 0:
  192. width = 80
  193. (start, offset) = self._hunks[-1].get_old_addr()
  194. max1 = start + offset - 1
  195. (start, offset) = self._hunks[-1].get_new_addr()
  196. max2 = start + offset - 1
  197. num_width = max(len(str(max1)), len(str(max2)))
  198. left_num_fmt = colorize('%%(left_num)%ds' % num_width, 'yellow')
  199. right_num_fmt = colorize('%%(right_num)%ds' % num_width, 'yellow')
  200. line_fmt = left_num_fmt + ' %(left)s ' + ansi_code('reset') + \
  201. right_num_fmt + ' %(right)s\n'
  202. # yield header, old path and new path
  203. for line in self._headers:
  204. yield self._markup_header(line)
  205. yield self._markup_old_path(self._old_path)
  206. yield self._markup_new_path(self._new_path)
  207. # yield hunks
  208. for hunk in self._hunks:
  209. yield self._markup_hunk_header(hunk.get_header())
  210. for old, new, changed in hunk.mdiff():
  211. if old[0]:
  212. left_num = str(hunk.get_old_addr()[0] + int(old[0]) - 1)
  213. else:
  214. left_num = ' '
  215. if new[0]:
  216. right_num = str(hunk.get_new_addr()[0] + int(new[0]) - 1)
  217. else:
  218. right_num = ' '
  219. left = _normalize(old[1])
  220. right = _normalize(new[1])
  221. if changed:
  222. if not old[0]:
  223. left = '%*s' % (width, ' ')
  224. right = right.lstrip('\x00+').rstrip('\x01')
  225. right = _fit_width(self._markup_new(right), width)
  226. elif not new[0]:
  227. left = left.lstrip('\x00-').rstrip('\x01')
  228. left = _fit_width(self._markup_old(left), width)
  229. right = ''
  230. else:
  231. left = _fit_width(self._markup_old_mix(left), width, 1)
  232. right = _fit_width(self._markup_new_mix(right), width)
  233. else:
  234. left = _fit_width(self._markup_common(left), width, 1)
  235. right = _fit_width(self._markup_common(right), width)
  236. yield line_fmt % {
  237. 'left_num': left_num,
  238. 'left': left,
  239. 'right_num': right_num,
  240. 'right': right
  241. }
  242. def _markup_header(self, line):
  243. return colorize(line, 'cyan')
  244. def _markup_old_path(self, line):
  245. return colorize(line, 'yellow')
  246. def _markup_new_path(self, line):
  247. return colorize(line, 'yellow')
  248. def _markup_hunk_header(self, line):
  249. return colorize(line, 'lightblue')
  250. def _markup_common(self, line):
  251. return colorize(line, 'reset')
  252. def _markup_old(self, line):
  253. return colorize(line, 'lightred')
  254. def _markup_new(self, line):
  255. return colorize(line, 'lightgreen')
  256. def _markup_mix(self, line, base_color):
  257. del_code = ansi_code('reverse') + ansi_code(base_color)
  258. add_code = ansi_code('reverse') + ansi_code(base_color)
  259. chg_code = ansi_code('underline') + ansi_code(base_color)
  260. rst_code = ansi_code('reset') + ansi_code(base_color)
  261. line = line.replace('\x00-', del_code)
  262. line = line.replace('\x00+', add_code)
  263. line = line.replace('\x00^', chg_code)
  264. line = line.replace('\x01', rst_code)
  265. return colorize(line, base_color)
  266. def _markup_old_mix(self, line):
  267. return self._markup_mix(line, 'red')
  268. def _markup_new_mix(self, line):
  269. return self._markup_mix(line, 'green')
  270. class Udiff(Diff):
  271. def is_old_path(self, line):
  272. return line.startswith('--- ')
  273. def is_new_path(self, line):
  274. return line.startswith('+++ ')
  275. def is_hunk_header(self, line):
  276. return line.startswith('@@ -')
  277. def parse_hunk_header(self, hunk_header):
  278. # @@ -3,7 +3,6 @@
  279. a = hunk_header.split()[1].split(',') # -3 7
  280. if len(a) > 1:
  281. old_addr = (int(a[0][1:]), int(a[1]))
  282. else:
  283. # @@ -1 +1,2 @@
  284. old_addr = (int(a[0][1:]), 0)
  285. b = hunk_header.split()[2].split(',') # +3 6
  286. if len(b) > 1:
  287. new_addr = (int(b[0][1:]), int(b[1]))
  288. else:
  289. # @@ -0,0 +1 @@
  290. new_addr = (int(b[0][1:]), 0)
  291. return (old_addr, new_addr)
  292. def is_old(self, line):
  293. return line.startswith('-') and not self.is_old_path(line)
  294. def is_new(self, line):
  295. return line.startswith('+') and not self.is_new_path(line)
  296. def is_common(self, line):
  297. return line.startswith(' ')
  298. def is_eof(self, line):
  299. # \ No newline at end of file
  300. return line.startswith('\\')
  301. def is_header(self, line):
  302. return re.match(r'^[^+@\\ -]', line)
  303. class DiffParser(object):
  304. def __init__(self, stream):
  305. """Detect Udiff with 3 conditions"""
  306. flag = 0
  307. for line in stream[:20]:
  308. if line.startswith('--- '):
  309. flag |= 1
  310. elif line.startswith('+++ '):
  311. flag |= 2
  312. elif line.startswith('@@ '):
  313. flag |= 4
  314. if flag & 7:
  315. self._type = 'udiff'
  316. else:
  317. raise RuntimeError('unknown diff type')
  318. try:
  319. self._diffs = self._parse(stream)
  320. except (AssertionError, IndexError):
  321. raise RuntimeError('invalid patch format')
  322. def get_diffs(self):
  323. return self._diffs
  324. def _parse(self, stream):
  325. """parse all diff lines, construct a list of Diff objects"""
  326. if self._type == 'udiff':
  327. difflet = Udiff(None, None, None, None)
  328. else:
  329. raise RuntimeError('unsupported diff format')
  330. out_diffs = []
  331. headers = []
  332. old_path = None
  333. new_path = None
  334. hunks = []
  335. hunk = None
  336. while stream:
  337. # 'common' line occurs before 'old_path' is considered as header
  338. # too, this happens with `git log -p` and `git show <commit>`
  339. #
  340. if difflet.is_header(stream[0]) or \
  341. (difflet.is_common(stream[0]) and old_path is None):
  342. if headers and old_path:
  343. # Encounter a new header
  344. assert new_path is not None
  345. assert hunk is not None
  346. hunks.append(hunk)
  347. out_diffs.append(Diff(headers, old_path, new_path, hunks))
  348. headers = []
  349. old_path = None
  350. new_path = None
  351. hunks = []
  352. hunk = None
  353. else:
  354. headers.append(stream.pop(0))
  355. elif difflet.is_old_path(stream[0]):
  356. if old_path:
  357. # Encounter a new patch set
  358. assert new_path is not None
  359. assert hunk is not None
  360. hunks.append(hunk)
  361. out_diffs.append(Diff(headers, old_path, new_path, hunks))
  362. headers = []
  363. old_path = None
  364. new_path = None
  365. hunks = []
  366. hunk = None
  367. else:
  368. old_path = stream.pop(0)
  369. elif difflet.is_new_path(stream[0]):
  370. assert old_path is not None
  371. assert new_path is None
  372. new_path = stream.pop(0)
  373. elif difflet.is_hunk_header(stream[0]):
  374. assert old_path is not None
  375. assert new_path is not None
  376. if hunk:
  377. # Encounter a new hunk header
  378. hunks.append(hunk)
  379. hunk = None
  380. else:
  381. hunk_header = stream.pop(0)
  382. old_addr, new_addr = difflet.parse_hunk_header(hunk_header)
  383. hunk = Hunk(hunk_header, old_addr, new_addr)
  384. elif difflet.is_old(stream[0]) or difflet.is_new(stream[0]) or \
  385. difflet.is_common(stream[0]):
  386. assert old_path is not None
  387. assert new_path is not None
  388. assert hunk is not None
  389. hunk_line = stream.pop(0)
  390. hunk.append(hunk_line[0], hunk_line[1:])
  391. elif difflet.is_eof(stream[0]):
  392. # ignore
  393. stream.pop(0)
  394. else:
  395. raise RuntimeError('unknown patch format: %s' % stream[0])
  396. # The last patch
  397. if hunk:
  398. hunks.append(hunk)
  399. if old_path:
  400. if new_path:
  401. out_diffs.append(Diff(headers, old_path, new_path, hunks))
  402. else:
  403. raise RuntimeError('unknown patch format after "%s"' % old_path)
  404. elif headers:
  405. raise RuntimeError('unknown patch format: %s' % \
  406. ('\n'.join(headers)))
  407. return out_diffs
  408. class DiffMarkup(object):
  409. def __init__(self, stream):
  410. self._diffs = DiffParser(stream).get_diffs()
  411. def markup(self, side_by_side=False, width=0):
  412. """Returns a generator"""
  413. if side_by_side:
  414. return self._markup_side_by_side(width)
  415. else:
  416. return self._markup_traditional()
  417. def _markup_traditional(self):
  418. for diff in self._diffs:
  419. for line in diff.markup_traditional():
  420. yield line
  421. def _markup_side_by_side(self, width):
  422. for diff in self._diffs:
  423. for line in diff.markup_side_by_side(width):
  424. yield line
  425. def markup_to_pager(stream, opts):
  426. markup = DiffMarkup(stream)
  427. color_diff = markup.markup(side_by_side=opts.side_by_side,
  428. width=opts.width)
  429. # args stolen fron git source: github.com/git/git/blob/master/pager.c
  430. pager = subprocess.Popen(['less', '-FRSXK'],
  431. stdin=subprocess.PIPE, stdout=sys.stdout)
  432. for line in color_diff:
  433. pager.stdin.write(line.encode('utf-8'))
  434. pager.stdin.close()
  435. pager.wait()
  436. def check_command_status(arguments):
  437. """Return True if command returns 0."""
  438. try:
  439. return subprocess.call(
  440. arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
  441. except OSError:
  442. return False
  443. def revision_control_diff():
  444. """Return diff from revision control system."""
  445. for check, diff in REVISION_CONTROL:
  446. if check_command_status(check):
  447. return subprocess.Popen(diff, stdout=subprocess.PIPE).stdout
  448. def decode(line):
  449. """Decode UTF-8 if necessary."""
  450. try:
  451. return line.decode('utf-8')
  452. except AttributeError:
  453. return line
  454. def main():
  455. import optparse
  456. supported_vcs = [check[0] for check, _ in REVISION_CONTROL]
  457. usage = """
  458. %prog [options]
  459. %prog [options] <patch>
  460. %prog [options] <file1> <file2>"""
  461. parser = optparse.OptionParser(usage=usage,
  462. description=META_INFO['description'],
  463. version='%%prog %s' % META_INFO['version'])
  464. parser.add_option('-c', '--color', default='auto', metavar='WHEN',
  465. help='colorize mode "auto" (default), "always", or "never"')
  466. parser.add_option('-s', '--side-by-side', action='store_true',
  467. help='show in side-by-side mode')
  468. parser.add_option('-w', '--width', type='int', default=80, metavar='N',
  469. help='set text width (side-by-side mode only), default is 80')
  470. opts, args = parser.parse_args()
  471. if len(args) > 2:
  472. parser.print_help()
  473. return 1
  474. elif len(args) == 2:
  475. diff_hdl = subprocess.Popen(['diff', '-u', args[0], args[1]],
  476. stdout=subprocess.PIPE).stdout
  477. elif len(args) == 1:
  478. if IS_PY3:
  479. # Python3 needs the newline='' to keep '\r' (DOS format)
  480. diff_hdl = open(args[0], mode='rt', newline='')
  481. else:
  482. diff_hdl = open(args[0], mode='rt')
  483. elif sys.stdin.isatty():
  484. diff_hdl = revision_control_diff()
  485. if not diff_hdl:
  486. sys.stderr.write(('*** Not in a supported workspace, supported '
  487. 'are: %s\n\n') % ', '.join(supported_vcs))
  488. parser.print_help()
  489. return 1
  490. else:
  491. diff_hdl = sys.stdin
  492. # FIXME: can't use generator for now due to current implementation in parser
  493. stream = [decode(line) for line in diff_hdl.readlines()]
  494. if diff_hdl is not sys.stdin:
  495. diff_hdl.close()
  496. # Don't let empty diff pass thru
  497. if not stream:
  498. return 0
  499. if opts.color == 'always' or (opts.color == 'auto' and sys.stdout.isatty()):
  500. try:
  501. markup_to_pager(stream, opts)
  502. except IOError:
  503. e = sys.exc_info()[1]
  504. if e.errno == errno.EPIPE:
  505. pass
  506. else:
  507. # pipe out stream untouched to make sure it is still a patch
  508. sys.stdout.write(''.join(stream))
  509. return 0
  510. if __name__ == '__main__':
  511. sys.exit(main())
  512. # vim:set et sts=4 sw=4 tw=80: