Skip to content

Commit

Permalink
Merge pull request #187 from mineralsfree/CFE-3967
Browse files Browse the repository at this point in the history
CFE-3967: cfbs should not try to commit without diff
  • Loading branch information
olehermanse authored Feb 2, 2024
2 parents a378131 + 57fae2a commit 5230ee3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
20 changes: 20 additions & 0 deletions cfbs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 14 additions & 4 deletions cfbs/git_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 5230ee3

Please sign in to comment.