Skip to content

Commit

Permalink
Improve tree order
Browse files Browse the repository at this point in the history
  • Loading branch information
cyclotruc committed Jan 2, 2025
1 parent 36b04a5 commit 3ecf535
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/gitingest/ingest_from_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ def _read_file_content(file_path: str) -> str:
return f"Error reading file: {str(e)}"


def _sort_children(children: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""
Sort children nodes with files first, then directories, both in alphanumerical order.
Parameters
----------
children : list[dict[str, Any]]
List of file and directory nodes to sort.
Returns
-------
list[dict[str, Any]]
Sorted list with files first, followed by directories.
"""
files = sorted([child for child in children if child["type"] == "file"], key=lambda x: x["name"])
directories = sorted([child for child in children if child["type"] == "directory"], key=lambda x: x["name"])
return files + directories


def _scan_directory(
path: str,
query: dict[str, Any],
Expand Down Expand Up @@ -329,6 +348,9 @@ def _scan_directory(
result["file_count"] += subdir["file_count"]
result["dir_count"] += 1 + subdir["dir_count"]

result["children"] = _sort_children(result["children"])
return result

except PermissionError:
print(f"Permission denied: {path}")

Expand Down

0 comments on commit 3ecf535

Please sign in to comment.