git_footers.py 6.18 KB
Newer Older
1 2 3 4 5 6
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
7
import json
8 9 10 11 12 13 14
import re
import sys

from collections import defaultdict

import git_common as git

15

16
FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): (.*)$')
17
CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/\-\.]+)@{#(\d+)}$')
18

19

20 21 22 23 24
def normalize_name(header):
  return '-'.join([ word.title() for word in header.strip().split('-') ])


def parse_footer(line):
25
  """Returns footer's (key, value) if footer is valid, else None."""
26 27 28 29 30 31 32 33 34
  match = FOOTER_PATTERN.match(line)
  if match:
    return (match.group(1), match.group(2))
  else:
    return None


def parse_footers(message):
  """Parses a git commit message into a multimap of footers."""
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  _, _, parsed_footers = split_footers(message)
  footer_map = defaultdict(list)
  if parsed_footers:
    # Read footers from bottom to top, because latter takes precedense,
    # and we want it to be first in the multimap value.
    for (k, v) in reversed(parsed_footers):
      footer_map[normalize_name(k)].append(v.strip())
  return footer_map


def split_footers(message):
  """Returns (non_footer_lines, footer_lines, parsed footers).

  Guarantees that:
    (non_footer_lines + footer_lines) == message.splitlines().
    parsed_footers is parse_footer applied on each line of footer_lines.
  """
  message_lines = list(message.splitlines())
53
  footer_lines = []
54
  for line in reversed(message_lines):
55 56 57
    if line == '' or line.isspace():
      break
    footer_lines.append(line)
58 59 60 61
  else:
    # The whole description was consisting of footers,
    # which means those aren't footers.
    footer_lines = []
62

63
  footer_lines.reverse()
64
  footers = map(parse_footer, footer_lines)
65 66 67
  if not footer_lines or not all(footers):
    return message_lines, [], []
  return message_lines[:-len(footer_lines)], footer_lines, footers
68 69


70 71 72 73 74 75 76 77
def get_footer_change_id(message):
  """Returns a list of Gerrit's ChangeId from given commit message."""
  return parse_footers(message).get(normalize_name('Change-Id'), [])


def add_footer_change_id(message, change_id):
  """Returns message with Change-ID footer in it.

78 79 80
  Assumes that Change-Id is not yet in footers, which is then inserted at
  earliest footer line which is after all of these footers:
    Bug|Issue|Test|Feature.
81
  """
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  assert 'Change-Id' not in parse_footers(message)
  return add_footer(message, 'Change-Id', change_id,
                    after_keys=['Bug', 'Issue', 'Test', 'Feature'])

def add_footer(message, key, value, after_keys=None):
  """Returns a message with given footer appended.

  If after_keys is None (default), appends footer last.
  Otherwise, after_keys must be iterable of footer keys, then the new footer
  would be inserted at the topmost position such there would be no footer lines
  after it with key matching one of after_keys.
  For example, given
      message='Header.\n\nAdded: 2016\nBug: 123\nVerified-By: CQ'
      after_keys=['Bug', 'Issue']
  the new footer will be inserted between Bug and Verified-By existing footers.
  """
  assert key == normalize_name(key), 'Use normalized key'
  new_footer = '%s: %s' % (key, value)

  top_lines, footer_lines, parsed_footers = split_footers(message)
  if not footer_lines:
    if not top_lines or top_lines[-1] != '':
      top_lines.append('')
    footer_lines = [new_footer]
  elif not after_keys:
    footer_lines.append(new_footer)
108
  else:
109 110 111 112 113
    after_keys = set(map(normalize_name, after_keys))
    # Iterate from last to first footer till we find the footer keys above.
    for i, (key, _) in reversed(list(enumerate(parsed_footers))):
      if normalize_name(key) in after_keys:
        footer_lines.insert(i + 1, new_footer)
114 115
        break
    else:
116 117
      footer_lines.insert(0, new_footer)
  return '\n'.join(top_lines + footer_lines)
118 119


120 121 122 123 124 125 126 127 128 129 130
def get_unique(footers, key):
  key = normalize_name(key)
  values = footers[key]
  assert len(values) <= 1, 'Multiple %s footers' % key
  if values:
    return values[0]
  else:
    return None


def get_position(footers):
131
  """Get the commit position from the footers multimap using a heuristic.
132 133 134 135 136 137

  Returns:
    A tuple of the branch and the position on that branch. For example,

    Cr-Commit-Position: refs/heads/master@{#292272}

138
    would give the return value ('refs/heads/master', 292272).
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  """

  position = get_unique(footers, 'Cr-Commit-Position')
  if position:
    match = CHROME_COMMIT_POSITION_PATTERN.match(position)
    assert match, 'Invalid Cr-Commit-Position value: %s' % position
    return (match.group(1), match.group(2))

  raise ValueError('Unable to infer commit position from footers')


def main(args):
  parser = argparse.ArgumentParser(
    formatter_class=argparse.ArgumentDefaultsHelpFormatter
  )
154 155
  parser.add_argument('ref', nargs='?', help="Git ref to retrieve footers from."
                      " Omit to parse stdin.")
156 157 158 159 160 161 162 163

  g = parser.add_mutually_exclusive_group()
  g.add_argument('--key', metavar='KEY',
                 help='Get all values for the given footer name, one per '
                 'line (case insensitive)')
  g.add_argument('--position', action='store_true')
  g.add_argument('--position-ref', action='store_true')
  g.add_argument('--position-num', action='store_true')
164
  g.add_argument('--json', help="filename to dump JSON serialized headers to.")
165 166 167 168


  opts = parser.parse_args(args)

169 170 171 172 173
  if opts.ref:
    message = git.run('log', '-1', '--format=%B', opts.ref)
  else:
    message = '\n'.join(l for l in sys.stdin)

174 175 176 177 178 179 180 181 182 183 184 185 186 187
  footers = parse_footers(message)

  if opts.key:
    for v in footers.get(normalize_name(opts.key), []):
      print v
  elif opts.position:
    pos = get_position(footers)
    print '%s@{#%s}' % (pos[0], pos[1] or '?')
  elif opts.position_ref:
    print get_position(footers)[0]
  elif opts.position_num:
    pos = get_position(footers)
    assert pos[1], 'No valid position for commit'
    print pos[1]
188 189 190
  elif opts.json:
    with open(opts.json, 'w') as f:
      json.dump(footers, f)
191 192 193 194
  else:
    for k in footers.keys():
      for v in footers[k]:
        print '%s: %s' % (k, v)
195
  return 0
196 197 198


if __name__ == '__main__':
199 200 201 202 203
  try:
    sys.exit(main(sys.argv[1:]))
  except KeyboardInterrupt:
    sys.stderr.write('interrupted\n')
    sys.exit(1)