From 854083082365005b7aec6cd511e4e4542ad4fcfb Mon Sep 17 00:00:00 2001 From: Simon Halvorsen Date: Mon, 15 Jun 2026 14:18:02 +0200 Subject: [PATCH] CFE-4688: Cull empty directories after cfbs convert Ticket: CFE-4688 Changelog: Title Signed-off-by: Simon Halvorsen --- cfbs/commands.py | 3 +++ cfbs/utils.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/cfbs/commands.py b/cfbs/commands.py index cfac1409..237f9f3e 100644 --- a/cfbs/commands.py +++ b/cfbs/commands.py @@ -73,6 +73,7 @@ def search_command(terms: List[str]): most_relevant_version, read_json, CFBSExitError, + remove_empty_folders, save_file, strip_right, pad_right, @@ -1526,6 +1527,8 @@ def cfbs_convert_git_commit( first_patch_conversion = False + print("\n") + remove_empty_folders(os.getcwd()) print("Conversion finished successfully.") return 0 diff --git a/cfbs/utils.py b/cfbs/utils.py index 4838a314..29806886 100644 --- a/cfbs/utils.py +++ b/cfbs/utils.py @@ -11,6 +11,7 @@ import urllib.request # needed on some platforms import urllib.error from collections import OrderedDict +from pathlib import Path from shutil import rmtree from typing import Iterable, List, Optional, Tuple, Union import filecmp @@ -694,3 +695,29 @@ def migrate_config_paths(): shutil.rmtree(old_dir) print("REMOVED: %s, use %s" % (old_dir, new_dir)) + + +def remove_empty_folders(target_path): + root_path = Path(target_path).expanduser() + assert root_path.exists() + + all_empty_dirs = set( + p + for p in root_path.glob("**/") + if p.is_dir() + and not any(part.startswith(".") for part in p.relative_to(root_path).parts) + and not any(files for _, _, files in os.walk(p)) + ) + + for p in all_empty_dirs: + if p.parent not in all_empty_dirs: + print( + "Deleted folder hierarchy which is now empty at '%s/'" + % p.relative_to(Path.cwd()) + ) + + for path in sorted(all_empty_dirs, key=lambda p: len(p.parts), reverse=True): + try: + path.rmdir() + except OSError: + pass