diff --git a/cfbs/git.py b/cfbs/git.py index a2a0fe5..025830e 100644 --- a/cfbs/git.py +++ b/cfbs/git.py @@ -177,3 +177,23 @@ def git_discard_changes_in_file(file_name): raise CFBSGitError( "Failed to discard changes in file '%s'" % file_name ) from cpe + + +def git_check_tracked_changes(scope=["all"]): + should_commit = False + try: + result = run(["git", "status", "-s", "-u"], check=True, stdout=PIPE) + if "all" in scope: + if len(result.stdout) > 0: + should_commit = True + else: + lines = result.stdout.decode("utf-8").split("\n") + changes = [line.strip().split(" ")[1] for line in lines if line] + should_commit = any(i in changes for i in scope) + if not should_commit: + print("No changes to commit") + return should_commit + except CalledProcessError as cpe: + raise CFBSGitError( + "Failed to run 'git status -s -u' to check for changes." + ) from cpe diff --git a/cfbs/git_magic.py b/cfbs/git_magic.py index 41a2263..212f801 100644 --- a/cfbs/git_magic.py +++ b/cfbs/git_magic.py @@ -6,7 +6,13 @@ from cfbs.prompts import YES_NO_CHOICES, prompt_user from cfbs.cfbs_config import CFBSConfig, CFBSReturnWithoutCommit -from cfbs.git import git_commit, git_discard_changes_in_file, CFBSGitError, is_git_repo +from cfbs.git import ( + git_commit, + git_discard_changes_in_file, + CFBSGitError, + is_git_repo, + git_check_tracked_changes, +) from cfbs.args import get_args from cfbs.result import Result import logging as log @@ -85,12 +91,16 @@ def decorated_fn(*args, **kwargs): msg = commit_msg % tuple(positional_args) else: msg = commit_msg - + config = CFBSConfig.get_instance() + do_git = get_args().git + should_commit = ( + should_commit + and config.get("git", False) + and git_check_tracked_changes(files) + ) if not should_commit: return ret - config = CFBSConfig.get_instance() - do_git = get_args().git if do_git == "yes": if not is_git_repo(): log.error(