Skip to content

Commit

Permalink
ask users if they want to pull latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
fe-art committed Sep 11, 2024
1 parent 5bee14c commit ab2649a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
41 changes: 41 additions & 0 deletions clashroyalebuildabot/utils/git_utils.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -39,6 +40,7 @@ def update_terminal_title():


def main():
check_and_pull_updates()
actions = [
ArchersAction,
GoblinBarrelAction,
Expand Down

0 comments on commit ab2649a

Please sign in to comment.