Skip to content

Commit 280d3f0

Browse files
committed
simplify: use direct node.children check instead of has_children() method
Even simpler approach: - Replace node.has_children() with direct if node.children: - Remove unnecessary has_children() methods from all classes - Pythonic and direct - empty lists are falsy, non-empty are truthy - Less code, same functionality This is the most straightforward way to check for children in Python.
1 parent 5efe631 commit 280d3f0

File tree

2 files changed

+169
-175
lines changed

2 files changed

+169
-175
lines changed

src/gitingest/output_formatter.py

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ def format_node(node: FileSystemNode, query: IngestionQuery) -> tuple[str, str,
4141
A tuple containing the summary, directory structure, and file contents.
4242
4343
"""
44-
is_single_file = node.is_single_file()
45-
summary = _create_summary_prefix(query, single_file=is_single_file)
46-
summary += node.get_summary_info()
44+
# Use polymorphic properties - much cleaner!
45+
summary = _create_summary_prefix(query, single_file=node.is_single_file)
46+
47+
# Add type-specific summary info
48+
if isinstance(node, FileSystemDirectory):
49+
summary += f"Files analyzed: {node.file_count}\n"
50+
elif isinstance(node, FileSystemFile):
51+
summary += f"File: {node.name or ''}\nLines: {len(node.content.splitlines()):,}\n"
4752

4853
tree = "Directory structure:\n" + _create_tree_structure(query, node=node)
49-
50-
content = _gather_file_contents(node)
54+
55+
# Use polymorphic content gathering
56+
content = node.gather_contents()
5157

5258
token_estimate = _format_token_count(tree + content)
5359
if token_estimate:
@@ -96,26 +102,6 @@ def _create_summary_prefix(query: IngestionQuery, *, single_file: bool = False)
96102
return "\n".join(parts) + "\n"
97103

98104

99-
def _gather_file_contents(node: FileSystemNode) -> str:
100-
"""Recursively gather contents of all files under the given node.
101-
102-
This function recursively processes a directory node and gathers the contents of all files
103-
under that node. It returns the concatenated content of all files as a single string.
104-
105-
Parameters
106-
----------
107-
node : FileSystemNode
108-
The current directory or file node being processed.
109-
110-
Returns
111-
-------
112-
str
113-
The concatenated content of all files under the given node.
114-
115-
"""
116-
return node.gather_contents()
117-
118-
119105
def _create_tree_structure(
120106
query: IngestionQuery,
121107
*,
@@ -152,11 +138,10 @@ def _create_tree_structure(
152138
tree_str = ""
153139
current_prefix = "└── " if is_last else "├── "
154140

155-
# Get the display name (handles directory slash, symlink target, etc.)
156-
display_name = node.get_display_name()
157-
tree_str += f"{prefix}{current_prefix}{display_name}\n"
141+
# Use polymorphic display name - handles files, dirs, symlinks automatically!
142+
tree_str += f"{prefix}{current_prefix}{node.display_name}\n"
158143

159-
if node.has_children():
144+
if node.children:
160145
prefix += " " if is_last else "│ "
161146
for i, child in enumerate(node.children):
162147
tree_str += _create_tree_structure(query, node=child, prefix=prefix, is_last=i == len(node.children) - 1)

0 commit comments

Comments
 (0)