]>
git.ipfire.org Git - thirdparty/openembedded/openembedded-core.git/blob - scripts/patchtest-get-branch
3 # Get target branch from the corresponding mbox
5 # NOTE: this script was based on patches coming to the openembedded-core
6 # where target branch is defined inside brackets as subject prefix
7 # i.e. [master], [rocko], etc.
9 # Copyright (C) 2016 Intel Corporation
11 # SPDX-License-Identifier: GPL-2.0-only
19 re_prefix
= re
.compile(r
"(\[.*\])", re
.DOTALL
)
21 def get_branch(filepath_repo
, filepath_mbox
, default_branch
):
24 # get all remotes branches
25 gitbranches
= git
.Git(filepath_repo
).branch('-a').splitlines()
27 # from gitbranches, just get the names
28 branches
= [b
.split('/')[-1] for b
in gitbranches
]
30 subject
= ' '.join(mailbox
.mbox(filepath_mbox
)[0]['subject'].splitlines())
32 # we expect that patches will have somewhere between one and three
33 # consecutive sets of square brackets with tokens inside, e.g.:
36 # 3. [OE-core][kirkstone][PATCH]
37 # Some of them may also be part of a series, in which case the PATCH
38 # token will be formatted like:
40 # or they will be revisions to previous patches, where it will be:
42 # Or they may contain both:
44 # In any case, we want mprefix to contain all of these tokens so
45 # that we can search for branch names within them.
46 mprefix
= re
.findall(r
'\[.*?\]', subject
)
49 # Iterate over the tokens and compare against the branch list to
50 # figure out which one the patch is targeting
52 stripped
= token
.lower().strip('[]')
53 if default_branch
in stripped
:
54 found_branch
= default_branch
57 for branch
in branches
:
58 # ignore branches named "core"
59 if branch
!= "core" and stripped
.rfind(branch
) != -1:
60 found_branch
= token
.split(' ')[0].strip('[]')
63 # if there's no mprefix content or no known branches were found in
64 # the tokens, assume the target is master
65 if found_branch
is None:
66 found_branch
= "master"
68 return (subject
, found_branch
)
70 if __name__
== '__main__':
72 parser
= argparse
.ArgumentParser()
73 parser
.add_argument('repo', metavar
='REPO', help='Main repository')
74 parser
.add_argument('mbox', metavar
='MBOX', help='mbox filename')
75 parser
.add_argument('--default-branch', metavar
='DEFAULT_BRANCH', default
='master', help='Use this branch if no one is found')
76 parser
.add_argument('--separator', '-s', metavar
='SEPARATOR', default
=' ', help='Char separator for output data')
77 args
= parser
.parse_args()
79 subject
, branch
= get_branch(args
.repo
, args
.mbox
, args
.default_branch
)
80 print("branch: %s" % branch
)