blob: 83fd30e8e11cfd2f34774d35551bde054bab0246 [file] [log] [blame]
Arthur She3eb254e2022-09-18 20:41:03 -07001#!/usr/bin/env python3
2#
3# Copyright (c) 2019-2022 Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8# This script parse the gerrit query results from the stdin
9# and return the correct refspec
10
11import sys
12import json
13
14def print_topic_tip(query_results):
15 patchsets = []
16 parents = []
17 project = query_results[0]["project"]
18 topic = query_results[0]["topic"]
19
20 # For each change, get its most recent patchset
21 for change in query_results:
22 patchsets.append(change["patchSets"][-1])
23
24 # For each patchset, get its parent commit
25 for patchset in patchsets:
26 parents.append(patchset["parents"][0])
27
28 # If a patchset's revision is NOT in the list of parents then it should
29 # be the tip commit
30 tips = list(filter(lambda x: x["revision"] not in parents, patchsets))
31
32 # There must be only one patchset remaining, otherwise the tip is ambiguous
33 if len(tips) > 1:
34 raise Exception("{} in {} has no unique tip commit.".format(topic, project))
35 if len(tips) == 0:
36 raise Exception("No tip commit found for {} in {}.".format(topic, project))
37 # Print the reference of the topic tip patchset
38 print(tips[0]["ref"])
39
40try:
41 changes = [json.loads(resp_line) for resp_line in sys.stdin]
42except:
43 raise Exception("Input error, it's not a JSON string!")
44
45# The last object is a summary; drop it as it's not of interest to us.
46changes.pop()
47
48if not changes:
49 raise Exception("Can not find anything.")
50
51if len(changes) > 1:
52 print_topic_tip(changes)
53else:
54 print(changes[0]["currentPatchSet"]["ref"])