From a9767889fc331935a5b29c45ff3de27de06f3502 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Wed, 17 Apr 2019 12:11:53 +0200 Subject: [PATCH 1/4] backport_pr: make error branch handling more idempotent Otherwise, when an error occurs (e.g. credentials wrong on git push) the worktree and the release branch still exists, which might be hard to remove for a newcomer not knowing about `git worktree`. --- dist/tools/backport_pr/backport_pr.py | 32 ++++++++++++++++++--------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/dist/tools/backport_pr/backport_pr.py b/dist/tools/backport_pr/backport_pr.py index 91ce72f91f..22a98db670 100755 --- a/dist/tools/backport_pr/backport_pr.py +++ b/dist/tools/backport_pr/backport_pr.py @@ -173,17 +173,27 @@ def main(): 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 + print("Pushing branch {} to origin".format(new_branch)) + 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'] From 989e2c6303cf71f6ee72d5c9188cdd1cafc795ed Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Wed, 17 Apr 2019 12:33:00 +0200 Subject: [PATCH 2/4] backport_pr: add function to find remote by URL --- dist/tools/backport_pr/backport_pr.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/dist/tools/backport_pr/backport_pr.py b/dist/tools/backport_pr/backport_pr.py index 22a98db670..19b7553c0a 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): From a7459e7463ea4a63eaaa1a0b3f19ea0a64788ad3 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Wed, 17 Apr 2019 12:42:02 +0200 Subject: [PATCH 3/4] backport_pr: don't assume devel remote to be 'origin' --- dist/tools/backport_pr/backport_pr.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dist/tools/backport_pr/backport_pr.py b/dist/tools/backport_pr/backport_pr.py index 19b7553c0a..37db6bc2db 100755 --- a/dist/tools/backport_pr/backport_pr.py +++ b/dist/tools/backport_pr/backport_pr.py @@ -185,9 +185,10 @@ def main(): for commit in commits: bp_repo.git.cherry_pick('-x', commit['sha']) # 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: - 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)) From 1930368fd588fdeef6aa3ae25ad86b5fe18232d2 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Wed, 17 Apr 2019 12:44:21 +0200 Subject: [PATCH 4/4] backport_pr: exit early when backport branch already exists --- dist/tools/backport_pr/backport_pr.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dist/tools/backport_pr/backport_pr.py b/dist/tools/backport_pr/backport_pr.py index 37db6bc2db..ce474766af 100755 --- a/dist/tools/backport_pr/backport_pr.py +++ b/dist/tools/backport_pr/backport_pr.py @@ -174,6 +174,9 @@ 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,