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