From 2b6cd72cedda48186a482fcfcfaf074108212e9c Mon Sep 17 00:00:00 2001 From: Robin Schneider Date: Sun, 16 Feb 2025 13:40:53 +0100 Subject: [PATCH] Support git submodules in dirty but without used files detection --- README.md | 2 ++ components/git_ref/text_sensor.py | 36 +++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 44212b4..1851be8 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ All of them are **optional**. - **unversioned** (String, default=`-UNVERSIONED`): Suffix to append when the device config file is not tracked by Git. ### Dirty parsing + When using the `dirty` option, _only_ **tracked** files that are relevant for the configuration are considered! This includes the following: @@ -55,3 +56,4 @@ This includes the following: If there are uncommitted changes to any of these files, the provided `dirty` suffix will be added to the `git describe` output. +When git submodules are included, the detection which files are actually used is currently not implemented and dirty is reporeted if any file or even a file in a git submodules has uncommitted changes. diff --git a/components/git_ref/text_sensor.py b/components/git_ref/text_sensor.py index 7429245..b8d6c3d 100644 --- a/components/git_ref/text_sensor.py +++ b/components/git_ref/text_sensor.py @@ -66,6 +66,15 @@ def is_config_versioned(): return git_ls_files == config_path_trimmed +def is_superproject_using_git_submodules(): + GIT_SUBMODULES = git.run_git_command( + ["git", "submodule", "status", "--cached"], + ) + _LOGGER.debug("git submodules: %s", GIT_SUBMODULES) + + return GIT_SUBMODULES != "" + + def get_git_diff(GIT_DIR, cached=False): DIFF_COMMAND = ["git", "diff", "--name-only"] if cached: @@ -249,17 +258,22 @@ def produce_git_describe(config): if "dirty" in config: # COMMAND.append(f"--dirty={config['dirty']}") if is_config_versioned(): - diff_paths = get_git_diff_file_paths(config) - config_paths = get_config_file_paths(config) - _LOGGER.info( - "Checking git Diffs: \n%s\n\nAgainst files in config: \n%s\n", - diff_paths, - config_paths, - ) - for file in config_paths: - if file in diff_paths: - dirty_postfix = config["dirty"] - _LOGGER.warning("Config dirty: '%s' has changes", file) + if is_superproject_using_git_submodules(): + # Git submodules are involved. Using simplified dirty status + # that does not check what files are actually used. + COMMAND.append(f"--dirty={config['dirty']}") + else: + diff_paths = get_git_diff_file_paths(config) + config_paths = get_config_file_paths(config) + _LOGGER.info( + "Checking git Diffs: \n%s\n\nAgainst files in config: \n%s\n", + diff_paths, + config_paths, + ) + for file in config_paths: + if file in diff_paths: + dirty_postfix = config["dirty"] + _LOGGER.warning("Config dirty: '%s' has changes", file) else: _LOGGER.warning("Config File '%s' is unversioned!", CORE.config_path) dirty_postfix = config["unversioned"]