Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Get target branch from the corresponding mbox |
| 4 | # |
| 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. |
| 8 | # |
| 9 | # Copyright (C) 2016 Intel Corporation |
| 10 | # |
| 11 | # SPDX-License-Identifier: GPL-2.0-only |
| 12 | # |
| 13 | |
| 14 | import mailbox |
| 15 | import argparse |
| 16 | import re |
| 17 | import git |
| 18 | |
| 19 | re_prefix = re.compile("(\[.*\])", re.DOTALL) |
| 20 | |
| 21 | def get_branch(filepath_repo, filepath_mbox, default_branch): |
| 22 | branch = None |
| 23 | |
| 24 | # get all remotes branches |
| 25 | gitbranches = git.Git(filepath_repo).branch('-a').splitlines() |
| 26 | |
| 27 | # from gitbranches, just get the names |
| 28 | branches = [b.split('/')[-1] for b in gitbranches] |
| 29 | |
| 30 | subject = ' '.join(mailbox.mbox(filepath_mbox)[0]['subject'].splitlines()) |
| 31 | |
| 32 | # we expect that patches will have somewhere between one and three |
| 33 | # consecutive sets of square brackets with tokens inside, e.g.: |
| 34 | # 1. [PATCH] |
| 35 | # 2. [OE-core][PATCH] |
| 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: |
| 39 | # [PATCH 1/4] |
| 40 | # or they will be revisions to previous patches, where it will be: |
| 41 | # [PATCH v2] |
| 42 | # Or they may contain both: |
| 43 | # [PATCH v2 3/4] |
| 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) |
| 47 | found_branch = None |
| 48 | if mprefix: |
| 49 | # Iterate over the tokens and compare against the branch list to |
| 50 | # figure out which one the patch is targeting |
| 51 | for token in mprefix: |
| 52 | stripped = token.lower().strip('[]') |
| 53 | if default_branch in stripped: |
| 54 | found_branch = default_branch |
| 55 | break |
| 56 | else: |
| 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('[]') |
| 61 | break |
| 62 | |
| 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" |
| 67 | |
| 68 | return (subject, found_branch) |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | |
| 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() |
| 78 | |
| 79 | subject, branch = get_branch(args.repo, args.mbox, args.default_branch) |
| 80 | print("branch: %s" % branch) |
| 81 | |