Skip to content

refactor: introduce Source base class #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "debugpy",
"request": "launch",
"module": "server",
"args": [],
"args": ["--reload"],
"cwd": "${workspaceFolder}/src"
}
]
Expand Down
19 changes: 8 additions & 11 deletions src/gitingest/ingestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

from gitingest.config import MAX_DIRECTORY_DEPTH, MAX_FILES, MAX_TOTAL_SIZE_BYTES
from gitingest.output_formatter import format_node
from gitingest.schemas import FileSystemNode, FileSystemNodeType, FileSystemStats
from gitingest.schemas import FileSystemDirectory, FileSystemFile, FileSystemNode, FileSystemStats, FileSystemSymlink
from gitingest.utils.compat_func import readlink
from gitingest.utils.ingestion_utils import _should_exclude, _should_include
from gitingest.utils.logging_config import get_logger

Expand Down Expand Up @@ -70,9 +71,8 @@ def ingest_query(query: IngestionQuery) -> tuple[str, str, str]:

relative_path = path.relative_to(query.local_path)

file_node = FileSystemNode(
file_node = FileSystemFile(
name=path.name,
type=FileSystemNodeType.FILE,
size=path.stat().st_size,
file_count=1,
path_str=str(relative_path),
Expand All @@ -95,9 +95,8 @@ def ingest_query(query: IngestionQuery) -> tuple[str, str, str]:

logger.info("Processing directory", extra={"directory_path": str(path)})

root_node = FileSystemNode(
root_node = FileSystemDirectory(
name=path.name,
type=FileSystemNodeType.DIRECTORY,
path_str=str(path.relative_to(query.local_path)),
path=path,
)
Expand Down Expand Up @@ -161,9 +160,8 @@ def _process_node(node: FileSystemNode, query: IngestionQuery, stats: FileSystem
continue
_process_file(path=sub_path, parent_node=node, stats=stats, local_path=query.local_path)
elif sub_path.is_dir():
child_directory_node = FileSystemNode(
child_directory_node = FileSystemDirectory(
name=sub_path.name,
type=FileSystemNodeType.DIRECTORY,
path_str=str(sub_path.relative_to(query.local_path)),
path=sub_path,
depth=node.depth + 1,
Expand Down Expand Up @@ -201,11 +199,11 @@ def _process_symlink(path: Path, parent_node: FileSystemNode, stats: FileSystemS
The base path of the repository or directory being processed.

"""
child = FileSystemNode(
child = FileSystemSymlink(
name=path.name,
type=FileSystemNodeType.SYMLINK,
path_str=str(path.relative_to(local_path)),
path=path,
target=str(readlink(path)),
depth=parent_node.depth + 1,
)
stats.total_files += 1
Expand Down Expand Up @@ -258,9 +256,8 @@ def _process_file(path: Path, parent_node: FileSystemNode, stats: FileSystemStat
stats.total_files += 1
stats.total_size += file_size

child = FileSystemNode(
child = FileSystemFile(
name=path.name,
type=FileSystemNodeType.FILE,
size=file_size,
file_count=1,
path_str=str(path.relative_to(local_path)),
Expand Down
58 changes: 14 additions & 44 deletions src/gitingest/output_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import requests.exceptions
import tiktoken

from gitingest.schemas import FileSystemNode, FileSystemNodeType
from gitingest.utils.compat_func import readlink
from gitingest.schemas import FileSystemDirectory, FileSystemFile, FileSystemNode, FileSystemSymlink
from gitingest.utils.logging_config import get_logger

if TYPE_CHECKING:
Expand Down Expand Up @@ -42,18 +41,19 @@ def format_node(node: FileSystemNode, query: IngestionQuery) -> tuple[str, str,
A tuple containing the summary, directory structure, and file contents.

"""
is_single_file = node.type == FileSystemNodeType.FILE
summary = _create_summary_prefix(query, single_file=is_single_file)

if node.type == FileSystemNodeType.DIRECTORY:
# Use polymorphic properties - much cleaner!
summary = _create_summary_prefix(query, single_file=node.is_single_file)

# Add type-specific summary info
if isinstance(node, FileSystemDirectory):
summary += f"Files analyzed: {node.file_count}\n"
elif node.type == FileSystemNodeType.FILE:
summary += f"File: {node.name}\n"
summary += f"Lines: {len(node.content.splitlines()):,}\n"
elif isinstance(node, FileSystemFile):
summary += f"File: {node.name or ''}\nLines: {len(node.content.splitlines()):,}\n"

tree = "Directory structure:\n" + _create_tree_structure(query, node=node)

content = _gather_file_contents(node)

# Use polymorphic content gathering
content = node.gather_contents()

token_estimate = _format_token_count(tree + content)
if token_estimate:
Expand Down Expand Up @@ -102,30 +102,6 @@ def _create_summary_prefix(query: IngestionQuery, *, single_file: bool = False)
return "\n".join(parts) + "\n"


def _gather_file_contents(node: FileSystemNode) -> str:
"""Recursively gather contents of all files under the given node.

This function recursively processes a directory node and gathers the contents of all files
under that node. It returns the concatenated content of all files as a single string.

Parameters
----------
node : FileSystemNode
The current directory or file node being processed.

Returns
-------
str
The concatenated content of all files under the given node.

"""
if node.type != FileSystemNodeType.DIRECTORY:
return node.content_string

# Recursively gather contents of all files under the current directory
return "\n".join(_gather_file_contents(child) for child in node.children)


def _create_tree_structure(
query: IngestionQuery,
*,
Expand Down Expand Up @@ -162,16 +138,10 @@ def _create_tree_structure(
tree_str = ""
current_prefix = "└── " if is_last else "├── "

# Indicate directories with a trailing slash
display_name = node.name
if node.type == FileSystemNodeType.DIRECTORY:
display_name += "/"
elif node.type == FileSystemNodeType.SYMLINK:
display_name += " -> " + readlink(node.path).name

tree_str += f"{prefix}{current_prefix}{display_name}\n"
# Use polymorphic display name - handles files, dirs, symlinks automatically!
tree_str += f"{prefix}{current_prefix}{node.display_name}\n"

if node.type == FileSystemNodeType.DIRECTORY and node.children:
if node.children:
prefix += " " if is_last else "│ "
for i, child in enumerate(node.children):
tree_str += _create_tree_structure(query, node=child, prefix=prefix, is_last=i == len(node.children) - 1)
Expand Down
22 changes: 20 additions & 2 deletions src/gitingest/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
"""Module containing the schemas for the Gitingest package."""

from gitingest.schemas.cloning import CloneConfig
from gitingest.schemas.filesystem import FileSystemNode, FileSystemNodeType, FileSystemStats
from gitingest.schemas.filesystem import (
FileSystemDirectory,
FileSystemFile,
FileSystemNode,
FileSystemStats,
FileSystemSymlink,
GitRepository,
)
from gitingest.schemas.ingestion import IngestionQuery
from gitingest.schemas.source import Source

__all__ = ["CloneConfig", "FileSystemNode", "FileSystemNodeType", "FileSystemStats", "IngestionQuery"]
__all__ = [
"CloneConfig",
"FileSystemDirectory",
"FileSystemFile",
"FileSystemNode",
"FileSystemStats",
"FileSystemSymlink",
"GitRepository",
"IngestionQuery",
"Source",
]
Loading
Loading