2018-04-03 21:25:15 +00:00
|
|
|
#!/usr/bin/env python2
|
2017-12-18 17:01:18 +00:00
|
|
|
# Copyright 2017 Google Inc.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
import httplib
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
def retrieve_changeid(commit_or_branch):
|
|
|
|
b = subprocess.check_output(['git', 'log', '-1', '--format=%B', commit_or_branch])
|
|
|
|
r = re.compile(r'^Change-Id: (.*)$')
|
|
|
|
for l in b.split('\n'):
|
|
|
|
m = r.match(l)
|
|
|
|
if m:
|
|
|
|
return m.group(1)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def gerrit_change_id_to_number(cid):
|
|
|
|
conn = httplib.HTTPSConnection('skia-review.googlesource.com')
|
|
|
|
conn.request('GET', '/changes/?q=change:%s' % cid)
|
|
|
|
r = conn.getresponse()
|
|
|
|
assert(r.status == 200)
|
|
|
|
x = r.read()
|
|
|
|
i = 0
|
|
|
|
while i < len(x) and x[i] != '[':
|
|
|
|
i += 1
|
2018-04-03 21:25:15 +00:00
|
|
|
return json.loads(x[i:])[0]['_number']
|
2017-12-18 17:01:18 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
if len(sys.argv) == 2 and len(sys.argv[1]) == 41 and sys.argv[1][0] == 'I':
|
|
|
|
gerrit_change_id_to_number(sys.argv[1])
|
|
|
|
else:
|
|
|
|
changeid = retrieve_changeid(sys.argv[1] if len(sys.argv) == 2 else 'HEAD')
|
|
|
|
if changeid is None:
|
|
|
|
exit(2)
|
2018-04-03 21:25:15 +00:00
|
|
|
sys.stdout.write('%d\n' % gerrit_change_id_to_number(changeid))
|
2017-12-18 17:01:18 +00:00
|
|
|
except:
|
|
|
|
exit(1)
|