Merge pull request #11409 from miri64/dist/enh/backport_pr_fixes

dist/tools/backport_pr: several improvements
This commit is contained in:
Gaëtan Harter 2019-04-18 14:55:03 +02:00 committed by GitHub
commit a0ef287f82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -74,11 +74,17 @@ def _get_latest_release(branches):
return (release_short, release_fullname) return (release_short, release_fullname)
def _get_upstream(repo): def _find_remote(repo, user, repo_name):
for remote in repo.remotes: for remote in repo.remotes:
if (remote.url.endswith("{}/{}.git".format(ORG, REPO)) or if (remote.url.endswith("{}/{}.git".format(user, repo_name)) or
remote.url.endswith("{}/{}".format(ORG, REPO))): remote.url.endswith("{}/{}".format(user, repo_name))):
return remote return remote
raise ValueError("Could not find remote with URL ending in {}/{}.git"
.format(user, repo_name))
def _get_upstream(repo):
return _find_remote(repo, ORG, REPO)
def _delete_worktree(repo, workdir): def _delete_worktree(repo, workdir):
@ -168,19 +174,33 @@ def main():
# Build topic branch in temp dir # Build topic branch in temp dir
new_branch = args.backport_branch_fmt.format(release=release_shortname, new_branch = args.backport_branch_fmt.format(release=release_shortname,
origbranch=orig_branch) origbranch=orig_branch)
if new_branch in repo.branches:
print("ERROR: Branch {} already exists".format(new_branch))
sys.exit(1)
worktree_dir = os.path.join(args.gitdir, WORKTREE_SUBDIR) worktree_dir = os.path.join(args.gitdir, WORKTREE_SUBDIR)
repo.git.worktree("add", "-b", repo.git.worktree("add", "-b",
new_branch, new_branch,
WORKTREE_SUBDIR, WORKTREE_SUBDIR,
"{}/{}".format(upstream_remote, release_fullname)) "{}/{}".format(upstream_remote, release_fullname))
try:
bp_repo = git.Repo(worktree_dir) bp_repo = git.Repo(worktree_dir)
# Apply commits # Apply commits
for commit in commits: for commit in commits:
bp_repo.git.cherry_pick('-x', commit['sha']) bp_repo.git.cherry_pick('-x', commit['sha'])
# Push to github # Push to github
print("Pushing branch {} to origin".format(new_branch)) origin = _find_remote(repo, username, REPO)
print("Pushing branch {} to {}".format(new_branch, origin))
if not args.noop: if not args.noop:
repo.git.push('origin', '{0}:{0}'.format(new_branch)) repo.git.push(origin, '{0}:{0}'.format(new_branch))
except Exception as exc:
# Delete worktree
print("Pruning temporary workdir at {}".format(worktree_dir))
_delete_worktree(repo, worktree_dir)
# also delete branch created by worktree; this is only possible after
# the worktree was deleted
repo.delete_head(new_branch)
raise exc
else:
# Delete worktree # Delete worktree
print("Pruning temporary workdir at {}".format(worktree_dir)) print("Pruning temporary workdir at {}".format(worktree_dir))
_delete_worktree(repo, worktree_dir) _delete_worktree(repo, worktree_dir)