From 70bc968166aabae1ca7e3fd0afdb8622309482dc Mon Sep 17 00:00:00 2001 From: Jasur Sadikov Date: Fri, 6 Sep 2024 19:34:18 +0200 Subject: [PATCH 1/8] Updates checker --- utils.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/utils.py b/utils.py index 4c6dfd7..1219770 100644 --- a/utils.py +++ b/utils.py @@ -109,18 +109,30 @@ settings: Settings -def remove_colors(): - for index in range(len(TEXT)): - TEXT[index] = '' - for index in range(len(BACK)): - BACK[index] = '' - - def set_up(): global GLYPHS GLYPHS = ICON_GLYPHS if settings.mud_settings['nerd_fonts'] else TEXT_GLYPHS +def updates_available(): + target_directory = os.curdir + os.chdir(os.path.dirname(os.path.abspath(__file__))) + + try: + subprocess.run(["git", "rev-parse", "--is-inside-work-tree"], check=True, stdout=subprocess.DEVNULL) + except subprocess.CalledProcessError: + return False, "Not a git repository" + + subprocess.run(["git", "fetch"], check=True) + result = subprocess.run(["git", "status", "-uno"], capture_output=True, text=True) + + if "Your branch is behind" in result.stdout: + print("Updates are available") + subprocess.run(["git", "log", "HEAD..origin/master", "--oneline"], text=True, stdout=subprocess.STDOUT) + + os.chdir(target_directory) + + def print_error(text: str, code: int = 255) -> None: print(f'{TEXT["red"]}Error:{RESET} {text}') sys.exit(code) From bb7c25458238a2dad3a98d696a4c3c140a52eb58 Mon Sep 17 00:00:00 2001 From: Jasur Sadikov Date: Fri, 6 Sep 2024 21:28:57 +0200 Subject: [PATCH 2/8] Updates check is implemented --- main.py | 3 +++ mud.py | 34 +++++++++++--------------------- settings.py | 3 ++- utils.py | 56 +++++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 64 insertions(+), 32 deletions(-) diff --git a/main.py b/main.py index feaa80f..6e3b641 100755 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import sys import utils import settings @@ -8,6 +9,8 @@ try: utils.settings = settings.Settings(utils.SETTINGS_FILE_NAME) utils.set_up() + if utils.settings.config['mud'].getboolean('ask_updates') and utils.check_updates(): + sys.exit() mud = Mud() mud.run() except KeyboardInterrupt: diff --git a/mud.py b/mud.py index 3ed9417..e324275 100644 --- a/mud.py +++ b/mud.py @@ -19,6 +19,7 @@ # Commands COMMANDS = { 'help': ['help', '--help', '-h'], + 'update': ['update'], 'configure': ['configure', 'config'], 'version': ['--version', '-v', 'version'], 'set-global': ['--set-global'], @@ -46,6 +47,7 @@ def _create_parser() -> ArgumentParser: subparsers = parser.add_subparsers(dest='command') subparsers.add_parser(COMMANDS['configure'][0], aliases=COMMANDS['configure'][1:], help='Runs the interactive configuration wizard.') + subparsers.add_parser(COMMANDS['update'][0], aliases=COMMANDS['update'][1:], help='Update mud to the latest version.') subparsers.add_parser(COMMANDS['init'][0], aliases=COMMANDS['init'][1:], help=f'Initializes the {STYLES["bold"]}.mudconfig{RESET} and adds all repositories in this directory to {STYLES["bold"]}.mudconfig{RESET}.') subparsers.add_parser(COMMANDS['info'][0], aliases=COMMANDS['info'][1:], help='Displays branch divergence and working directory changes') subparsers.add_parser(COMMANDS['log'][0], aliases=COMMANDS['log'][1:], help='Displays log of latest commit messages for all repositories in a table view.') @@ -92,6 +94,9 @@ def run(self) -> None: elif sys.argv[1] in COMMANDS['configure']: self.configure() return + elif sys.argv[1] in COMMANDS['update']: + utils.check_updates(True) + return current_directory = os.getcwd() self.config = config.Config() @@ -170,29 +175,12 @@ def init(self, args) -> None: self.config.save(utils.CONFIG_FILE_NAME) def configure(self): - def ask(text: str) -> bool: - print(f"{text} [Y/n]", end='', flush=True) - if sys.platform.startswith('win'): - from msvcrt import getch - response = getch().decode().lower() - else: - import tty, termios - fd = sys.stdin.fileno() - old_settings = termios.tcgetattr(fd) - try: - tty.setraw(fd) - response = sys.stdin.read(1).lower() - finally: - termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) - - print() # Move to new line after key press - return response in ['y', '\r', '\n'] - - utils.settings.config['mud']['run_async'] = str(ask('Do you want to run commands simultaneously for multiple repositories?')) - utils.settings.config['mud']['run_table'] = str(ask('Do you want to see command execution progress in table view? This will limit output content.')) - utils.settings.config['mud']['auto_fetch'] = str(ask(f'Do you want to automatically run {STYLES["bold"]}\'git fetch\'{RESET} whenever you run commands such as {STYLES["bold"]}\'mud info\'{RESET}?')) - utils.settings.config['mud']['nerd_fonts'] = str(ask(f'Do you want to use {STYLES["bold"]}nerd-fonts{RESET}?')) - utils.settings.config['mud']['simplify_branches'] = str(ask(f'Do you want to simplify branches? (ex. {STYLES["bold"]}feature/name{RESET} -> {STYLES["bold"]}f/name{RESET}')) + utils.settings.config['mud']['run_async'] = str(utils.ask('Do you want to run commands simultaneously for multiple repositories?')) + utils.settings.config['mud']['run_table'] = str(utils.ask('Do you want to see command execution progress in table view? This will limit output content.')) + utils.settings.config['mud']['auto_fetch'] = str(utils.ask(f'Do you want to automatically run {STYLES["bold"]}\'git fetch\'{RESET} whenever you run commands such as {STYLES["bold"]}\'mud info\'{RESET}?')) + utils.settings.config['mud']['ask_updates'] = str(utils.ask(f'Do you want to get information about latest updates?')) + utils.settings.config['mud']['nerd_fonts'] = str(utils.ask(f'Do you want to use {STYLES["bold"]}nerd-fonts{RESET}?')) + utils.settings.config['mud']['simplify_branches'] = str(utils.ask(f'Do you want to simplify branches? (ex. {STYLES["bold"]}feature/name{RESET} -> {STYLES["bold"]}f/name{RESET}')) utils.settings.save() print('Your settings are updated!') pass diff --git a/settings.py b/settings.py index 994247c..75d50d4 100644 --- a/settings.py +++ b/settings.py @@ -19,7 +19,8 @@ def __init__(self, file_name: str) -> None: 'auto_fetch': False, 'run_async': True, 'run_table': True, - 'simplify_branches': True + 'simplify_branches': True, + 'ask_updates': True }, 'alias': { 'to': 'git checkout', diff --git a/utils.py b/utils.py index 1219770..f0b2033 100644 --- a/utils.py +++ b/utils.py @@ -114,23 +114,63 @@ def set_up(): GLYPHS = ICON_GLYPHS if settings.mud_settings['nerd_fonts'] else TEXT_GLYPHS -def updates_available(): +def check_updates(explicit: bool = False) -> bool: target_directory = os.curdir os.chdir(os.path.dirname(os.path.abspath(__file__))) - try: - subprocess.run(["git", "rev-parse", "--is-inside-work-tree"], check=True, stdout=subprocess.DEVNULL) - except subprocess.CalledProcessError: - return False, "Not a git repository" - subprocess.run(["git", "fetch"], check=True) + result = subprocess.run(["git", "status", "-uno"], capture_output=True, text=True) if "Your branch is behind" in result.stdout: - print("Updates are available") - subprocess.run(["git", "log", "HEAD..origin/master", "--oneline"], text=True, stdout=subprocess.STDOUT) + m = random.choice(list(TEXT.values())[3:]) + u = random.choice(list(TEXT.values())[3:]) + d = random.choice(list(TEXT.values())[3:]) + print(fr''' + {m} __ __{u} __ __{d} _____ + {m}/\ '-./ \{u}/\ \/\ \{d}/\ __-.{RESET} + {m}\ \ \-./\ \{u} \ \_\ \{d} \ \/\ \{RESET} + {m} \ \_\ \ \_\{u} \_____\{d} \____-{RESET} + {m} \/_/ \/_/{u}\/_____/{d}\/____/{RESET} + ''') + print(f"{STYLES["bold"]}New update(s) is available!{RESET}\n") + + log = subprocess.run(["git", "log", "HEAD..@{u}", "--oneline", "--color=always"], text=True, stdout=subprocess.PIPE).stdout + print(log) + + if ask("Do you want to update?"): + update_process = subprocess.run(["git", "pull", "--force"], text=False, stdout=subprocess.DEVNULL) + if update_process.returncode == 0: + print(f'{TEXT["green"]}{STYLES["BOLD"]}Update successful!{RESET}') + else: + print_error('Update failed', update_process.returncode) + os.chdir(target_directory) + return True + + if explicit: + print("No updates available") os.chdir(target_directory) + return False + + +def ask(text: str) -> bool: + print(f"{text} [Y/n]", end='', flush=True) + if sys.platform.startswith('win'): + from msvcrt import getch + response = getch().decode().lower() + else: + import tty, termios + fd = sys.stdin.fileno() + old_settings = termios.tcgetattr(fd) + try: + tty.setraw(fd) + response = sys.stdin.read(1).lower() + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + + print() + return response in ['y', '\r', '\n'] def print_error(text: str, code: int = 255) -> None: From ef510b159c2402ca39eb0ba4a2a55602395b44df Mon Sep 17 00:00:00 2001 From: Jasur Sadikov Date: Fri, 6 Sep 2024 21:31:49 +0200 Subject: [PATCH 3/8] Updates check is added in pipeline --- .github/workflows/main.yaml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 5d55f40..95445d1 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -87,4 +87,12 @@ jobs: - name: Test branch filtering run: | ./mud.sh -l=label_1 git checkout -b develop - ./mud.sh -b=develop echo "Hello world" \ No newline at end of file + ./mud.sh -b=develop echo "Hello world" + - name: Test update feature + run: | + echo "Update content" > dummy.txt + git add test.txt + git commit -m "Update dummy is created" + git reset --hard HEAD^1 + ./mud update + git pull --force \ No newline at end of file From a974c289dfee8f1dd37ae85c469ef76733b1821b Mon Sep 17 00:00:00 2001 From: Jasur Sadikov Date: Fri, 6 Sep 2024 21:33:54 +0200 Subject: [PATCH 4/8] WIP --- utils.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/utils.py b/utils.py index f0b2033..7fbacbb 100644 --- a/utils.py +++ b/utils.py @@ -118,11 +118,11 @@ def check_updates(explicit: bool = False) -> bool: target_directory = os.curdir os.chdir(os.path.dirname(os.path.abspath(__file__))) - subprocess.run(["git", "fetch"], check=True) + subprocess.run(['git', 'fetch'], check=True) - result = subprocess.run(["git", "status", "-uno"], capture_output=True, text=True) + result = subprocess.run(['git', 'status', '-uno'], capture_output=True, text=True) - if "Your branch is behind" in result.stdout: + if 'Your branch is behind' in result.stdout: m = random.choice(list(TEXT.values())[3:]) u = random.choice(list(TEXT.values())[3:]) d = random.choice(list(TEXT.values())[3:]) @@ -133,13 +133,13 @@ def check_updates(explicit: bool = False) -> bool: {m} \ \_\ \ \_\{u} \_____\{d} \____-{RESET} {m} \/_/ \/_/{u}\/_____/{d}\/____/{RESET} ''') - print(f"{STYLES["bold"]}New update(s) is available!{RESET}\n") + print(f'{STYLES["bold"]}New update(s) is available!{RESET}\n') - log = subprocess.run(["git", "log", "HEAD..@{u}", "--oneline", "--color=always"], text=True, stdout=subprocess.PIPE).stdout + log = subprocess.run(['git', 'log', 'HEAD..@{u}', '--oneline', '--color=always'], text=True, stdout=subprocess.PIPE).stdout print(log) - if ask("Do you want to update?"): - update_process = subprocess.run(["git", "pull", "--force"], text=False, stdout=subprocess.DEVNULL) + if ask('Do you want to update?'): + update_process = subprocess.run(['git', 'pull', '--force'], text=False, stdout=subprocess.DEVNULL) if update_process.returncode == 0: print(f'{TEXT["green"]}{STYLES["BOLD"]}Update successful!{RESET}') else: @@ -148,14 +148,14 @@ def check_updates(explicit: bool = False) -> bool: return True if explicit: - print("No updates available") + print('No updates available') os.chdir(target_directory) return False def ask(text: str) -> bool: - print(f"{text} [Y/n]", end='', flush=True) + print(f"{text} [Y/n] ", end='', flush=True) if sys.platform.startswith('win'): from msvcrt import getch response = getch().decode().lower() From 80c609a4a9e0bb1e70cad62ca3fd35c3ba30b9ab Mon Sep 17 00:00:00 2001 From: Jasur Sadikov Date: Fri, 6 Sep 2024 21:34:34 +0200 Subject: [PATCH 5/8] WIP --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 95445d1..45af5b9 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -91,7 +91,7 @@ jobs: - name: Test update feature run: | echo "Update content" > dummy.txt - git add test.txt + git add dummy.txt git commit -m "Update dummy is created" git reset --hard HEAD^1 ./mud update From 33293650ddb703169f0a86f4e15943910b260efd Mon Sep 17 00:00:00 2001 From: Jasur Sadikov Date: Fri, 6 Sep 2024 21:35:42 +0200 Subject: [PATCH 6/8] WIP --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 45af5b9..7d1ba06 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -94,5 +94,5 @@ jobs: git add dummy.txt git commit -m "Update dummy is created" git reset --hard HEAD^1 - ./mud update + ./mud.sh update git pull --force \ No newline at end of file From 5565060973b9550cfbc8fa5bd34544b29e4fa0c7 Mon Sep 17 00:00:00 2001 From: Jasur Sadikov Date: Fri, 6 Sep 2024 21:38:14 +0200 Subject: [PATCH 7/8] WIP --- .github/workflows/main.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 7d1ba06..1387f8d 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -90,9 +90,6 @@ jobs: ./mud.sh -b=develop echo "Hello world" - name: Test update feature run: | - echo "Update content" > dummy.txt - git add dummy.txt - git commit -m "Update dummy is created" git reset --hard HEAD^1 ./mud.sh update git pull --force \ No newline at end of file From 7a428fd14d8da319cd425954108a5196dfd8d844 Mon Sep 17 00:00:00 2001 From: Jasur Sadikov Date: Fri, 6 Sep 2024 21:39:48 +0200 Subject: [PATCH 8/8] WIP --- .github/workflows/main.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 1387f8d..1490383 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -90,6 +90,7 @@ jobs: ./mud.sh -b=develop echo "Hello world" - name: Test update feature run: | - git reset --hard HEAD^1 ./mud.sh update - git pull --force \ No newline at end of file +# git reset --hard HEAD^1 +# ./mud.sh update +# git pull --force \ No newline at end of file