cdiff.py 20KB

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