From 2579cb4defe8e7da21c2ab7bec3007fd916697ab Mon Sep 17 00:00:00 2001 From: Aaron Lichtman Date: Wed, 15 Nov 2023 06:10:51 -0800 Subject: [PATCH] Allow custom commit messages Fix #335 --- shallow_backup/__main__.py | 34 +++++++++++++++------------- shallow_backup/git_wrapper.py | 42 +++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/shallow_backup/__main__.py b/shallow_backup/__main__.py index 7fd7015d..ba7e1c0b 100644 --- a/shallow_backup/__main__.py +++ b/shallow_backup/__main__.py @@ -241,27 +241,29 @@ def cli( dry_run=dry_run, skip=True, ) - if not dry_run: - git_add_all_commit_push(repo, "full_backup") + git_add_all_commit_push( + repo, DEFAULT_COMMIT_MSG["full_backup"], dry_run=dry_run + ) elif backup_dots_flag: backup_dotfiles(dotfiles_path, dry_run=dry_run, skip=True) # The reason that dotfiles/.git is special cased, and none of the others are is because maintaining a separate git repo for dotfiles is a common use case. handle_separate_git_dir_in_dotfiles(dotfiles_path, dry_run) - if not dry_run: - git_add_all_commit_push(repo, "dotfiles") + msg = DEFAULT_COMMIT_MSG["dotfiles"] + git_add_all_commit_push(repo, msg, dry_run=dry_run) elif backup_configs_flag: backup_configs(configs_path, dry_run=dry_run, skip=True) - if not dry_run: - git_add_all_commit_push(repo, "configs") + git_add_all_commit_push( + repo, DEFAULT_COMMIT_MSG["configs"], dry_run=dry_run + ) elif backup_packages_flag: backup_packages(packages_path, dry_run=dry_run, skip=True) - if not dry_run: - git_add_all_commit_push(repo, "packages") + git_add_all_commit_push( + repo, DEFAULT_COMMIT_MSG["packages"], dry_run=dry_run + ) elif backup_fonts_flag: backup_fonts(fonts_path, dry_run=dry_run, skip=True) - if not dry_run: - git_add_all_commit_push(repo, "fonts") - # No CL options, show action menu and process selected option. + git_add_all_commit_push(repo, DEFAULT_COMMIT_MSG["fonts"], dry_run=dry_run) + # No command line options, show action menu and process selected option. else: selection = main_menu_prompt() action, _, target = selection.rpartition(" ") @@ -269,20 +271,20 @@ def cli( if target == "all": backup_all(dotfiles_path, packages_path, fonts_path, configs_path) handle_separate_git_dir_in_dotfiles(dotfiles_path, dry_run=dry_run) - git_add_all_commit_push(repo, target) + git_add_all_commit_push(repo, DEFAULT_COMMIT_MSG[target]) elif target == "dotfiles": backup_dotfiles(dotfiles_path) handle_separate_git_dir_in_dotfiles(dotfiles_path, dry_run) - git_add_all_commit_push(repo, target) + git_add_all_commit_push(repo, DEFAULT_COMMIT_MSG[target]) elif target == "configs": backup_configs(configs_path) - git_add_all_commit_push(repo, target) + git_add_all_commit_push(repo, DEFAULT_COMMIT_MSG[target]) elif target == "packages": backup_packages(packages_path) - git_add_all_commit_push(repo, target) + git_add_all_commit_push(repo, DEFAULT_COMMIT_MSG[target]) elif target == "fonts": backup_fonts(fonts_path) - git_add_all_commit_push(repo, target) + git_add_all_commit_push(repo, DEFAULT_COMMIT_MSG[target]) elif action == "reinstall": if target == "packages": reinstall_packages_sb(packages_path) diff --git a/shallow_backup/git_wrapper.py b/shallow_backup/git_wrapper.py index f4d735d2..18149a00 100644 --- a/shallow_backup/git_wrapper.py +++ b/shallow_backup/git_wrapper.py @@ -13,13 +13,13 @@ # GLOBALS ######### -COMMIT_MSG = { - "all": "Back up everything.", - "configs": "Back up configs.", - "dotfiles": "Back up dotfiles.", - "fonts": "Back up fonts.", - "full_backup": "Full back up.", - "packages": "Back up packages.", +DEFAULT_COMMIT_MSG = { + "all": "[shallow-backup] Back up everything", + "configs": "[shallow-backup] Back up configs", + "dotfiles": "[shallow-backup] Back up dotfiles", + "fonts": "[shallow-backup] Back up fonts", + "full_backup": "[shallow-backup] Full back up", + "packages": "[shallow-backup] Back up packages", } ########### @@ -102,7 +102,9 @@ def handle_separate_git_dir_in_dotfiles(dotfiles_path: Path, dry_run: bool = Fal ): print_green_bold("Okay, switching into dotfiles subrepo...") git_add_all_commit_push( - dotfiles_repo, message="dotfiles", dry_run=dry_run + dotfiles_repo, + message=DEFAULT_COMMIT_MSG["dotfiles"], + dry_run=dry_run, ) print_green_bold("Switching back to parent shallow-backup repo...") else: @@ -179,11 +181,9 @@ def git_add_all_commit_push(repo: git.Repo, message: str, dry_run: bool = False) print_yellow_bold("Dry run: Would have made a commit!") return print_yellow_bold("Making new commit...") + message = prompt_for_custom_git_commit_message(message) try: - stdout = subprocess.run( - ["git", "commit", "-m", f"{COMMIT_MSG[message]}"], cwd=repo.working_dir - ).stdout - print(stdout) + subprocess.run(["git", "commit", "-m", message], cwd=repo.working_dir) if prompt_yes_no( "Does the trufflehog output contain any secrets? If so, please exit and remove them.", Fore.YELLOW, @@ -237,3 +237,21 @@ def move_git_repo(source_path, dest_path): print_blue_bold("Moving git repo to new location.") except FileNotFoundError: pass + + +def prompt_for_custom_git_commit_message(default_message: str) -> str: + """ + Ask user if they'd like to set a custom git commit message. + If yes, return the message. If no, return the default message. + """ + if prompt_yes_no( + f"Custom commit message? If not, `{default_message}` will be used", + Fore.GREEN, + invert=True, + ): + custom_message = input( + Fore.GREEN + Style.BRIGHT + "Custom message: " + Fore.RESET + ) + if custom_message: + return custom_message + return default_message