Skip to content

Commit

Permalink
Add more sorting to tree ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
cyclotruc committed Jan 2, 2025
1 parent 3ecf535 commit 2125765
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/gitingest/ingest_from_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ def _read_file_content(file_path: str) -> str:

def _sort_children(children: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""
Sort children nodes with files first, then directories, both in alphanumerical order.
Sort children nodes with:
1. README.md first
2. Regular files (not starting with dot)
3. Hidden files (starting with dot)
4. Regular directories (not starting with dot)
5. Hidden directories (starting with dot)
All groups are sorted alphanumerically within themselves.
Parameters
----------
Expand All @@ -161,11 +167,30 @@ def _sort_children(children: list[dict[str, Any]]) -> list[dict[str, Any]]:
Returns
-------
list[dict[str, Any]]
Sorted list with files first, followed by directories.
Sorted list according to the specified order.
"""
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
# Separate files and directories
files = [child for child in children if child["type"] == "file"]
directories = [child for child in children if child["type"] == "directory"]

# Find README.md
readme_files = [f for f in files if f["name"].lower() == "readme.md"]
other_files = [f for f in files if f["name"].lower() != "readme.md"]

# Separate hidden and regular files/directories
regular_files = [f for f in other_files if not f["name"].startswith(".")]
hidden_files = [f for f in other_files if f["name"].startswith(".")]
regular_dirs = [d for d in directories if not d["name"].startswith(".")]
hidden_dirs = [d for d in directories if d["name"].startswith(".")]

# Sort each group alphanumerically
regular_files.sort(key=lambda x: x["name"])
hidden_files.sort(key=lambda x: x["name"])
regular_dirs.sort(key=lambda x: x["name"])
hidden_dirs.sort(key=lambda x: x["name"])

# Combine all groups in the desired order
return readme_files + regular_files + hidden_files + regular_dirs + hidden_dirs


def _scan_directory(
Expand Down Expand Up @@ -412,7 +437,7 @@ def _create_file_content_string(files: list[dict[str, Any]]) -> str:
Create a formatted string of file contents with separators.
This function takes a list of files and generates a formatted string where each file’s
content is separated by a divider. If a README.md file is found, it is placed at the top.
content is separated by a divider.
Parameters
----------
Expand All @@ -427,21 +452,9 @@ def _create_file_content_string(files: list[dict[str, Any]]) -> str:
output = ""
separator = "=" * 48 + "\n"

# First add README.md if it exists
for file in files:
if not file["content"]:
continue

if file["path"].lower() == "/readme.md":
output += separator
output += f"File: {file['path']}\n"
output += separator
output += f"{file['content']}\n\n"
break

# Then add all other files in their original order
for file in files:
if not file["content"] or file["path"].lower() == "/readme.md":
if not file["content"]:
continue

output += separator
Expand Down

0 comments on commit 2125765

Please sign in to comment.