add --reverse option to go from log URL to review URL

Change-Id: I9f70d954f21e96767cd5be40dd3484d1a5fa3742
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2017-10-02 11:50:20 -04:00
parent 3284c05947
commit b8f173b5f0
1 changed files with 47 additions and 11 deletions

View File

@ -37,6 +37,13 @@ def main():
default=False,
help='show the URL but do not open it',
)
parser.add_argument(
'-r', '--reverse',
dest='reverse',
action='store_true',
default=False,
help='given a log URL, show the gerrit URL',
)
parser.add_argument(
'ref',
nargs='?',
@ -46,22 +53,51 @@ def main():
args = parser.parse_args()
ref = args.ref
try:
ref_hash = subprocess.check_output(
['git', 'show-ref', '-s', ref]
).decode('utf-8').rstrip()
except subprocess.CalledProcessError:
# Maybe they gave us a commit id
if args.reverse:
# A log URL looks something like:
# http://logs.openstack.org/c4/c4afbe14deee6f55378cda53e624c8c6aa9a9d08/release-post/tag-releases/2ea6052/
try:
sha = ref.split('/')[4]
except IndexError:
parser.abort(
'Could not parse log URL {}'.format(ref)
)
try:
parents = subprocess.check_output(
['git', 'rev-list', '--parents', '-n1', sha]
).decode('utf-8').rstrip().split(' ')
except subprocess.CalledProcessError:
parser.abort(
'Could not determine parents of {}'.format(sha)
)
if len(parents) == 2:
# This commit was merged directly into the branch without
# a separate merge commit. We want to show it, rather than
# a parent.
commit = parents[0]
else:
# The commit is a merge commit, so we need to show the
# parent that has the actual patch.
commit = parents[-1]
url = 'https://review.openstack.org/#/q/commit:' + commit
else:
try:
ref_hash = subprocess.check_output(
['git', 'show', '--pretty=format:%H', '--quiet', ref]
['git', 'show-ref', '-s', ref]
).decode('utf-8').rstrip()
except subprocess.CalledProcessError:
sys.stderr.write('Could not get hash for ref %r\n' % ref)
return 1
# Maybe they gave us a commit id
try:
ref_hash = subprocess.check_output(
['git', 'show', '--pretty=format:%H', '--quiet', ref]
).decode('utf-8').rstrip()
except subprocess.CalledProcessError:
sys.stderr.write('Could not get hash for ref %r\n' % ref)
return 1
ref_hash_str = ref_hash.decode('utf8')
url = '%s/%s/%s/' % (args.base, ref_hash_str[:2], ref_hash_str)
ref_hash_str = ref_hash.decode('utf8')
url = '%s/%s/%s/' % (args.base, ref_hash_str[:2], ref_hash_str)
if args.url:
print(url)
else: