Skip to content

Commit

Permalink
Always use git commit --verbose, and don't interactively prompt for a…
Browse files Browse the repository at this point in the history
… commit message in shallow-backup

Instead, shell out to whatever `$EDITOR` is. Fix #340
  • Loading branch information
alichtman committed Jun 30, 2024
1 parent 199e840 commit daaec28
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 114 deletions.
84 changes: 39 additions & 45 deletions shallow_backup/__main__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
import os
import sys
from colorama import Fore

import click
from colorama import Fore

from .backup import (
backup_all,
backup_dotfiles,
backup_configs,
backup_packages,
backup_dotfiles,
backup_fonts,
backup_packages,
)
from .config import (
add_dot_path_to_config,
check_insecure_config_permissions,
delete_config_file,
get_config,
safe_create_config,
write_config,
)
from .git_wrapper import (
create_gitignore,
git_add_all_commit_push,
git_set_remote,
handle_separate_git_dir_in_dotfiles,
safe_git_init,
)
from .printing import print_path_blue, print_red_bold, print_version_info
from .prompts import (
splash_screen,
path_update_prompt,
main_menu_prompt,
add_to_config_prompt,
remove_from_config_prompt,
git_url_prompt,
edit_config,
git_url_prompt,
main_menu_prompt,
path_update_prompt,
prompt_yes_no,
remove_from_config_prompt,
splash_screen,
)
from .reinstall import (
reinstall_all_sb,
Expand All @@ -26,30 +44,13 @@
reinstall_fonts_sb,
reinstall_packages_sb,
)
from .git_wrapper import (
safe_git_init,
git_add_all_commit_push,
handle_separate_git_dir_in_dotfiles,
git_set_remote,
create_gitignore,
DEFAULT_COMMIT_MSG,
)
from .upgrade import check_if_config_upgrade_needed
from .utils import (
mkdir_warn_overwrite,
check_if_path_is_valid_dir,
destroy_backup_dir,
expand_to_abs_path,
check_if_path_is_valid_dir,
)
from .config import (
safe_create_config,
get_config,
write_config,
add_dot_path_to_config,
delete_config_file,
check_insecure_config_permissions,
mkdir_warn_overwrite,
)
from .upgrade import check_if_config_upgrade_needed
from .printing import print_version_info, print_path_blue, print_red_bold


# custom help options
Expand Down Expand Up @@ -282,28 +283,21 @@ def cli(
dry_run=dry_run,
skip=True,
)
git_add_all_commit_push(
repo, DEFAULT_COMMIT_MSG["full_backup"], dry_run=dry_run
)
git_add_all_commit_push(repo, 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)
msg = DEFAULT_COMMIT_MSG["dotfiles"]
git_add_all_commit_push(repo, msg, dry_run=dry_run)
git_add_all_commit_push(repo, dry_run=dry_run)
elif backup_configs_flag:
backup_configs(configs_path, dry_run=dry_run, skip=True)
git_add_all_commit_push(
repo, DEFAULT_COMMIT_MSG["configs"], dry_run=dry_run
)
git_add_all_commit_push(repo, dry_run=dry_run)
elif backup_packages_flag:
backup_packages(packages_path, dry_run=dry_run, skip=True)
git_add_all_commit_push(
repo, DEFAULT_COMMIT_MSG["packages"], dry_run=dry_run
)
git_add_all_commit_push(repo, dry_run=dry_run)
elif backup_fonts_flag:
backup_fonts(fonts_path, dry_run=dry_run, skip=True)
git_add_all_commit_push(repo, DEFAULT_COMMIT_MSG["fonts"], dry_run=dry_run)
git_add_all_commit_push(repo, dry_run=dry_run)
# No command line options, show action menu and process selected option.
else:
selection = main_menu_prompt()
Expand All @@ -312,20 +306,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, DEFAULT_COMMIT_MSG[target])
git_add_all_commit_push(repo)
elif target == "dotfiles":
backup_dotfiles(dotfiles_path)
handle_separate_git_dir_in_dotfiles(dotfiles_path, dry_run)
git_add_all_commit_push(repo, DEFAULT_COMMIT_MSG[target])
git_add_all_commit_push(repo)
elif target == "configs":
backup_configs(configs_path)
git_add_all_commit_push(repo, DEFAULT_COMMIT_MSG[target])
git_add_all_commit_push(repo)
elif target == "packages":
backup_packages(packages_path)
git_add_all_commit_push(repo, DEFAULT_COMMIT_MSG[target])
git_add_all_commit_push(repo)
elif target == "fonts":
backup_fonts(fonts_path)
git_add_all_commit_push(repo, DEFAULT_COMMIT_MSG[target])
git_add_all_commit_push(repo)
elif action == "reinstall":
if target == "packages":
reinstall_packages_sb(packages_path)
Expand Down
98 changes: 29 additions & 69 deletions shallow_backup/git_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
import os
from colorama import Fore, Style
from pathlib import Path
import readline # Imported to support arrow key navigation during input
import subprocess
import sys
from difflib import unified_diff
from pathlib import Path
from shutil import move, which

import git
import readline # Imported to support arrow key navigation during input
from colorama import Fore
from git import GitCommandError
from shutil import move, which

from .config import get_config
from .printing import (
print_blue_bold,
print_error_report_github_issue_and_exit,
print_green_bold,
print_path_red,
print_path_yellow,
print_red_bold,
print_yellow,
print_yellow_bold,
print_path_red,
print_red_bold,
print_blue_bold,
prompt_yes_no,
print_error_report_github_issue_and_exit,
)
from .config import get_config
from .utils import safe_mkdir

#########
# GLOBALS
#########

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",
}

###########
# FUNCTIONS
###########
Expand Down Expand Up @@ -133,7 +122,6 @@ 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=DEFAULT_COMMIT_MSG["dotfiles"],
dry_run=dry_run,
)
print_green_bold("Switching back to parent shallow-backup repo...")
Expand All @@ -143,17 +131,11 @@ def handle_separate_git_dir_in_dotfiles(dotfiles_path: Path, dry_run: bool = Fal
print_yellow_bold("No nested dotfiles repo detected.")


def prompt_to_show_git_diff(repo):
if prompt_yes_no("Show git diff?", Fore.BLUE):
print(repo.git.diff(staged=True, color="always"))


def git_add_all_and_print_status(repo: git.Repo):
print_yellow("Staging all files for commit...")
repo.git.add(all=True)
print_yellow_bold(f"Git status of {repo.working_dir}")
print(repo.git.status())
prompt_to_show_git_diff(repo)


def install_trufflehog_git_hook(repo: git.Repo):
Expand Down Expand Up @@ -223,7 +205,7 @@ def update_precommit_file():
subprocess.call("pre-commit install", cwd=repo.working_dir, shell=True)


def git_add_all_commit_push(repo: git.Repo, message: str, dry_run: bool = False):
def git_add_all_commit_push(repo: git.Repo, dry_run: bool = False):
"""
Stages all changed files in dir_path and its children folders for commit,
commits them and pushes to a remote if it's configured.
Expand All @@ -243,30 +225,26 @@ 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:
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,
):
sys.exit()
except git.exc.GitCommandError as e:
print_red_bold(f"Error while making a commit: {e.command}\n{e}\n")
process = subprocess.run(["git", "commit", "--verbose"], cwd=repo.working_dir)
if process.returncode != 0:
print_red_bold(
"Please open a new issue at https://github.com/alichtman/shallow-backup/issues/new"
"Failed to make a commit. The two most likely reasons for this are:\n\t1. No commit message was provided.\n\t2. trufflehog detected secrets in the commit.\nPlease resolve ths issue and try again."
)
return

print_yellow_bold("Successful commit.")
sys.exit(1)
else:
print_yellow_bold("Successful commit.")

if "origin" in [remote.name for remote in repo.remotes]:
print_path_yellow(
"Pushing to remote:",
f"{repo.remotes.origin.url}[origin/{repo.active_branch.name}]...",
)
repo.git.fetch()
repo.git.push("--set-upstream", "origin", "HEAD")
if prompt_yes_no(
"Push commit to remote? Did you check for secrets carefully? trufflehog is not perfect...",
Fore.YELLOW,
):
if "origin" in [remote.name for remote in repo.remotes]:
print_path_yellow(
"Pushing to remote:",
f"{repo.remotes.origin.url}[origin/{repo.active_branch.name}]...",
)
repo.git.fetch()
repo.git.push("--set-upstream", "origin", "HEAD")
else:
print_yellow_bold("No changes to commit...")

Expand Down Expand Up @@ -299,21 +277,3 @@ 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 daaec28

Please sign in to comment.