git.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!/usr/bin/env python3
  2. """Git helper functions.
  3. Everything here should be project agnostic: it shouldn't rely on project's
  4. structure, or make assumptions about the passed arguments or calls' outcomes.
  5. """
  6. import io
  7. import os
  8. import posixpath
  9. import re
  10. import subprocess
  11. import sys
  12. SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
  13. sys.path.append(SCRIPT_DIR)
  14. from patches import PATCH_FILENAME_PREFIX, is_patch_location_line
  15. UPSTREAM_HEAD='refs/patches/upstream-head'
  16. def is_repo_root(path):
  17. path_exists = os.path.exists(path)
  18. if not path_exists:
  19. return False
  20. git_folder_path = os.path.join(path, '.git')
  21. git_folder_exists = os.path.exists(git_folder_path)
  22. return git_folder_exists
  23. def get_repo_root(path):
  24. """Finds a closest ancestor folder which is a repo root."""
  25. norm_path = os.path.normpath(path)
  26. norm_path_exists = os.path.exists(norm_path)
  27. if not norm_path_exists:
  28. return None
  29. if is_repo_root(norm_path):
  30. return norm_path
  31. parent_path = os.path.dirname(norm_path)
  32. # Check if we're in the root folder already.
  33. if parent_path == norm_path:
  34. return None
  35. return get_repo_root(parent_path)
  36. def am(repo, patch_data, threeway=False, directory=None, exclude=None,
  37. committer_name=None, committer_email=None, keep_cr=True):
  38. args = []
  39. if threeway:
  40. args += ['--3way']
  41. if directory is not None:
  42. args += ['--directory', directory]
  43. if exclude is not None:
  44. for path_pattern in exclude:
  45. args += ['--exclude', path_pattern]
  46. if keep_cr is True:
  47. # Keep the CR of CRLF in case any patches target files with Windows line
  48. # endings.
  49. args += ['--keep-cr']
  50. root_args = ['-C', repo]
  51. if committer_name is not None:
  52. root_args += ['-c', 'user.name=' + committer_name]
  53. if committer_email is not None:
  54. root_args += ['-c', 'user.email=' + committer_email]
  55. root_args += ['-c', 'commit.gpgsign=false']
  56. command = ['git'] + root_args + ['am'] + args
  57. proc = subprocess.Popen(
  58. command,
  59. stdin=subprocess.PIPE)
  60. proc.communicate(patch_data.encode('utf-8'))
  61. if proc.returncode != 0:
  62. raise RuntimeError("Command {} returned {}".format(command,
  63. proc.returncode))
  64. def import_patches(repo, ref=UPSTREAM_HEAD, **kwargs):
  65. """same as am(), but we save the upstream HEAD so we can refer to it when we
  66. later export patches"""
  67. update_ref(repo=repo, ref=ref, newvalue='HEAD')
  68. am(repo=repo, **kwargs)
  69. def update_ref(repo, ref, newvalue):
  70. args = ['git', '-C', repo, 'update-ref', ref, newvalue]
  71. return subprocess.check_call(args)
  72. def get_commit_for_ref(repo, ref):
  73. args = ['git', '-C', repo, 'rev-parse', '--verify', ref]
  74. return subprocess.check_output(args).decode('utf-8').strip()
  75. def get_commit_count(repo, commit_range):
  76. args = ['git', '-C', repo, 'rev-list', '--count', commit_range]
  77. return int(subprocess.check_output(args).decode('utf-8').strip())
  78. def guess_base_commit(repo, ref):
  79. """Guess which commit the patches might be based on"""
  80. try:
  81. upstream_head = get_commit_for_ref(repo, ref)
  82. num_commits = get_commit_count(repo, upstream_head + '..')
  83. return [upstream_head, num_commits]
  84. except subprocess.CalledProcessError:
  85. args = [
  86. 'git',
  87. '-C',
  88. repo,
  89. 'describe',
  90. '--tags',
  91. ]
  92. return subprocess.check_output(args).decode('utf-8').rsplit('-', 2)[0:2]
  93. def format_patch(repo, since):
  94. args = [
  95. 'git',
  96. '-C',
  97. repo,
  98. '-c',
  99. 'core.attributesfile='
  100. + os.path.join(
  101. os.path.dirname(os.path.realpath(__file__)),
  102. 'electron.gitattributes',
  103. ),
  104. # Ensure it is not possible to match anything
  105. # Disabled for now as we have consistent chunk headers
  106. # '-c',
  107. # 'diff.electron.xfuncname=$^',
  108. 'format-patch',
  109. '--keep-subject',
  110. '--no-stat',
  111. '--stdout',
  112. # Per RFC 3676 the signature is separated from the body by a line with
  113. # '-- ' on it. If the signature option is omitted the signature defaults
  114. # to the Git version number.
  115. '--no-signature',
  116. # The name of the parent commit object isn't useful information in this
  117. # context, so zero it out to avoid needless patch-file churn.
  118. '--zero-commit',
  119. # Some versions of git print out different numbers of characters in the
  120. # 'index' line of patches, so pass --full-index to get consistent
  121. # behaviour.
  122. '--full-index',
  123. since
  124. ]
  125. return subprocess.check_output(args).decode('utf-8')
  126. def split_patches(patch_data):
  127. """Split a concatenated series of patches into N separate patches"""
  128. patches = []
  129. patch_start = re.compile('^From [0-9a-f]+ ')
  130. # Keep line endings in case any patches target files with CRLF.
  131. keep_line_endings = True
  132. for line in patch_data.splitlines(keep_line_endings):
  133. if patch_start.match(line):
  134. patches.append([])
  135. patches[-1].append(line)
  136. return patches
  137. def filter_patches(patches, key):
  138. """Return patches that include the specified key"""
  139. if key is None:
  140. return patches
  141. matches = []
  142. for patch in patches:
  143. if any(key in line for line in patch):
  144. matches.append(patch)
  145. continue
  146. return matches
  147. def munge_subject_to_filename(subject):
  148. """Derive a suitable filename from a commit's subject"""
  149. if subject.endswith('.patch'):
  150. subject = subject[:-6]
  151. return re.sub(r'[^A-Za-z0-9-]+', '_', subject).strip('_').lower() + '.patch'
  152. def get_file_name(patch):
  153. """Return the name of the file to which the patch should be written"""
  154. file_name = None
  155. for line in patch:
  156. if line.startswith(PATCH_FILENAME_PREFIX):
  157. file_name = line[len(PATCH_FILENAME_PREFIX):]
  158. break
  159. # If no patch-filename header, munge the subject.
  160. if not file_name:
  161. for line in patch:
  162. if line.startswith('Subject: '):
  163. file_name = munge_subject_to_filename(line[len('Subject: '):])
  164. break
  165. return file_name.rstrip('\n')
  166. def join_patch(patch):
  167. """Joins and formats patch contents"""
  168. return ''.join(remove_patch_location(patch)).rstrip('\n') + '\n'
  169. def remove_patch_location(patch):
  170. """Strip out the patch location lines from a patch's message body"""
  171. force_keep_next_line = False
  172. n = len(patch)
  173. for i, l in enumerate(patch):
  174. skip_line = is_patch_location_line(l)
  175. skip_next = i < n - 1 and is_patch_location_line(patch[i + 1])
  176. if not force_keep_next_line and (
  177. skip_line or (skip_next and len(l.rstrip()) == 0)
  178. ):
  179. pass # drop this line
  180. else:
  181. yield l
  182. force_keep_next_line = l.startswith('Subject: ')
  183. def export_patches(repo, out_dir,
  184. patch_range=None, ref=UPSTREAM_HEAD,
  185. dry_run=False, grep=None):
  186. if not os.path.exists(repo):
  187. sys.stderr.write(
  188. "Skipping patches in {} because it does not exist.\n".format(repo)
  189. )
  190. return
  191. if patch_range is None:
  192. patch_range, num_patches = guess_base_commit(repo, ref)
  193. sys.stderr.write("Exporting {} patches in {} since {}\n".format(
  194. num_patches, repo, patch_range[0:7]))
  195. patch_data = format_patch(repo, patch_range)
  196. patches = split_patches(patch_data)
  197. if grep:
  198. olen = len(patches)
  199. patches = filter_patches(patches, grep)
  200. sys.stderr.write("Exporting {} of {} patches\n".format(len(patches), olen))
  201. try:
  202. os.mkdir(out_dir)
  203. except OSError:
  204. pass
  205. if dry_run:
  206. # If we're doing a dry run, iterate through each patch and see if the newly
  207. # exported patch differs from what exists. Report number of mismatched
  208. # patches and fail if there's more than one.
  209. bad_patches = []
  210. for patch in patches:
  211. filename = get_file_name(patch)
  212. filepath = posixpath.join(out_dir, filename)
  213. existing_patch = str(io.open(filepath, 'rb').read(), 'utf-8')
  214. formatted_patch = join_patch(patch)
  215. if formatted_patch != existing_patch:
  216. bad_patches.append(filename)
  217. if len(bad_patches) > 0:
  218. sys.stderr.write(
  219. "Patches in {} not up to date: {} patches need update\n-- {}\n".format(
  220. out_dir, len(bad_patches), "\n-- ".join(bad_patches)
  221. )
  222. )
  223. sys.exit(1)
  224. else:
  225. # Remove old patches so that deleted commits are correctly reflected in the
  226. # patch files (as a removed file)
  227. for p in os.listdir(out_dir):
  228. if p.endswith('.patch'):
  229. os.remove(posixpath.join(out_dir, p))
  230. with io.open(
  231. posixpath.join(out_dir, '.patches'),
  232. 'w',
  233. newline='\n',
  234. encoding='utf-8',
  235. ) as pl:
  236. for patch in patches:
  237. filename = get_file_name(patch)
  238. file_path = posixpath.join(out_dir, filename)
  239. formatted_patch = join_patch(patch)
  240. # Write in binary mode to retain mixed line endings on write.
  241. with io.open(
  242. file_path, 'wb'
  243. ) as f:
  244. f.write(formatted_patch.encode('utf-8'))
  245. pl.write(filename + '\n')