Skip to content

Commit

Permalink
Allow custom commit messages
Browse files Browse the repository at this point in the history
Fix #335
  • Loading branch information
alichtman committed Nov 16, 2023
1 parent 3c194ff commit 2579cb4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
34 changes: 18 additions & 16 deletions shallow_backup/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,48 +241,50 @@ 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(" ")
if action == "back up":
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)
Expand Down
42 changes: 30 additions & 12 deletions shallow_backup/git_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

###########
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit 2579cb4

Please sign in to comment.