blob: 9e6b370cdd8221c397f500b0502dcbe76ccf30b2 [file] [log] [blame]
Fathi Boudra422bf772019-12-02 11:10:16 +02001#!/usr/bin/env python3
2#
3# Copyright (c) 2019, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8# This scripts translates certain accepted refspec schemes to something that can
9# be used on git command line. For example, given the refspec 'topic:foo/bar'
10# for a given project, this script translates and prints the full commit hash.
11#
12# If a scheme is not recognized, print the received refspec unchanged.
13
14import argparse
15import gerrit
16import sys
17
18# Gerrit servers we care about.
19gerrit_arm = gerrit.GerritServer("gerrit.oss.arm.com")
20gerrit_tforg = gerrit.GerritServer("review.trustedfirmware.org")
21
22# Trusted Firmware-A and associated projects.
23# Different projects are hosted on different Gerrit servers.
24projects = {
25 # Projects hosted on Arm Gerrit server.
26 "arm": {
27 "trusted-firmware": gerrit.GerritProject("pdcs-platforms/ap/tf-topics", gerrit_arm),
28 "trusted-firmware-tf": gerrit.GerritProject("trusted-firmware/tf-a-tests", gerrit_arm),
29 "trusted-firmware-ci": gerrit.GerritProject("pdswinf/ci/pdcs-platforms/platform-ci", gerrit_arm),
30 "scp": gerrit.GerritProject("scp/firmware", gerrit_arm),
31 },
32
33 # Projects hosted on trustedfirmware.org Gerrit server.
34 "tforg": {
35 "trusted-firmware": gerrit.GerritProject("TF-A/trusted-firmware-a", gerrit_tforg),
36 "trusted-firmware-tf": gerrit.GerritProject("TF-A/tf-a-tests", gerrit_tforg),
37 },
38}
39
40# Argument setup
41parser = argparse.ArgumentParser()
42parser.add_argument("--project", "-p",
43 help="Gerrit project identifier this refspec belongs to")
44parser.add_argument("--server", "-s", help="Gerrit server hosting this project",
45 choices=["arm", "tforg"])
46parser.add_argument("--user", "-u",
47 help="Username to use to query the Gerrit server")
48parser.add_argument("--key", "-k",
49 help="SSH private key to use to authenticate with the Gerrit server")
50parser.add_argument("refspec", help="Refspec to translate")
51opts = parser.parse_args()
52
53project = projects[opts.server][opts.project]
54
55# Default action: print refspec and exit
56def do_default():
57 print(opts.refspec)
58 sys.exit(0)
59
60def print_topic_tip(query_results):
61 patchsets = []
62 parents = []
63
64 # For each change, get its most recent patchset
65 for change in query_results:
66 patchsets.append(change["patchSets"][-1])
67
68 # For each patchset, get its parent commit
69 for patchset in patchsets:
70 parents.append(patchset["parents"][0])
71
72 # If a patchset's revision is NOT in the list of parents then it should
73 # be the tip commit
74 tips = list(filter(lambda x: x["revision"] not in parents, patchsets))
75
76 # There must be only one patchset remaining, otherwise the tip is ambiguous
77 if len(tips) > 1:
78 raise Exception("{} in {} has no unique tip commit.".format(opts.refspec,
79 opts.project))
80 if len(tips) == 0:
81 raise Exception("No tip commit found for {} in {}.".format(opts.refspec,
82 opts.project))
83 # Print the reference of the topic tip patchset
84 print(tips[0]["ref"])
85
86query = ["status:open"]
87
88# If we don't understand the refspec, that's OK. We don't translate it, but
89# print it as is.
90try:
91 scheme, rest = opts.refspec.split(":")
92 if scheme == "topic":
93 query += ["topic:" + rest]
94 elif scheme == "change":
95 query += [opts.refspec]
96 else:
97 do_default()
98except:
99 do_default()
100
101changes = project.query(query, username=opts.user, keyfile=opts.key)
102
103# The last object is a summary; drop it as it's not of interest to us.
104changes.pop()
105
106if not changes:
107 raise Exception("{} for {} resolved to nothing.".format(opts.refspec,
108 opts.project))
109
110if scheme == "topic":
111 if len(changes) > 1:
112 print_topic_tip(changes)
113 else:
114 print(changes[0]["currentPatchSet"]["ref"])
115elif scheme == "change":
116 if len(changes) > 1:
117 # When querying for a specific change there must be just a single result
118 raise Exception("{} for {} did not resolve uniquely.".format(opts.refspec,
119 opts.project))
120 print(changes[0]["currentPatchSet"]["revision"])