patch.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. # patch.py -- For dealing with packed-style patches.
  2. # Copyright (C) 2009-2013 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) a later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Classes for dealing with git am-style patches.
  19. These patches are basically unified diffs with some extra metadata tacked
  20. on.
  21. """
  22. from difflib import SequenceMatcher
  23. import rfc822
  24. import time
  25. from dulwich.objects import (
  26. Commit,
  27. S_ISGITLINK,
  28. )
  29. FIRST_FEW_BYTES = 8000
  30. def write_commit_patch(f, commit, contents, progress, version=None):
  31. """Write a individual file patch.
  32. :param commit: Commit object
  33. :param progress: Tuple with current patch number and total.
  34. :return: tuple with filename and contents
  35. """
  36. (num, total) = progress
  37. f.write("From %s %s\n" % (commit.id, time.ctime(commit.commit_time)))
  38. f.write("From: %s\n" % commit.author)
  39. f.write("Date: %s\n" % time.strftime("%a, %d %b %Y %H:%M:%S %Z"))
  40. f.write("Subject: [PATCH %d/%d] %s\n" % (num, total, commit.message))
  41. f.write("\n")
  42. f.write("---\n")
  43. try:
  44. import subprocess
  45. p = subprocess.Popen(["diffstat"], stdout=subprocess.PIPE,
  46. stdin=subprocess.PIPE)
  47. except (ImportError, OSError):
  48. pass # diffstat not available?
  49. else:
  50. (diffstat, _) = p.communicate(contents)
  51. f.write(diffstat)
  52. f.write("\n")
  53. f.write(contents)
  54. f.write("-- \n")
  55. if version is None:
  56. from dulwich import __version__ as dulwich_version
  57. f.write("Dulwich %d.%d.%d\n" % dulwich_version)
  58. else:
  59. f.write("%s\n" % version)
  60. def get_summary(commit):
  61. """Determine the summary line for use in a filename.
  62. :param commit: Commit
  63. :return: Summary string
  64. """
  65. return commit.message.splitlines()[0].replace(" ", "-")
  66. def unified_diff(a, b, fromfile='', tofile='', n=3):
  67. """difflib.unified_diff that doesn't write any dates or trailing spaces.
  68. Based on the same function in Python2.6.5-rc2's difflib.py
  69. """
  70. started = False
  71. for group in SequenceMatcher(None, a, b).get_grouped_opcodes(n):
  72. if not started:
  73. yield '--- %s\n' % fromfile
  74. yield '+++ %s\n' % tofile
  75. started = True
  76. i1, i2, j1, j2 = group[0][1], group[-1][2], group[0][3], group[-1][4]
  77. yield "@@ -%d,%d +%d,%d @@\n" % (i1+1, i2-i1, j1+1, j2-j1)
  78. for tag, i1, i2, j1, j2 in group:
  79. if tag == 'equal':
  80. for line in a[i1:i2]:
  81. yield ' ' + line
  82. continue
  83. if tag == 'replace' or tag == 'delete':
  84. for line in a[i1:i2]:
  85. if not line[-1] == '\n':
  86. line += '\n\\ No newline at end of file\n'
  87. yield '-' + line
  88. if tag == 'replace' or tag == 'insert':
  89. for line in b[j1:j2]:
  90. if not line[-1] == '\n':
  91. line += '\n\\ No newline at end of file\n'
  92. yield '+' + line
  93. def is_binary(content):
  94. """See if the first few bytes contain any null characters.
  95. :param content: Bytestring to check for binary content
  96. """
  97. return '\0' in content[:FIRST_FEW_BYTES]
  98. def write_object_diff(f, store, (old_path, old_mode, old_id),
  99. (new_path, new_mode, new_id),
  100. diff_binary=False):
  101. """Write the diff for an object.
  102. :param f: File-like object to write to
  103. :param store: Store to retrieve objects from, if necessary
  104. :param (old_path, old_mode, old_hexsha): Old file
  105. :param (new_path, new_mode, new_hexsha): New file
  106. :param diff_binary: Whether to diff files even if they
  107. are considered binary files by is_binary().
  108. :note: the tuple elements should be None for nonexistant files
  109. """
  110. def shortid(hexsha):
  111. if hexsha is None:
  112. return "0" * 7
  113. else:
  114. return hexsha[:7]
  115. def content(mode, hexsha):
  116. if hexsha is None:
  117. return ''
  118. elif S_ISGITLINK(mode):
  119. return "Submodule commit " + hexsha + "\n"
  120. else:
  121. return store[hexsha].data
  122. def lines(content):
  123. if not content:
  124. return []
  125. else:
  126. return content.splitlines(True)
  127. if old_path is None:
  128. old_path = "/dev/null"
  129. else:
  130. old_path = "a/%s" % old_path
  131. if new_path is None:
  132. new_path = "/dev/null"
  133. else:
  134. new_path = "b/%s" % new_path
  135. f.write("diff --git %s %s\n" % (old_path, new_path))
  136. if old_mode != new_mode:
  137. if new_mode is not None:
  138. if old_mode is not None:
  139. f.write("old mode %o\n" % old_mode)
  140. f.write("new mode %o\n" % new_mode)
  141. else:
  142. f.write("deleted mode %o\n" % old_mode)
  143. f.write("index %s..%s" % (shortid(old_id), shortid(new_id)))
  144. if new_mode is not None:
  145. f.write(" %o" % new_mode)
  146. f.write("\n")
  147. old_content = content(old_mode, old_id)
  148. new_content = content(new_mode, new_id)
  149. if not diff_binary and (is_binary(old_content) or is_binary(new_content)):
  150. f.write("Binary files %s and %s differ\n" % (old_path, new_path))
  151. else:
  152. f.writelines(unified_diff(lines(old_content), lines(new_content),
  153. old_path, new_path))
  154. def write_blob_diff(f, (old_path, old_mode, old_blob),
  155. (new_path, new_mode, new_blob)):
  156. """Write diff file header.
  157. :param f: File-like object to write to
  158. :param (old_path, old_mode, old_blob): Previous file (None if nonexisting)
  159. :param (new_path, new_mode, new_blob): New file (None if nonexisting)
  160. :note: The use of write_object_diff is recommended over this function.
  161. """
  162. def blob_id(blob):
  163. if blob is None:
  164. return "0" * 7
  165. else:
  166. return blob.id[:7]
  167. def lines(blob):
  168. if blob is not None:
  169. return blob.data.splitlines(True)
  170. else:
  171. return []
  172. if old_path is None:
  173. old_path = "/dev/null"
  174. else:
  175. old_path = "a/%s" % old_path
  176. if new_path is None:
  177. new_path = "/dev/null"
  178. else:
  179. new_path = "b/%s" % new_path
  180. f.write("diff --git %s %s\n" % (old_path, new_path))
  181. if old_mode != new_mode:
  182. if new_mode is not None:
  183. if old_mode is not None:
  184. f.write("old mode %o\n" % old_mode)
  185. f.write("new mode %o\n" % new_mode)
  186. else:
  187. f.write("deleted mode %o\n" % old_mode)
  188. f.write("index %s..%s" % (blob_id(old_blob), blob_id(new_blob)))
  189. if new_mode is not None:
  190. f.write(" %o" % new_mode)
  191. f.write("\n")
  192. old_contents = lines(old_blob)
  193. new_contents = lines(new_blob)
  194. f.writelines(unified_diff(old_contents, new_contents,
  195. old_path, new_path))
  196. def write_tree_diff(f, store, old_tree, new_tree, diff_binary=False):
  197. """Write tree diff.
  198. :param f: File-like object to write to.
  199. :param old_tree: Old tree id
  200. :param new_tree: New tree id
  201. :param diff_binary: Whether to diff files even if they
  202. are considered binary files by is_binary().
  203. """
  204. changes = store.tree_changes(old_tree, new_tree)
  205. for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
  206. write_object_diff(f, store, (oldpath, oldmode, oldsha),
  207. (newpath, newmode, newsha),
  208. diff_binary=diff_binary)
  209. def git_am_patch_split(f):
  210. """Parse a git-am-style patch and split it up into bits.
  211. :param f: File-like object to parse
  212. :return: Tuple with commit object, diff contents and git version
  213. """
  214. msg = rfc822.Message(f)
  215. c = Commit()
  216. c.author = msg["from"]
  217. c.committer = msg["from"]
  218. try:
  219. patch_tag_start = msg["subject"].index("[PATCH")
  220. except ValueError:
  221. subject = msg["subject"]
  222. else:
  223. close = msg["subject"].index("] ", patch_tag_start)
  224. subject = msg["subject"][close+2:]
  225. c.message = subject.replace("\n", "") + "\n"
  226. first = True
  227. for l in f:
  228. if l == "---\n":
  229. break
  230. if first:
  231. if l.startswith("From: "):
  232. c.author = l[len("From: "):].rstrip()
  233. else:
  234. c.message += "\n" + l
  235. first = False
  236. else:
  237. c.message += l
  238. diff = ""
  239. for l in f:
  240. if l == "-- \n":
  241. break
  242. diff += l
  243. try:
  244. version = f.next().rstrip("\n")
  245. except StopIteration:
  246. version = None
  247. return c, diff, version