cdiff.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #!/usr/bin/env python
  2. import sys
  3. import os
  4. import re
  5. import difflib
  6. COLORS = {
  7. 'reset' : '\x1b[0m',
  8. 'red' : '\x1b[31m',
  9. 'green' : '\x1b[32m',
  10. 'yellow' : '\x1b[33m',
  11. 'blue' : '\x1b[34m',
  12. 'magenta' : '\x1b[35m',
  13. 'cyan' : '\x1b[36m',
  14. 'lightred' : '\x1b[1;31m',
  15. 'lightgreen' : '\x1b[1;32m',
  16. 'lightyellow' : '\x1b[1;33m',
  17. 'lightblue' : '\x1b[1;34m',
  18. 'lightmagenta' : '\x1b[1;35m',
  19. 'lightcyan' : '\x1b[1;36m',
  20. }
  21. def ansi_code(color):
  22. return COLORS.get(color, '')
  23. def colorize(text, start_color, end_color='reset'):
  24. return ansi_code(start_color) + text + ansi_code(end_color)
  25. class Hunk(object):
  26. def __init__(self, hunk_header):
  27. self._hunk_header = hunk_header
  28. self._hunk_list = [] # 2-element group (attr, line)
  29. def get_header(self):
  30. return self._hunk_header
  31. def append(self, attr, line):
  32. """attr: '-': old, '+': new, ' ': common"""
  33. self._hunk_list.append((attr, line))
  34. def mdiff(self):
  35. """The difflib._mdiff() function returns an interator which returns a
  36. tuple: (from line tuple, to line tuple, boolean flag)
  37. from/to line tuple -- (line num, line text)
  38. line num -- integer or None (to indicate a context separation)
  39. line text -- original line text with following markers inserted:
  40. '\0+' -- marks start of added text
  41. '\0-' -- marks start of deleted text
  42. '\0^' -- marks start of changed text
  43. '\1' -- marks end of added/deleted/changed text
  44. boolean flag -- None indicates context separation, True indicates
  45. either "from" or "to" line contains a change, otherwise False.
  46. """
  47. return difflib._mdiff(self._get_old_text(), self._get_new_text())
  48. def _get_old_text(self):
  49. out = []
  50. for (attr, line) in self._hunk_list:
  51. if attr != '+':
  52. out.append(line)
  53. return out
  54. def _get_new_text(self):
  55. out = []
  56. for (attr, line) in self._hunk_list:
  57. if attr != '-':
  58. out.append(line)
  59. return out
  60. def __iter__(self):
  61. for hunk_line in self._hunk_list:
  62. yield hunk_line
  63. class Diff(object):
  64. def __init__(self, headers, old_path, new_path, hunks):
  65. self._headers = headers
  66. self._old_path = old_path
  67. self._new_path = new_path
  68. self._hunks = hunks
  69. def markup_traditional(self):
  70. out = []
  71. for line in self._headers:
  72. out.append(self._markup_header(line))
  73. out.append(self._markup_old_path(self._old_path))
  74. out.append(self._markup_new_path(self._new_path))
  75. for hunk in self._hunks:
  76. out.append(self._markup_hunk_header(hunk.get_header()))
  77. save_line = ''
  78. for from_info, to_info, changed in hunk.mdiff():
  79. if changed:
  80. if not from_info[0]:
  81. line = to_info[1].strip('\x00\x01')
  82. out.append(self._markup_new(line))
  83. elif not to_info[0]:
  84. line = from_info[1].strip('\x00\x01')
  85. out.append(self._markup_old(line))
  86. else:
  87. out.append(self._markup_old('-') +
  88. self._markup_old_mix(from_info[1]))
  89. out.append(self._markup_new('+') +
  90. self._markup_new_mix(to_info[1]))
  91. else:
  92. out.append(self._markup_common(' ' + from_info[1]))
  93. return ''.join(out)
  94. def markup_side_by_side(self, show_number, width):
  95. """Do not really need to parse the hunks..."""
  96. return 'TODO: show_number=%s, width=%d' % (show_number, width)
  97. def _markup_header(self, line):
  98. return colorize(line, 'cyan')
  99. def _markup_old_path(self, line):
  100. return colorize(line, 'yellow')
  101. def _markup_new_path(self, line):
  102. return colorize(line, 'yellow')
  103. def _markup_hunk_header(self, line):
  104. return colorize(line, 'lightblue')
  105. def _markup_common(self, line):
  106. return colorize(line, 'reset')
  107. def _markup_old(self, line):
  108. return colorize(line, 'lightred')
  109. def _markup_new(self, line):
  110. return colorize(line, 'lightgreen')
  111. def _markup_mix(self, line, base_color, del_color, add_color, chg_color):
  112. line = line.replace('\x00-', ansi_code(del_color))
  113. line = line.replace('\x00+', ansi_code(add_color))
  114. line = line.replace('\x00^', ansi_code(chg_color))
  115. line = line.replace('\x01', ansi_code(base_color))
  116. return colorize(line, base_color)
  117. def _markup_old_mix(self, line):
  118. return self._markup_mix(line, 'cyan', 'lightred', 'lightgreen',
  119. 'yellow')
  120. def _markup_new_mix(self, line):
  121. return self._markup_mix(line, 'lightcyan', 'lightred', 'lightgreen',
  122. 'lightyellow')
  123. class Udiff(Diff):
  124. @staticmethod
  125. def is_old_path(line):
  126. return line.startswith('--- ')
  127. @staticmethod
  128. def is_new_path(line):
  129. return line.startswith('+++ ')
  130. @staticmethod
  131. def is_hunk_header(line):
  132. return line.startswith('@@ -')
  133. @staticmethod
  134. def is_old(line):
  135. return line.startswith('-') and not Udiff.is_old_path(line)
  136. @staticmethod
  137. def is_new(line):
  138. return line.startswith('+') and not Udiff.is_new_path(line)
  139. @staticmethod
  140. def is_common(line):
  141. return line.startswith(' ')
  142. @staticmethod
  143. def is_eof(line):
  144. # \ No newline at end of file
  145. return line.startswith('\\')
  146. @staticmethod
  147. def is_header(line):
  148. return re.match(r'^[^+@\\ -]', line)
  149. class DiffParser(object):
  150. def __init__(self, stream):
  151. for line in stream[:10]:
  152. if line.startswith('+++ '):
  153. self._type = 'udiff'
  154. break
  155. else:
  156. raise RuntimeError('unknown diff type')
  157. try:
  158. self._diffs = self._parse(stream)
  159. except (AssertionError, IndexError):
  160. raise RuntimeError('invalid patch format')
  161. def get_diffs(self):
  162. return self._diffs
  163. def _parse(self, stream):
  164. if self._type == 'udiff':
  165. return self._parse_udiff(stream)
  166. else:
  167. raise RuntimeError('unsupported diff format')
  168. def _parse_udiff(self, stream):
  169. """parse all diff lines here, construct a list of Udiff objects"""
  170. out_diffs = []
  171. headers = []
  172. old_path = None
  173. new_path = None
  174. hunks = []
  175. hunk = None
  176. while stream:
  177. if Udiff.is_header(stream[0]):
  178. if headers and old_path:
  179. # Encounter a new header
  180. assert new_path is not None
  181. assert hunk is not None
  182. hunks.append(hunk)
  183. out_diffs.append(Diff(headers, old_path, new_path, hunks))
  184. headers = []
  185. old_path = None
  186. new_path = None
  187. hunks = []
  188. hunk = None
  189. else:
  190. headers.append(stream.pop(0))
  191. elif Udiff.is_old_path(stream[0]):
  192. if old_path:
  193. # Encounter a new patch set
  194. assert new_path is not None
  195. assert hunk is not None
  196. hunks.append(hunk)
  197. out_diffs.append(Diff(headers, old_path, new_path, hunks))
  198. headers = []
  199. old_path = None
  200. new_path = None
  201. hunks = []
  202. hunk = None
  203. else:
  204. old_path = stream.pop(0)
  205. elif Udiff.is_new_path(stream[0]):
  206. assert old_path is not None
  207. assert new_path is None
  208. new_path = stream.pop(0)
  209. elif Udiff.is_hunk_header(stream[0]):
  210. assert old_path is not None
  211. assert new_path is not None
  212. if hunk:
  213. # Encounter a new hunk header
  214. hunks.append(hunk)
  215. hunk = None
  216. else:
  217. hunk = Hunk(stream.pop(0))
  218. elif Udiff.is_old(stream[0]) or Udiff.is_new(stream[0]) or \
  219. Udiff.is_common(stream[0]):
  220. assert old_path is not None
  221. assert new_path is not None
  222. assert hunk is not None
  223. hunk_line = stream.pop(0)
  224. hunk.append(hunk_line[0], hunk_line[1:])
  225. elif Udiff.is_eof(stream[0]):
  226. # ignore
  227. stream.pop(0)
  228. else:
  229. raise RuntimeError('unknown patch format: %s' % stream[0])
  230. # The last patch
  231. if hunk:
  232. hunks.append(hunk)
  233. if old_path:
  234. if new_path:
  235. out_diffs.append(Diff(headers, old_path, new_path, hunks))
  236. else:
  237. raise RuntimeError('unknown patch format after "%s"' % old_path)
  238. elif headers:
  239. raise RuntimeError('unknown patch format: %s' % \
  240. ('\n'.join(headers)))
  241. return out_diffs
  242. class DiffMarkup(object):
  243. def __init__(self, stream):
  244. self._diffs = DiffParser(stream).get_diffs()
  245. def markup(self, side_by_side=False, show_number=False, width=0):
  246. if side_by_side:
  247. return self._markup_side_by_side(show_number, width)
  248. else:
  249. return self._markup_traditional()
  250. def _markup_traditional(self):
  251. out = []
  252. for diff in self._diffs:
  253. out.append(diff.markup_traditional())
  254. return out
  255. def _markup_side_by_side(self, show_number, width):
  256. """width of 0 or negative means auto detect terminal width"""
  257. out = []
  258. for diff in self._diffs:
  259. out.append(diff.markup_side_by_side(show_number, width))
  260. return out
  261. if __name__ == '__main__':
  262. import optparse
  263. import subprocess
  264. usage = '''
  265. %(prog)s [options] [diff]
  266. View diff (patch) file if given, otherwise read stdin''' % \
  267. {'prog': os.path.basename(sys.argv[0])}
  268. parser = optparse.OptionParser(usage)
  269. parser.add_option('-s', '--side-by-side', action='store_true',
  270. help=('show in side-by-side mode'))
  271. parser.add_option('-n', '--number', action='store_true',
  272. help='show line number')
  273. parser.add_option('-w', '--width', type='int', default=0,
  274. help='set line width (side-by-side mode only)')
  275. opts, args = parser.parse_args()
  276. if opts.width < 0:
  277. opts.width = 0
  278. if len(args) >= 1:
  279. diff_hdl = open(args[0], 'r')
  280. elif sys.stdin.isatty():
  281. sys.stderr.write('Try --help option for usage\n')
  282. sys.exit(1)
  283. else:
  284. diff_hdl = sys.stdin
  285. stream = diff_hdl.readlines()
  286. if diff_hdl is not sys.stdin:
  287. diff_hdl.close()
  288. if sys.stdout.isatty():
  289. markup = DiffMarkup(stream)
  290. color_diff = markup.markup(side_by_side=opts.side_by_side,
  291. show_number=opts.number, width=opts.width)
  292. # args stolen fron git source: github.com/git/git/blob/master/pager.c
  293. pager = subprocess.Popen(['less', '-FRSXK'],
  294. stdin=subprocess.PIPE, stdout=sys.stdout)
  295. pager.stdin.write(''.join(color_diff))
  296. pager.stdin.close()
  297. pager.wait()
  298. else:
  299. # pipe out stream untouched to make sure it is still a patch
  300. sys.stdout.write(''.join(stream))
  301. sys.exit(0)
  302. # vim:set et sts=4 sw=4 tw=80: