Skip to content

Commit fe05a85

Browse files
committed
Improve tree order
1 parent 7b1b97a commit fe05a85

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/gitingest/ingest_from_query.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ def _read_file_content(file_path: str) -> str:
149149
return f"Error reading file: {str(e)}"
150150

151151

152+
def _sort_children(children: list[dict[str, Any]]) -> list[dict[str, Any]]:
153+
"""
154+
Sort children nodes with files first, then directories, both in alphanumerical order.
155+
156+
Parameters
157+
----------
158+
children : list[dict[str, Any]]
159+
List of file and directory nodes to sort.
160+
161+
Returns
162+
-------
163+
list[dict[str, Any]]
164+
Sorted list with files first, followed by directories.
165+
"""
166+
files = sorted([child for child in children if child["type"] == "file"], key=lambda x: x["name"])
167+
directories = sorted([child for child in children if child["type"] == "directory"], key=lambda x: x["name"])
168+
return files + directories
169+
170+
152171
def _scan_directory(
153172
path: str,
154173
query: dict[str, Any],
@@ -329,6 +348,9 @@ def _scan_directory(
329348
result["file_count"] += subdir["file_count"]
330349
result["dir_count"] += 1 + subdir["dir_count"]
331350

351+
result["children"] = _sort_children(result["children"])
352+
return result
353+
332354
except PermissionError:
333355
print(f"Permission denied: {path}")
334356

0 commit comments

Comments
 (0)