Skip to content

Commit

Permalink
Merge pull request #34 from GEOS-ESM/feature/mathomp4/#33-checkout-if…
Browse files Browse the repository at this point in the history
…-exists

Fixes #33. Adds a new checkout-if-exists command
  • Loading branch information
tclune authored Feb 19, 2020
2 parents bf0c848 + f74057e commit e34d7e4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
8 changes: 8 additions & 0 deletions mepo.d/cmdline/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def parse(self):
self.__status()
self.__diff()
self.__checkout()
self.__checkout_if_exists()
self.__branch()
self.__develop()
self.__compare()
Expand Down Expand Up @@ -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 <branch-name> 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)
Expand Down
15 changes: 15 additions & 0 deletions mepo.d/command/checkout-if-exists/checkout-if-exists.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions mepo.d/repository/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions mepo.d/utilities/shellcmd.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e34d7e4

Please sign in to comment.