diff options
Diffstat (limited to 'scripts/patchtest-get-branch')
| -rwxr-xr-x | scripts/patchtest-get-branch | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/scripts/patchtest-get-branch b/scripts/patchtest-get-branch new file mode 100755 index 0000000000..9415de98ef --- /dev/null +++ b/scripts/patchtest-get-branch | |||
| @@ -0,0 +1,92 @@ | |||
| 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 | # This program is free software; you can redistribute it and/or modify | ||
| 12 | # it under the terms of the GNU General Public License version 2 as | ||
| 13 | # published by the Free Software Foundation. | ||
| 14 | # | ||
| 15 | # This program is distributed in the hope that it will be useful, | ||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 18 | # GNU General Public License for more details. | ||
| 19 | # | ||
| 20 | # You should have received a copy of the GNU General Public License along | ||
| 21 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
| 22 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 23 | |||
| 24 | import mailbox | ||
| 25 | import argparse | ||
| 26 | import re | ||
| 27 | import git | ||
| 28 | import sys | ||
| 29 | |||
| 30 | re_prefix = re.compile("(\[.*\])", re.DOTALL) | ||
| 31 | |||
| 32 | def get_branch(filepath_repo, filepath_mbox, default_branch): | ||
| 33 | branch = None | ||
| 34 | |||
| 35 | # get all remotes branches | ||
| 36 | gitbranches = git.Git(filepath_repo).branch('-a').splitlines() | ||
| 37 | |||
| 38 | # from gitbranches, just get the names | ||
| 39 | branches = [b.split('/')[-1] for b in gitbranches] | ||
| 40 | |||
| 41 | subject = ' '.join(mailbox.mbox(filepath_mbox)[0]['subject'].splitlines()) | ||
| 42 | |||
| 43 | # we expect that patches will have somewhere between one and three | ||
| 44 | # consecutive sets of square brackets with tokens inside, e.g.: | ||
| 45 | # 1. [PATCH] | ||
| 46 | # 2. [OE-core][PATCH] | ||
| 47 | # 3. [OE-core][kirkstone][PATCH] | ||
| 48 | # Some of them may also be part of a series, in which case the PATCH | ||
| 49 | # token will be formatted like: | ||
| 50 | # [PATCH 1/4] | ||
| 51 | # or they will be revisions to previous patches, where it will be: | ||
| 52 | # [PATCH v2] | ||
| 53 | # Or they may contain both: | ||
| 54 | # [PATCH v2 3/4] | ||
| 55 | # In any case, we want mprefix to contain all of these tokens so | ||
| 56 | # that we can search for branch names within them. | ||
| 57 | mprefix = re.findall(r'\[.*?\]', subject) | ||
| 58 | found_branch = None | ||
| 59 | if mprefix: | ||
| 60 | # Iterate over the tokens and compare against the branch list to | ||
| 61 | # figure out which one the patch is targeting | ||
| 62 | for token in mprefix: | ||
| 63 | stripped = token.lower().strip('[]') | ||
| 64 | if default_branch in stripped: | ||
| 65 | found_branch = default_branch | ||
| 66 | break | ||
| 67 | else: | ||
| 68 | for branch in branches: | ||
| 69 | # ignore branches named "core" | ||
| 70 | if branch != "core" and stripped.rfind(branch) != -1: | ||
| 71 | found_branch = token.split(' ')[0].strip('[]') | ||
| 72 | break | ||
| 73 | |||
| 74 | # if there's no mprefix content or no known branches were found in | ||
| 75 | # the tokens, assume the target is master | ||
| 76 | if found_branch is None: | ||
| 77 | found_branch = "master" | ||
| 78 | |||
| 79 | return (subject, found_branch) | ||
| 80 | |||
| 81 | if __name__ == '__main__': | ||
| 82 | |||
| 83 | parser = argparse.ArgumentParser() | ||
| 84 | parser.add_argument('repo', metavar='REPO', help='Main repository') | ||
| 85 | parser.add_argument('mbox', metavar='MBOX', help='mbox filename') | ||
| 86 | parser.add_argument('--default-branch', metavar='DEFAULT_BRANCH', default='master', help='Use this branch if no one is found') | ||
| 87 | parser.add_argument('--separator', '-s', metavar='SEPARATOR', default=' ', help='Char separator for output data') | ||
| 88 | args = parser.parse_args() | ||
| 89 | |||
| 90 | subject, branch = get_branch(args.repo, args.mbox, args.default_branch) | ||
| 91 | print("branch: %s" % branch) | ||
| 92 | |||
