From f74057ece96708a5ae53b923109ee4d6de887911 Mon Sep 17 00:00:00 2001 From: Matthew Thompson Date: Fri, 14 Feb 2020 13:20:24 -0500 Subject: [PATCH] Fixes #33. Adds a new checkout-if-exists command This command is run like: ``` mepo checkout-if-exists ``` It then checks every repository for that branch and if it's found, it checks it out. --- mepo.d/cmdline/parser.py | 8 ++++++++ .../checkout-if-exists/checkout-if-exists.py | 15 +++++++++++++++ mepo.d/repository/git.py | 5 +++++ mepo.d/utilities/shellcmd.py | 8 ++++++-- 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 mepo.d/command/checkout-if-exists/checkout-if-exists.py diff --git a/mepo.d/cmdline/parser.py b/mepo.d/cmdline/parser.py index 712eaf09..b2c182e4 100644 --- a/mepo.d/cmdline/parser.py +++ b/mepo.d/cmdline/parser.py @@ -21,6 +21,7 @@ def parse(self): self.__status() self.__diff() self.__checkout() + self.__checkout_if_exists() self.__branch() self.__develop() self.__compare() @@ -73,6 +74,13 @@ def __checkout(self): checkout.add_argument('comp_name', metavar = 'comp-name', nargs = '+') checkout.add_argument('-b', action = 'store_true', help = 'create the branch') + def __checkout_if_exists(self): + checkout_if_exists = self.subparsers.add_parser( + 'checkout-if-exists', + description = 'Switch to branch in any component where it is present. ') + checkout_if_exists.add_argument('branch_name', metavar = 'branch-name') + checkout_if_exists.add_argument('--verbose', action = 'store_true', help = 'verbose') + def __branch(self): branch = self.subparsers.add_parser('branch') MepoBranchArgParser(branch) diff --git a/mepo.d/command/checkout-if-exists/checkout-if-exists.py b/mepo.d/command/checkout-if-exists/checkout-if-exists.py new file mode 100644 index 00000000..64755cda --- /dev/null +++ b/mepo.d/command/checkout-if-exists/checkout-if-exists.py @@ -0,0 +1,15 @@ +from state.state import MepoState +from utilities import verify +from repository.git import GitRepository + +def run(args): + allcomps = MepoState.read_state() + for comp in allcomps: + git = GitRepository(comp.remote, comp.local) + branch = args.branch_name + status = git.verify_branch(branch) + + if status == 0: + if args.verbose: + print("Found branch [%s] in repository [%s]" % (branch, comp.name)) + git.checkout(branch) diff --git a/mepo.d/repository/git.py b/mepo.d/repository/git.py index 5335c8d4..a573912c 100644 --- a/mepo.d/repository/git.py +++ b/mepo.d/repository/git.py @@ -62,6 +62,11 @@ def delete_branch(self, branch_name, force): cmd = self.__git + ' branch {} {}'.format(delete, branch_name) shellcmd.run(cmd.split()) + def verify_branch(self, branch_name): + cmd = self.__git + ' show-branch remotes/origin/{}'.format(branch_name) + status = shellcmd.run(cmd.split(),status=True) + return status + def check_status(self): cmd = self.__git + ' status --porcelain=v2' output = shellcmd.run(cmd.split(), output=True) diff --git a/mepo.d/utilities/shellcmd.py b/mepo.d/utilities/shellcmd.py index f8f06ada..642fd6a2 100644 --- a/mepo.d/utilities/shellcmd.py +++ b/mepo.d/utilities/shellcmd.py @@ -1,14 +1,18 @@ import subprocess as sp -def run(cmd, output=None): +def run(cmd, output=None, status=None): result = sp.run( cmd, stdout = sp.PIPE, stderr = sp.PIPE, universal_newlines = True # result byte sequence -> string ) - if result.returncode != 0: + + if status: + return result.returncode + elif result.returncode != 0: print(result.stderr) result.check_returncode() + if output: return result.stdout + result.stderr