2 # Copyright 2017 Google Inc.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
12 # TODO(halcanary): document functions and script usage.
14 def retrieve_changeid(commit_or_branch):
16 cmd = ['git', 'log', '-1', '--format=%B', commit_or_branch, '--']
17 body = subprocess.check_output(cmd)
19 raise Exception('git not found')
20 except subprocess.CalledProcessError:
21 raise Exception('`%s` failed' % ' '.join(cmd))
22 match = re.search(r'^Change-Id: *(.*) *$', body, re.MULTILINE)
24 raise Exception('Change-Id field missing from commit %s' % commit_or_branch)
28 def gerrit_change_id_to_number(site, cid):
29 url = 'https://%s/changes/?q=change:%s' % (site, cid)
31 content = urllib.urlopen(url).read()
33 raise Exception('error reading "%s"' % url)
35 parsed = json.loads(content[content.find('['):])
37 raise Exception('unable to parse content\n"""\n%s\n"""' % content)
39 return parsed[0]['_number']
40 except (IndexError, KeyError):
41 raise Exception('Content missing\n"""\n%s\n"""' %
42 json.dumps(parsed, indent=2))
45 def args_to_changeid(argv):
46 if len(argv) == 2 and len(argv[1]) == 41 and argv[1][0] == 'I':
49 return retrieve_changeid(argv[1] if len(argv) == 2 else 'HEAD')
52 if __name__ == '__main__':
54 sys.stdout.write('%d\n' %
55 gerrit_change_id_to_number('skia-review.googlesource.com',
56 args_to_changeid(sys.argv)))
57 except Exception as e:
58 sys.stderr.write('%s\n' % e)