Home | History | Annotate | Download | only in tools
      1 #!/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 
      6 import httplib
      7 import json
      8 import re
      9 import subprocess
     10 import sys
     11 
     12 def 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 
     21 def 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 
     32 if __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)
     43