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