patch.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # patch.py -- For dealing with packed-style patches.
  2. # Copyright (C) 2009 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), e:
  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 contains any null characters."""
  95. return '\0' in content[:FIRST_FEW_BYTES]
  96. def write_object_diff(f, store, (old_path, old_mode, old_id),
  97. (new_path, new_mode, new_id)):
  98. """Write the diff for an object.
  99. :param f: File-like object to write to
  100. :param store: Store to retrieve objects from, if necessary
  101. :param (old_path, old_mode, old_hexsha): Old file
  102. :param (new_path, new_mode, new_hexsha): New file
  103. :note: the tuple elements should be None for nonexistant files
  104. """
  105. def shortid(hexsha):
  106. if hexsha is None:
  107. return "0" * 7
  108. else:
  109. return hexsha[:7]
  110. def content(mode, hexsha):
  111. if hexsha is None:
  112. return ''
  113. elif S_ISGITLINK(mode):
  114. return "Submodule commit " + hexsha + "\n"
  115. else:
  116. return store[hexsha].data
  117. def lines(content):
  118. if not content:
  119. return []
  120. else:
  121. return content.splitlines(True)
  122. if old_path is None:
  123. old_path = "/dev/null"
  124. else:
  125. old_path = "a/%s" % old_path
  126. if new_path is None:
  127. new_path = "/dev/null"
  128. else:
  129. new_path = "b/%s" % new_path
  130. f.write("diff --git %s %s\n" % (old_path, new_path))
  131. if old_mode != new_mode:
  132. if new_mode is not None:
  133. if old_mode is not None:
  134. f.write("old mode %o\n" % old_mode)
  135. f.write("new mode %o\n" % new_mode)
  136. else:
  137. f.write("deleted mode %o\n" % old_mode)
  138. f.write("index %s..%s" % (shortid(old_id), shortid(new_id)))
  139. if new_mode is not None:
  140. f.write(" %o" % new_mode)
  141. f.write("\n")
  142. old_content = content(old_mode, old_id)
  143. new_content = content(new_mode, new_id)
  144. if is_binary(old_content) or is_binary(new_content):
  145. f.write("Binary files %s and %s differ\n" % (old_path, new_path))
  146. else:
  147. f.writelines(unified_diff(lines(old_content), lines(new_content),
  148. old_path, new_path))
  149. def write_blob_diff(f, (old_path, old_mode, old_blob),
  150. (new_path, new_mode, new_blob)):
  151. """Write diff file header.
  152. :param f: File-like object to write to
  153. :param (old_path, old_mode, old_blob): Previous file (None if nonexisting)
  154. :param (new_path, new_mode, new_blob): New file (None if nonexisting)
  155. :note: The use of write_object_diff is recommended over this function.
  156. """
  157. def blob_id(blob):
  158. if blob is None:
  159. return "0" * 7
  160. else:
  161. return blob.id[:7]
  162. def lines(blob):
  163. if blob is not None:
  164. return blob.data.splitlines(True)
  165. else:
  166. return []
  167. if old_path is None:
  168. old_path = "/dev/null"
  169. else:
  170. old_path = "a/%s" % old_path
  171. if new_path is None:
  172. new_path = "/dev/null"
  173. else:
  174. new_path = "b/%s" % new_path
  175. f.write("diff --git %s %s\n" % (old_path, new_path))
  176. if old_mode != new_mode:
  177. if new_mode is not None:
  178. if old_mode is not None:
  179. f.write("old mode %o\n" % old_mode)
  180. f.write("new mode %o\n" % new_mode)
  181. else:
  182. f.write("deleted mode %o\n" % old_mode)
  183. f.write("index %s..%s" % (blob_id(old_blob), blob_id(new_blob)))
  184. if new_mode is not None:
  185. f.write(" %o" % new_mode)
  186. f.write("\n")
  187. old_contents = lines(old_blob)
  188. new_contents = lines(new_blob)
  189. f.writelines(unified_diff(old_contents, new_contents,
  190. old_path, new_path))
  191. def write_tree_diff(f, store, old_tree, new_tree):
  192. """Write tree diff.
  193. :param f: File-like object to write to.
  194. :param old_tree: Old tree id
  195. :param new_tree: New tree id
  196. """
  197. changes = store.tree_changes(old_tree, new_tree)
  198. for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
  199. write_object_diff(f, store, (oldpath, oldmode, oldsha),
  200. (newpath, newmode, newsha))
  201. def git_am_patch_split(f):
  202. """Parse a git-am-style patch and split it up into bits.
  203. :param f: File-like object to parse
  204. :return: Tuple with commit object, diff contents and git version
  205. """
  206. msg = rfc822.Message(f)
  207. c = Commit()
  208. c.author = msg["from"]
  209. c.committer = msg["from"]
  210. try:
  211. patch_tag_start = msg["subject"].index("[PATCH")
  212. except ValueError:
  213. subject = msg["subject"]
  214. else:
  215. close = msg["subject"].index("] ", patch_tag_start)
  216. subject = msg["subject"][close+2:]
  217. c.message = subject.replace("\n", "") + "\n"
  218. first = True
  219. for l in f:
  220. if l == "---\n":
  221. break
  222. if first:
  223. if l.startswith("From: "):
  224. c.author = l[len("From: "):].rstrip()
  225. else:
  226. c.message += "\n" + l
  227. first = False
  228. else:
  229. c.message += l
  230. diff = ""
  231. for l in f:
  232. if l == "-- \n":
  233. break
  234. diff += l
  235. try:
  236. version = f.next().rstrip("\n")
  237. except StopIteration:
  238. version = None
  239. return c, diff, version