Skip to content

Commit

Permalink
fix: cfbs should not try to commit without diff
Browse files Browse the repository at this point in the history
Ticket: CFE-3967
Changelog: None
Signed-off-by: Mikita Pilinka <[email protected]>
  • Loading branch information
mineralsfree committed Jan 25, 2024
1 parent 1cad09d commit e49afe4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
22 changes: 22 additions & 0 deletions cfbs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,25 @@ 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"], check=True, capture_output=True)
if scope == "all":
if len(result.stdout) > 0:
should_commit = True
else:
changes = list(
map(
lambda m: m.strip().split(" ")[1],
list(filter(None, result.stdout.split("\n"))),
)
)
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:
return False
10 changes: 8 additions & 2 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,7 +91,7 @@ def decorated_fn(*args, **kwargs):
msg = commit_msg % tuple(positional_args)
else:
msg = commit_msg

should_commit &= git_check_tracked_changes(files_to_commit)
if not should_commit:
return ret

Expand Down

0 comments on commit e49afe4

Please sign in to comment.