diff --git a/clashroyalebuildabot/utils/git_utils.py b/clashroyalebuildabot/utils/git_utils.py new file mode 100644 index 0000000..b5e037f --- /dev/null +++ b/clashroyalebuildabot/utils/git_utils.py @@ -0,0 +1,41 @@ +import subprocess +import sys + +from loguru import logger + + +def _is_branch_late() -> bool: + # Fetch the latest changes from the remote repository + subprocess.run( + ["git", "fetch"], check=True, capture_output=True, text=True + ) + + # Check if the local branch is behind the remote branch + # Get the local status to check if it's up to date + status = subprocess.run( + ["git", "status", "-uno"], + capture_output=True, + text=True, + check=True, + ).stdout + return "Your branch is up to date" not in status + + +def _check_and_pull_updates() -> None: + if not _is_branch_late(): + return + + should_update = input( + "New updates available. Do you want to update the bot? [y]/n: " + ) + if should_update.lower() not in ["y", "yes", ""]: + return + subprocess.run(["git", "pull"], check=True, capture_output=True, text=True) + + +def check_and_pull_updates() -> None: + try: + _check_and_pull_updates() + except subprocess.CalledProcessError as e: + logger.error(f"Error while checking / pulling updates: {e.stderr}") + sys.exit(1) diff --git a/main.py b/main.py index 125b286..8d38fe4 100644 --- a/main.py +++ b/main.py @@ -15,6 +15,7 @@ from clashroyalebuildabot.actions import MusketeerAction from clashroyalebuildabot.actions import WitchAction from clashroyalebuildabot.bot import Bot +from clashroyalebuildabot.utils.git_utils import check_and_pull_updates start_time = datetime.now() @@ -39,6 +40,7 @@ def update_terminal_title(): def main(): + check_and_pull_updates() actions = [ ArchersAction, GoblinBarrelAction,