OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import argparse | 6 import argparse |
7 import re | 7 import re |
8 import sys | 8 import sys |
9 | 9 |
10 from collections import defaultdict | 10 from collections import defaultdict |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 message = git.run('log', '-1', '--format=%B', branch) | 55 message = git.run('log', '-1', '--format=%B', branch) |
56 footers = parse_footers(message) | 56 footers = parse_footers(message) |
57 git_svn_id = get_unique(footers, 'git-svn-id') | 57 git_svn_id = get_unique(footers, 'git-svn-id') |
58 if git_svn_id: | 58 if git_svn_id: |
59 match = GIT_SVN_ID_PATTERN.match(git_svn_id) | 59 match = GIT_SVN_ID_PATTERN.match(git_svn_id) |
60 if match: | 60 if match: |
61 svn_id = match.group(1) | 61 svn_id = match.group(1) |
62 return svn_id | 62 return svn_id |
63 | 63 |
64 | 64 |
| 65 def get_footer_change_id(message): |
| 66 """Returns a list of Gerrit's ChangeId from given commit message.""" |
| 67 return parse_footers(message).get(normalize_name('Change-Id'), []) |
| 68 |
| 69 |
| 70 def add_footer_change_id(message, change_id): |
| 71 """Returns message with Change-ID footer in it. |
| 72 |
| 73 Assumes that Change-Id is not yet in footers, which is then |
| 74 inserted after any of these footers: Bug|Issue|Test|Feature. |
| 75 """ |
| 76 assert 0 == len(get_footer_change_id(message)) |
| 77 change_id_line = 'Change-Id: %s' % change_id |
| 78 # This code does the same as parse_footers, but keeps track of line |
| 79 # numbers so that ChangeId is inserted in the right place. |
| 80 lines = message.splitlines() |
| 81 footer_lines = [] |
| 82 for line in reversed(lines): |
| 83 if line == '' or line.isspace(): |
| 84 break |
| 85 footer_lines.append(line) |
| 86 # footers order is from end to start of the message. |
| 87 footers = map(parse_footer, footer_lines) |
| 88 if not all(footers): |
| 89 lines.append('') |
| 90 lines.append(change_id_line) |
| 91 else: |
| 92 after = set(map(normalize_name, ['Bug', 'Issue', 'Test', 'Feature'])) |
| 93 for i, (key, _) in enumerate(footers): |
| 94 if normalize_name(key) in after: |
| 95 insert_at = len(lines) - i |
| 96 break |
| 97 else: |
| 98 insert_at = len(lines) - len(footers) |
| 99 lines.insert(insert_at, change_id_line) |
| 100 return '\n'.join(lines) |
| 101 |
| 102 |
65 def get_unique(footers, key): | 103 def get_unique(footers, key): |
66 key = normalize_name(key) | 104 key = normalize_name(key) |
67 values = footers[key] | 105 values = footers[key] |
68 assert len(values) <= 1, 'Multiple %s footers' % key | 106 assert len(values) <= 1, 'Multiple %s footers' % key |
69 if values: | 107 if values: |
70 return values[0] | 108 return values[0] |
71 else: | 109 else: |
72 return None | 110 return None |
73 | 111 |
74 | 112 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 print '%s: %s' % (k, v) | 193 print '%s: %s' % (k, v) |
156 return 0 | 194 return 0 |
157 | 195 |
158 | 196 |
159 if __name__ == '__main__': | 197 if __name__ == '__main__': |
160 try: | 198 try: |
161 sys.exit(main(sys.argv[1:])) | 199 sys.exit(main(sys.argv[1:])) |
162 except KeyboardInterrupt: | 200 except KeyboardInterrupt: |
163 sys.stderr.write('interrupted\n') | 201 sys.stderr.write('interrupted\n') |
164 sys.exit(1) | 202 sys.exit(1) |
OLD | NEW |