blob: 864979d48f96786264bb2026a8edbad9970874cb [file] [log] [blame]
Fathi Boudra422bf772019-12-02 11:10:16 +02001#!/usr/bin/env python3
2#
Leonardo Sandoval579c7372020-10-23 15:23:32 -05003# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
Fathi Boudra422bf772019-12-02 11:10:16 +02004#
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": {
Tomás González2955a742025-08-05 14:09:19 +010027 "rusted-firmware": gerrit.GerritProject("trusted-firmware/rusted-firmware-a", gerrit_arm),
Fathi Boudra422bf772019-12-02 11:10:16 +020028 "trusted-firmware": gerrit.GerritProject("pdcs-platforms/ap/tf-topics", gerrit_arm),
29 "trusted-firmware-tf": gerrit.GerritProject("trusted-firmware/tf-a-tests", gerrit_arm),
30 "trusted-firmware-ci": gerrit.GerritProject("pdswinf/ci/pdcs-platforms/platform-ci", gerrit_arm),
Zelalem219df412020-05-17 19:21:20 -050031 "cc_plugin": gerrit.GerritProject("tests/lava/test-definitions.git", gerrit_arm),
Fathi Boudra422bf772019-12-02 11:10:16 +020032 "scp": gerrit.GerritProject("scp/firmware", gerrit_arm),
Olivier Deprez965a7792019-12-16 14:09:03 +010033 "spm": gerrit.GerritProject("trusted-firmware/spm", gerrit_arm),
Manish V Badarkhe41909452025-04-11 12:06:45 +010034 "tf-rmm": gerrit.GerritProject("trusted-firmware/tf-rmm", gerrit_arm),
Jimmy Brisson0d5e12c2023-05-16 14:51:51 -050035 "tf-m-tests": gerrit.GerritProject("iot-sw/tf-m/internal/tf-m-tests", gerrit_arm),
36 "tf-m-extras": gerrit.GerritProject("iot-sw/tf-m/internal/tf-m-extras", gerrit_arm),
Fathi Boudra422bf772019-12-02 11:10:16 +020037 },
38
39 # Projects hosted on trustedfirmware.org Gerrit server.
40 "tforg": {
Tomás González67121af2025-07-23 17:18:36 +010041 "rusted-firmware": gerrit.GerritProject("RF-A/rusted-firmware-a", gerrit_tforg),
Tomás González2955a742025-08-05 14:09:19 +010042 "trusted-firmware": gerrit.GerritProject("TF-A/trusted-firmware-a", gerrit_tforg),
Fathi Boudra422bf772019-12-02 11:10:16 +020043 "trusted-firmware-tf": gerrit.GerritProject("TF-A/tf-a-tests", gerrit_tforg),
Zelalem11ea6e02020-08-18 14:49:51 -050044 "trusted-firmware-ci": gerrit.GerritProject("ci/tf-a-ci-scripts", gerrit_tforg),
Olivier Deprez965a7792019-12-16 14:09:03 +010045 "spm": gerrit.GerritProject("hafnium/hafnium", gerrit_tforg),
Manish V Badarkhe41909452025-04-11 12:06:45 +010046 "tf-rmm": gerrit.GerritProject("TF-RMM/tf-rmm", gerrit_tforg),
Jimmy Brisson0d5e12c2023-05-16 14:51:51 -050047 "tf-m-tests": gerrit.GerritProject("TF-M/tf-m-tests", gerrit_tforg),
48 "tf-m-extras": gerrit.GerritProject("TF-M/tf-m-extras", gerrit_tforg),
Fathi Boudra422bf772019-12-02 11:10:16 +020049 },
50}
51
52# Argument setup
53parser = argparse.ArgumentParser()
54parser.add_argument("--project", "-p",
55 help="Gerrit project identifier this refspec belongs to")
56parser.add_argument("--server", "-s", help="Gerrit server hosting this project",
57 choices=["arm", "tforg"])
58parser.add_argument("--user", "-u",
59 help="Username to use to query the Gerrit server")
60parser.add_argument("--key", "-k",
61 help="SSH private key to use to authenticate with the Gerrit server")
62parser.add_argument("refspec", help="Refspec to translate")
63opts = parser.parse_args()
64
65project = projects[opts.server][opts.project]
66
67# Default action: print refspec and exit
68def do_default():
69 print(opts.refspec)
70 sys.exit(0)
71
72def print_topic_tip(query_results):
73 patchsets = []
74 parents = []
75
76 # For each change, get its most recent patchset
77 for change in query_results:
78 patchsets.append(change["patchSets"][-1])
79
80 # For each patchset, get its parent commit
81 for patchset in patchsets:
82 parents.append(patchset["parents"][0])
83
84 # If a patchset's revision is NOT in the list of parents then it should
85 # be the tip commit
86 tips = list(filter(lambda x: x["revision"] not in parents, patchsets))
87
88 # There must be only one patchset remaining, otherwise the tip is ambiguous
89 if len(tips) > 1:
90 raise Exception("{} in {} has no unique tip commit.".format(opts.refspec,
91 opts.project))
92 if len(tips) == 0:
93 raise Exception("No tip commit found for {} in {}.".format(opts.refspec,
94 opts.project))
95 # Print the reference of the topic tip patchset
96 print(tips[0]["ref"])
97
98query = ["status:open"]
99
100# If we don't understand the refspec, that's OK. We don't translate it, but
101# print it as is.
102try:
103 scheme, rest = opts.refspec.split(":")
104 if scheme == "topic":
105 query += ["topic:" + rest]
106 elif scheme == "change":
107 query += [opts.refspec]
108 else:
109 do_default()
110except:
111 do_default()
112
113changes = project.query(query, username=opts.user, keyfile=opts.key)
114
115# The last object is a summary; drop it as it's not of interest to us.
116changes.pop()
117
118if not changes:
119 raise Exception("{} for {} resolved to nothing.".format(opts.refspec,
120 opts.project))
121
122if scheme == "topic":
123 if len(changes) > 1:
124 print_topic_tip(changes)
125 else:
126 print(changes[0]["currentPatchSet"]["ref"])
127elif scheme == "change":
128 if len(changes) > 1:
129 # When querying for a specific change there must be just a single result
130 raise Exception("{} for {} did not resolve uniquely.".format(opts.refspec,
131 opts.project))
132 print(changes[0]["currentPatchSet"]["revision"])