diff --git a/dist/tools/backport_pr/backport_pr.py b/dist/tools/backport_pr/backport_pr.py index 91ce72f91f..ce474766af 100755 --- a/dist/tools/backport_pr/backport_pr.py +++ b/dist/tools/backport_pr/backport_pr.py @@ -74,11 +74,17 @@ def _get_latest_release(branches): return (release_short, release_fullname) -def _get_upstream(repo): +def _find_remote(repo, user, repo_name): for remote in repo.remotes: - if (remote.url.endswith("{}/{}.git".format(ORG, REPO)) or - remote.url.endswith("{}/{}".format(ORG, REPO))): + if (remote.url.endswith("{}/{}.git".format(user, repo_name)) or + remote.url.endswith("{}/{}".format(user, repo_name))): 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): @@ -168,22 +174,36 @@ def main(): # Build topic branch in temp dir new_branch = args.backport_branch_fmt.format(release=release_shortname, 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) repo.git.worktree("add", "-b", new_branch, WORKTREE_SUBDIR, "{}/{}".format(upstream_remote, release_fullname)) - bp_repo = git.Repo(worktree_dir) - # Apply commits - for commit in commits: - bp_repo.git.cherry_pick('-x', commit['sha']) - # Push to github - print("Pushing branch {} to origin".format(new_branch)) - if not args.noop: - repo.git.push('origin', '{0}:{0}'.format(new_branch)) - # Delete worktree - print("Pruning temporary workdir at {}".format(worktree_dir)) - _delete_worktree(repo, worktree_dir) + try: + bp_repo = git.Repo(worktree_dir) + # Apply commits + for commit in commits: + bp_repo.git.cherry_pick('-x', commit['sha']) + # Push to github + origin = _find_remote(repo, username, REPO) + print("Pushing branch {} to {}".format(new_branch, origin)) + if not args.noop: + 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 + print("Pruning temporary workdir at {}".format(worktree_dir)) + _delete_worktree(repo, worktree_dir) labels = _get_labels(pulldata) merger = pulldata['merged_by']['login']