Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom commit messages #337

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading