From 623fda22b87bd3c35cca2263a9c7f272687d6e67 Mon Sep 17 00:00:00 2001 From: Martin Moss Date: Wed, 24 Aug 2022 16:38:11 +0100 Subject: [PATCH] add concept of seperator to allow feature_ and feature/ for branch names also unfortunately black formatted at the same time --- gitfeatures/core.py | 58 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/gitfeatures/core.py b/gitfeatures/core.py index 1840b58..56f996a 100644 --- a/gitfeatures/core.py +++ b/gitfeatures/core.py @@ -8,6 +8,7 @@ from six.moves import input master_branch = os.environ.get("GITFEATURES_MASTER_BRANCH", "master") +seperator = os.environ.get("GITFEATURES_BRANCH_SEPERATOR", "_") repo = os.environ.get("GITFEATURES_REPO", "github") merge_strategy = os.environ.get("GITFEATURES_STRATEGY", "merge") @@ -16,7 +17,14 @@ def _call(args): try: return check_output(args).decode("utf-8") except CalledProcessError: - sys.exit(__name__ + ": none zero exit status executing: " + " ".join(args)) # noqa + sys.exit( + __name__ + ": none zero exit status executing: " + " ".join(args) + ) # noqa + + +def _get_branch_name(prefix, name): + name = f"{prefix}{seperator}{name}" + return name def new_feature(name, prefix): @@ -33,10 +41,12 @@ def new_feature(name, prefix): sys.exit("Ok, Exiting") # noqa _call(["git", "remote", "update", "origin"]) - new_branch = "%s_%s" % (prefix, name) + new_branch = _get_branch_name(prefix, name) if _branch_exists(new_branch): - sys.exit(__name__ + ": local or remote branch already exists: " + new_branch) # noqa + sys.exit( + __name__ + ": local or remote branch already exists: " + new_branch + ) # noqa _call(["git", "checkout", "-b", new_branch]) _call(["git", "push", "-u", "origin", new_branch + ":" + new_branch]) @@ -46,18 +56,22 @@ def finish_feature(name, prefix): cur_branch = _current_branch() if name: - branch = prefix + "_" + name + branch = _get_branch_name(prefix, name) if branch == cur_branch: _call(["git", "checkout", master_branch]) elif cur_branch != master_branch: branch = cur_branch _call(["git", "checkout", master_branch]) else: - sys.exit(__name__ + ": please provide a branch name if on {}".format(master_branch)) + sys.exit( + __name__ + ": please provide a branch name if on {}".format(master_branch) + ) _call(["git", "remote", "update", "origin"]) - commits = _call(["git", "log", "--oneline", branch, "^origin/{}".format(master_branch)]) + commits = _call( + ["git", "log", "--oneline", branch, "^origin/{}".format(master_branch)] + ) if commits: sys.exit( __name__ @@ -75,14 +89,16 @@ def finish_feature(name, prefix): def _branch_func(branch_type, args): if len(args) > 0 and args[0] == "new": date = datetime.datetime.now() - new_branch = "{}_{}".format(branch_type, date.strftime("%Y%m%d")) + branch = _get_branch_name(branch_type, date.strftime("%Y%m%d")) _call(["git", "checkout", "-b", new_branch]) _call(["git", "push", "-u", "origin", new_branch + ":" + new_branch]) branches = _get_branches(branch_type) if len(branches) > 3: - print(f"you have more than 3 {branch_type} branches, shall I delete the eldest one? [y/n]") # noqa + print( + f"you have more than 3 {branch_type} branches, shall I delete the eldest one? [y/n]" + ) # noqa if input().lower() == "y": branch = branches[0] _call(["git", "push", "origin", "--delete", branch]) @@ -116,10 +132,14 @@ def pullrequest(args): # check its up to date with remote master if not pull _call(["git", "remote", "update", "origin"]) - commits = _call(["git", "log", "--oneline", "^" + branch, "origin/{}".format(master_branch)]) + commits = _call( + ["git", "log", "--oneline", "^" + branch, "origin/{}".format(master_branch)] + ) if commits: print( - "Your branch is behind origin/{} so cannot be automatically {}d.".format(merge_strategy, master_branch) + "Your branch is behind origin/{} so cannot be automatically {}d.".format( + merge_strategy, master_branch + ) ) # noqa print(commits) print( @@ -133,9 +153,15 @@ def pullrequest(args): _call(["git", "checkout", branch]) try: print("git {} {}".format(merge_strategy, master_branch)) - output = check_output(["git", merge_strategy, master_branch]).decode("utf-8") + output = check_output(["git", merge_strategy, master_branch]).decode( + "utf-8" + ) print(output) - print("Congratulations, successfully {}d {}".format(merge_strategy, master_branch)) + print( + "Congratulations, successfully {}d {}".format( + merge_strategy, master_branch + ) + ) except CalledProcessError as e: if "CONFLICT" in e.output: err = ( @@ -158,7 +184,9 @@ def pullrequest(args): origin = _call(["git", "config", "--get", "remote.origin.url"]) name = origin.split(":")[1].replace(".git\n", "") url = _get_pullrequest_url(name, branch) - if (len(args) > 0 and args[0] == "--dry-run") or os.environ.get("CONSOLEONLY", False): # noqa + if (len(args) > 0 and args[0] == "--dry-run") or os.environ.get( + "CONSOLEONLY", False + ): # noqa print(url) else: webbrowser.open_new_tab(url) @@ -169,7 +197,9 @@ def _get_pullrequest_url(name, branch): if repo == "github": url = "https://github.com/" + name + "/pull/new/" + branch elif repo == "bitbucket": - url = "https://bitbucket.org/" + name + "/pull-requests/new?t=1&source=" + branch # noqa + url = ( + "https://bitbucket.org/" + name + "/pull-requests/new?t=1&source=" + branch + ) # noqa return url