From b8f173b5f0445775c61e14127b23d373b5d6e554 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Mon, 2 Oct 2017 11:50:20 -0400 Subject: [PATCH] add --reverse option to go from log URL to review URL Change-Id: I9f70d954f21e96767cd5be40dd3484d1a5fa3742 Signed-off-by: Doug Hellmann --- git_os_job/cmd.py | 58 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/git_os_job/cmd.py b/git_os_job/cmd.py index 16120bd..8d610ac 100755 --- a/git_os_job/cmd.py +++ b/git_os_job/cmd.py @@ -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: