git_footers.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/usr/bin/env python3
  2. # Copyright 2014 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import argparse
  6. import json
  7. import re
  8. import sys
  9. from collections import defaultdict
  10. import git_common as git
  11. FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): *(.*)$')
  12. CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/\-\.]+)@{#(\d+)}$')
  13. FOOTER_KEY_BLOCKLIST = set(['http', 'https'])
  14. def normalize_name(header):
  15. return '-'.join([word.title() for word in header.strip().split('-')])
  16. def parse_footer(line):
  17. """Returns footer's (key, value) if footer is valid, else None."""
  18. match = FOOTER_PATTERN.match(line)
  19. if match and match.group(1) not in FOOTER_KEY_BLOCKLIST:
  20. return (match.group(1), match.group(2))
  21. return None
  22. def parse_footers(message):
  23. """Parses a git commit message into a multimap of footers."""
  24. _, _, parsed_footers = split_footers(message)
  25. footer_map = defaultdict(list)
  26. if parsed_footers:
  27. # Read footers from bottom to top, because latter takes precedense,
  28. # and we want it to be first in the multimap value.
  29. for (k, v) in reversed(parsed_footers):
  30. footer_map[normalize_name(k)].append(v.strip())
  31. return footer_map
  32. def matches_footer_key(line, key):
  33. """Returns whether line is a valid footer whose key matches a given one.
  34. Keys are compared in normalized form.
  35. """
  36. r = parse_footer(line)
  37. if r is None:
  38. return False
  39. return normalize_name(r[0]) == normalize_name(key)
  40. def split_footers(message):
  41. """Returns (non_footer_lines, footer_lines, parsed footers).
  42. Guarantees that:
  43. (non_footer_lines + footer_lines) ~= message.splitlines(), with at
  44. most one new newline, if the last paragraph is text followed by
  45. footers.
  46. parsed_footers is parse_footer applied on each line of footer_lines.
  47. There could be fewer parsed_footers than footer lines if some lines
  48. in last paragraph are malformed.
  49. """
  50. message_lines = list(message.rstrip().splitlines())
  51. footer_lines = []
  52. maybe_footer_lines = []
  53. for line in reversed(message_lines):
  54. if line == '' or line.isspace():
  55. break
  56. if parse_footer(line):
  57. footer_lines.extend(maybe_footer_lines)
  58. maybe_footer_lines = []
  59. footer_lines.append(line)
  60. else:
  61. # We only want to include malformed lines if they are preceded by
  62. # well-formed lines. So keep them in holding until we see a
  63. # well-formed line (case above).
  64. maybe_footer_lines.append(line)
  65. else:
  66. # The whole description was consisting of footers,
  67. # which means those aren't footers.
  68. footer_lines = []
  69. footer_lines.reverse()
  70. footers = [footer for footer in map(parse_footer, footer_lines) if footer]
  71. if not footers:
  72. return message_lines, [], []
  73. if maybe_footer_lines:
  74. # If some malformed lines were left over, add a newline to split them
  75. # from the well-formed ones.
  76. return message_lines[:-len(footer_lines)] + [''], footer_lines, footers
  77. return message_lines[:-len(footer_lines)], footer_lines, footers
  78. def get_footer_change_id(message):
  79. """Returns a list of Gerrit's ChangeId from given commit message."""
  80. return parse_footers(message).get(normalize_name('Change-Id'), [])
  81. def add_footer_change_id(message, change_id):
  82. """Returns message with Change-ID footer in it.
  83. Assumes that Change-Id is not yet in footers, which is then inserted at
  84. earliest footer line which is after all of these footers:
  85. Bug|Issue|Test|Feature.
  86. """
  87. assert 'Change-Id' not in parse_footers(message)
  88. return add_footer(message,
  89. 'Change-Id',
  90. change_id,
  91. after_keys=['Bug', 'Issue', 'Test', 'Feature'])
  92. def add_footer(message, key, value, after_keys=None, before_keys=None):
  93. """Returns a message with given footer appended.
  94. If after_keys and before_keys are both None (default), appends footer last.
  95. If after_keys is provided and matches footers already present, inserts
  96. footer as *early* as possible while still appearing after all provided
  97. keys, even if doing so conflicts with before_keys.
  98. If before_keys is provided, inserts footer as late as possible while still
  99. appearing before all provided keys.
  100. For example, given
  101. message='Header.\n\nAdded: 2016\nBug: 123\nVerified-By: CQ'
  102. after_keys=['Bug', 'Issue']
  103. the new footer will be inserted between Bug and Verified-By existing
  104. footers.
  105. """
  106. assert key == normalize_name(key), 'Use normalized key'
  107. new_footer = '%s: %s' % (key, value)
  108. if not FOOTER_PATTERN.match(new_footer):
  109. raise ValueError('Invalid footer %r' % new_footer)
  110. top_lines, footer_lines, _ = split_footers(message)
  111. if not footer_lines:
  112. if not top_lines or top_lines[-1] != '':
  113. top_lines.append('')
  114. footer_lines = [new_footer]
  115. else:
  116. after_keys = set(map(normalize_name, after_keys or []))
  117. after_indices = [
  118. footer_lines.index(x) for x in footer_lines for k in after_keys
  119. if matches_footer_key(x, k)
  120. ]
  121. before_keys = set(map(normalize_name, before_keys or []))
  122. before_indices = [
  123. footer_lines.index(x) for x in footer_lines for k in before_keys
  124. if matches_footer_key(x, k)
  125. ]
  126. if after_indices:
  127. # after_keys takes precedence, even if there's a conflict.
  128. insert_idx = max(after_indices) + 1
  129. elif before_indices:
  130. insert_idx = min(before_indices)
  131. else:
  132. insert_idx = len(footer_lines)
  133. footer_lines.insert(insert_idx, new_footer)
  134. return '\n'.join(top_lines + footer_lines)
  135. def remove_footer(message, key):
  136. """Returns a message with all instances of given footer removed."""
  137. key = normalize_name(key)
  138. top_lines, footer_lines, _ = split_footers(message)
  139. if not footer_lines:
  140. return message
  141. new_footer_lines = []
  142. for line in footer_lines:
  143. try:
  144. f = normalize_name(parse_footer(line)[0])
  145. if f != key:
  146. new_footer_lines.append(line)
  147. except TypeError:
  148. # If the footer doesn't parse (i.e. is malformed), just let it carry
  149. # over.
  150. new_footer_lines.append(line)
  151. return '\n'.join(top_lines + new_footer_lines)
  152. def get_unique(footers, key):
  153. key = normalize_name(key)
  154. values = footers[key]
  155. assert len(values) <= 1, 'Multiple %s footers' % key
  156. if values:
  157. return values[0]
  158. return None
  159. def get_position(footers):
  160. """Get the commit position from the footers multimap using a heuristic.
  161. Returns:
  162. A tuple of the branch and the position on that branch. For example,
  163. Cr-Commit-Position: refs/heads/main@{#292272}
  164. would give the return value ('refs/heads/main', 292272).
  165. """
  166. position = get_unique(footers, 'Cr-Commit-Position')
  167. if position:
  168. match = CHROME_COMMIT_POSITION_PATTERN.match(position)
  169. assert match, 'Invalid Cr-Commit-Position value: %s' % position
  170. return (match.group(1), match.group(2))
  171. raise ValueError('Unable to infer commit position from footers')
  172. def main(args):
  173. parser = argparse.ArgumentParser(
  174. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  175. parser.add_argument('ref',
  176. nargs='?',
  177. help='Git ref to retrieve footers from.'
  178. ' Omit to parse stdin.')
  179. g = parser.add_mutually_exclusive_group()
  180. g.add_argument('--key',
  181. metavar='KEY',
  182. help='Get all values for the given footer name, one per '
  183. 'line (case insensitive)')
  184. g.add_argument('--position', action='store_true')
  185. g.add_argument('--position-ref', action='store_true')
  186. g.add_argument('--position-num', action='store_true')
  187. g.add_argument('--json',
  188. help='filename to dump JSON serialized footers to.')
  189. opts = parser.parse_args(args)
  190. if opts.ref:
  191. message = git.run('log', '-1', '--format=%B', opts.ref)
  192. else:
  193. message = sys.stdin.read()
  194. footers = parse_footers(message)
  195. if opts.key:
  196. for v in footers.get(normalize_name(opts.key), []):
  197. print(v)
  198. elif opts.position:
  199. pos = get_position(footers)
  200. print('%s@{#%s}' % (pos[0], pos[1] or '?'))
  201. elif opts.position_ref:
  202. print(get_position(footers)[0])
  203. elif opts.position_num:
  204. pos = get_position(footers)
  205. assert pos[1], 'No valid position for commit'
  206. print(pos[1])
  207. elif opts.json:
  208. with open(opts.json, 'w') as f:
  209. json.dump(footers, f)
  210. else:
  211. for k in footers.keys():
  212. for v in footers[k]:
  213. print('%s: %s' % (k, v))
  214. return 0
  215. if __name__ == '__main__':
  216. try:
  217. sys.exit(main(sys.argv[1:]))
  218. except KeyboardInterrupt:
  219. sys.stderr.write('interrupted\n')
  220. sys.exit(1)