blob: ddc9045edb5f602208c7c39e2ff0e6e6ba26086d [file] [log] [blame]
Hal Canaryd2ded552017-12-18 12:01:18 -05001#!/usr/bin/env python
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.
5
6import httplib
7import json
8import re
9import subprocess
10import sys
11
12def retrieve_changeid(commit_or_branch):
13 b = subprocess.check_output(['git', 'log', '-1', '--format=%B', commit_or_branch])
14 r = re.compile(r'^Change-Id: (.*)$')
15 for l in b.split('\n'):
16 m = r.match(l)
17 if m:
18 return m.group(1)
19 return None
20
21def gerrit_change_id_to_number(cid):
22 conn = httplib.HTTPSConnection('skia-review.googlesource.com')
23 conn.request('GET', '/changes/?q=change:%s' % cid)
24 r = conn.getresponse()
25 assert(r.status == 200)
26 x = r.read()
27 i = 0
28 while i < len(x) and x[i] != '[':
29 i += 1
30 print json.loads(x[i:])[0]['_number']
31
32if __name__ == '__main__':
33 try:
34 if len(sys.argv) == 2 and len(sys.argv[1]) == 41 and sys.argv[1][0] == 'I':
35 gerrit_change_id_to_number(sys.argv[1])
36 else:
37 changeid = retrieve_changeid(sys.argv[1] if len(sys.argv) == 2 else 'HEAD')
38 if changeid is None:
39 exit(2)
40 gerrit_change_id_to_number(changeid)
41 except:
42 exit(1)