blob: 0e1b87b0a9baec30c9be1cbd304e57d70e6b5f81 [file] [log] [blame]
Manish V Badarkhe62a10a42022-09-28 19:08:44 +01001#!/usr/bin/env python3
2#
3# Copyright (c) 2019-2020 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
18 # For each change, get its most recent patchset
19 for change in query_results:
20 patchsets.append(change["patchSets"][-1])
21
22 # For each patchset, get its parent commit
23 for patchset in patchsets:
24 parents.append(patchset["parents"][0])
25
26 # If a patchset's revision is NOT in the list of parents then it should
27 # be the tip commit
28 tips = list(filter(lambda x: x["revision"] not in parents, patchsets))
29
30 # There must be only one patchset remaining, otherwise the tip is ambiguous
31 if len(tips) > 1:
32 raise Exception("Has no unique tip commit.")
33 if len(tips) == 0:
34 raise Exception("No tip commit found.")
35 # Print the reference of the topic tip patchset
36 print(tips[0]["ref"])
37
38try:
39 changes = [json.loads(resp_line) for resp_line in sys.stdin]
40except:
41 raise Exception("Input error, it's not a JSON string!")
42
43# The last object is a summary; drop it as it's not of interest to us.
44changes.pop()
45
46if not changes:
47 raise Exception("resolved to nothing.")
48
49if len(changes) > 1:
50 print_topic_tip(changes)
51else:
52 print(changes[0]["currentPatchSet"]["ref"])