From 7ca3da52d917bac1e3456e119f0719e9dc31eb8f Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Tue, 19 Dec 2023 23:05:35 +0100 Subject: [PATCH] refactor: move git repo handling to function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - move git repo update logic into a separate function - add error handling for each step in the update process - return early if any step in the update process fails - call the new function in the main loop for each stack 🤖 --- dc-ops | 64 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 24 deletions(-) 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