From e49afe4a4f213098166f88855b9d277bb5ad5a77 Mon Sep 17 00:00:00 2001 From: Mikita Pilinka Date: Wed, 24 Jan 2024 11:28:59 +0100 Subject: [PATCH] fix: cfbs should not try to commit without diff Ticket: CFE-3967 Changelog: None Signed-off-by: Mikita Pilinka --- cfbs/git.py | 22 ++++++++++++++++++++++ cfbs/git_magic.py | 10 ++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/cfbs/git.py b/cfbs/git.py index a2a0fe56..de0fd57b 100644 --- a/cfbs/git.py +++ b/cfbs/git.py @@ -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 diff --git a/cfbs/git_magic.py b/cfbs/git_magic.py index 41a22639..e87d9643 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,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