mirror of
https://github.com/kirill-markin/repo-to-text.git
synced 2025-12-06 03:22:23 -08:00
empty dirs remove
This commit is contained in:
parent
d720a094dd
commit
2dd3413f0d
1 changed files with 23 additions and 12 deletions
|
|
@ -4,12 +4,13 @@ import pathspec
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
# Configure logging
|
# Configure logging
|
||||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
|
|
||||||
def get_tree_structure(path='.', gitignore_spec=None) -> str:
|
def get_tree_structure(path='.', gitignore_spec=None) -> str:
|
||||||
logging.debug(f'Generating tree structure for path: {path}')
|
logging.debug(f'Generating tree structure for path: {path}')
|
||||||
result = subprocess.run(['tree', '-a', '-f', '--noreport', path], stdout=subprocess.PIPE)
|
result = subprocess.run(['tree', '-a', '-f', '--noreport', path], stdout=subprocess.PIPE)
|
||||||
tree_output = result.stdout.decode('utf-8')
|
tree_output = result.stdout.decode('utf-8')
|
||||||
|
logging.debug(f'Tree output generated: {tree_output}')
|
||||||
|
|
||||||
if not gitignore_spec:
|
if not gitignore_spec:
|
||||||
logging.debug('No .gitignore specification found')
|
logging.debug('No .gitignore specification found')
|
||||||
|
|
@ -44,30 +45,40 @@ def is_ignored_path(file_path: str) -> bool:
|
||||||
logging.debug(f'Path ignored: {file_path}')
|
logging.debug(f'Path ignored: {file_path}')
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def remove_empty_dirs(tree_output: str) -> str:
|
def remove_empty_dirs(tree_output: str, path='.') -> str:
|
||||||
logging.debug('Removing empty directories from tree output')
|
logging.debug('Removing empty directories from tree output')
|
||||||
lines = tree_output.splitlines()
|
lines = tree_output.splitlines()
|
||||||
non_empty_dirs = set()
|
non_empty_dirs = set()
|
||||||
filtered_lines = []
|
filtered_lines = []
|
||||||
|
|
||||||
for line in reversed(lines):
|
for line in lines:
|
||||||
logging.debug(f'Processing line: {line}')
|
parts = line.strip().split()
|
||||||
if line.strip().endswith('/'):
|
if parts:
|
||||||
logging.debug('Line is a directory')
|
full_path = parts[-1]
|
||||||
if any(line.strip() in dir_line for dir_line in non_empty_dirs):
|
if os.path.isdir(full_path) and not any(os.path.isfile(os.path.join(full_path, f)) for f in os.listdir(full_path)):
|
||||||
filtered_lines.append(line)
|
logging.debug(f'Directory is empty and will be removed: {full_path}')
|
||||||
else:
|
continue
|
||||||
non_empty_dirs.add(line)
|
non_empty_dirs.add(os.path.dirname(full_path))
|
||||||
filtered_lines.append(line)
|
filtered_lines.append(line)
|
||||||
|
|
||||||
|
final_lines = []
|
||||||
|
for line in filtered_lines:
|
||||||
|
parts = line.strip().split()
|
||||||
|
if parts:
|
||||||
|
full_path = parts[-1]
|
||||||
|
if os.path.isdir(full_path) and full_path not in non_empty_dirs:
|
||||||
|
logging.debug(f'Directory is empty and will be removed: {full_path}')
|
||||||
|
continue
|
||||||
|
final_lines.append(line)
|
||||||
|
|
||||||
logging.debug('Empty directory removal complete')
|
logging.debug('Empty directory removal complete')
|
||||||
return '\n'.join(reversed(filtered_lines))
|
return '\n'.join(final_lines)
|
||||||
|
|
||||||
def save_repo_to_text(path='.') -> None:
|
def save_repo_to_text(path='.') -> None:
|
||||||
logging.debug(f'Starting to save repo structure to text for path: {path}')
|
logging.debug(f'Starting to save repo structure to text for path: {path}')
|
||||||
gitignore_spec = load_gitignore(path)
|
gitignore_spec = load_gitignore(path)
|
||||||
tree_structure = get_tree_structure(path, gitignore_spec)
|
tree_structure = get_tree_structure(path, gitignore_spec)
|
||||||
tree_structure = remove_empty_dirs(tree_structure)
|
tree_structure = remove_empty_dirs(tree_structure, path)
|
||||||
|
|
||||||
output_file = 'repo_structure.txt'
|
output_file = 'repo_structure.txt'
|
||||||
with open(output_file, 'w') as file:
|
with open(output_file, 'w') as file:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue