Skip to content

Commit 2125765

Browse files
committed
Add more sorting to tree ordering
1 parent 3ecf535 commit 2125765

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

src/gitingest/ingest_from_query.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,13 @@ def _read_file_content(file_path: str) -> str:
151151

152152
def _sort_children(children: list[dict[str, Any]]) -> list[dict[str, Any]]:
153153
"""
154-
Sort children nodes with files first, then directories, both in alphanumerical order.
154+
Sort children nodes with:
155+
1. README.md first
156+
2. Regular files (not starting with dot)
157+
3. Hidden files (starting with dot)
158+
4. Regular directories (not starting with dot)
159+
5. Hidden directories (starting with dot)
160+
All groups are sorted alphanumerically within themselves.
155161
156162
Parameters
157163
----------
@@ -161,11 +167,30 @@ def _sort_children(children: list[dict[str, Any]]) -> list[dict[str, Any]]:
161167
Returns
162168
-------
163169
list[dict[str, Any]]
164-
Sorted list with files first, followed by directories.
170+
Sorted list according to the specified order.
165171
"""
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
172+
# Separate files and directories
173+
files = [child for child in children if child["type"] == "file"]
174+
directories = [child for child in children if child["type"] == "directory"]
175+
176+
# Find README.md
177+
readme_files = [f for f in files if f["name"].lower() == "readme.md"]
178+
other_files = [f for f in files if f["name"].lower() != "readme.md"]
179+
180+
# Separate hidden and regular files/directories
181+
regular_files = [f for f in other_files if not f["name"].startswith(".")]
182+
hidden_files = [f for f in other_files if f["name"].startswith(".")]
183+
regular_dirs = [d for d in directories if not d["name"].startswith(".")]
184+
hidden_dirs = [d for d in directories if d["name"].startswith(".")]
185+
186+
# Sort each group alphanumerically
187+
regular_files.sort(key=lambda x: x["name"])
188+
hidden_files.sort(key=lambda x: x["name"])
189+
regular_dirs.sort(key=lambda x: x["name"])
190+
hidden_dirs.sort(key=lambda x: x["name"])
191+
192+
# Combine all groups in the desired order
193+
return readme_files + regular_files + hidden_files + regular_dirs + hidden_dirs
169194

170195

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

430-
# First add README.md if it exists
431-
for file in files:
432-
if not file["content"]:
433-
continue
434-
435-
if file["path"].lower() == "/readme.md":
436-
output += separator
437-
output += f"File: {file['path']}\n"
438-
output += separator
439-
output += f"{file['content']}\n\n"
440-
break
441-
442455
# Then add all other files in their original order
443456
for file in files:
444-
if not file["content"] or file["path"].lower() == "/readme.md":
457+
if not file["content"]:
445458
continue
446459

447460
output += separator

0 commit comments

Comments
 (0)