diff --git a/dc-ops b/dc-ops index 34ef7c1..b354e28 100755 --- a/dc-ops +++ b/dc-ops @@ -40,6 +40,44 @@ def run_subprocess(command: str, workdir: str) -> bool: return True +def update_git_repo(repo: str) -> bool: + """update repo and check for new commits + + Args: + repo (str): path to git repo + + Returns: + bool: False if any step fails + """ + # create repo instance if it exists + try: + repo = Repo(stackdir) + except NoSuchPathError: + log.error("directory not found") + return False + + # try to fetch latest changes + try: + repo.git.fetch() + except GitCommandError as e: + log.error(str(e)) + return False + + # check for new commits + if repo.rev_parse("HEAD") == repo.rev_parse(f"origin/{repo.active_branch}"): + log.info("no changes - skipping") + return False + + # pull remote changes to local branch + try: + log.info(repo.git.pull()) + except GitCommandError as e: + log.error(str(e)) + return False + + return True + + # iterate all stacks for stack in cfg["stacks"]: # skip disabled stacks @@ -50,30 +88,8 @@ for stack in cfg["stacks"]: stackdir = stack["dir"] log.info(f"* processing {stackdir}") - # create repo instance if it exists - try: - repo = Repo(stackdir) - except NoSuchPathError: - log.error("directory not found") - continue - - # try to fetch latest changes - try: - repo.git.fetch() - except GitCommandError as e: - log.error(str(e)) - continue - - # check for new commits - if repo.rev_parse("HEAD") == repo.rev_parse(f"origin/{repo.active_branch}"): - log.info("no changes - skipping") - continue - - # pull remote changes to local branch - try: - log.info(repo.git.pull()) - except GitCommandError as e: - log.error(str(e)) + # update repo and check for new commits + if not update_git_repo(stackdir): continue # run pre-command