From: Pieter Lexis Date: Tue, 7 Nov 2017 20:29:09 +0000 (+0100) Subject: Add script to cherry-pick PR commits for backports X-Git-Tag: auth-4.1.0~26^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e46c6218693d3f710c05d4ec4e13e11f5d84ce6;p=thirdparty%2Fpdns.git Add script to cherry-pick PR commits for backports --- diff --git a/build-scripts/cherry-pick-pr.py b/build-scripts/cherry-pick-pr.py new file mode 100755 index 0000000000..c455d81159 --- /dev/null +++ b/build-scripts/cherry-pick-pr.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +import requests +import sys +import subprocess +import argparse + + +def get_commits(pr): + try: + res = requests.get('https://api.github.com/repos/PowerDNS/pdns/pulls/' + '{}/commits'.format(pr)).json() + return [c['sha'] for c in res] + except (ValueError, requests.exceptions.HTTPError) as e: + print(e) + sys.exit(1) + + +def run_command(cmd): + try: + subprocess.check_call(cmd) + except subprocess.CalledProcessError as e: + print(e) + sys.exit(1) + + +a = argparse.ArgumentParser() +action = a.add_mutually_exclusive_group(required=True) +action.add_argument( + '-b', '--backport-unto', metavar='REF', nargs=1, help='Backport, using ' + 'cherry-pick, all commits from PULL_REQUEST onto REF. This is done on a ' + 'branch called "backport-PULL_REQUEST". When the cherry-pick fails, solve ' + 'the conflict as usual and run "git cherry-pick --continue --allow-empty"') +action.add_argument( + '-m', '--merge-into', metavar='REF', nargs=1, help='Take the backport-' + 'PULL_REQUEST branch and merge it into REF') +a.add_argument( + 'pull_request', metavar='PULL_REQUEST', type=int, + help='The PR number to backport') + +args = a.parse_args() + +if args.backport_unto: + command = ['git', 'checkout', '-b', + 'backport-{}'.format(args.pull_request), args.backport_unto[0]] + run_command(command) + + commits = get_commits(args.pull_request) + command = ['git', 'cherry-pick', '-x', '--allow-empty'] + commits + run_command(command) + +if args.merge_into: + command = ['git', 'checkout', args.merge_into[0]] + run_command(command) + + command = ['git', 'merge', '--no-ff', + 'backport-{}'.format(args.pull_request), '-m', + 'Backport #{}'.format(args.pull_request)] + run_command(command)