Skip to content

Commit 7a1def5

Browse files
committed
commit post catastrophe
1 parent 61d2ed8 commit 7a1def5

File tree

5 files changed

+42
-25
lines changed

5 files changed

+42
-25
lines changed

src/gitingest/entrypoint.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from gitingest.clone import clone_repo
1717
from gitingest.config import MAX_FILE_SIZE
1818
from gitingest.ingestion import ingest_query
19+
from gitingest.output_formatter import generate_digest
1920
from gitingest.query_parser import parse_local_dir_path, parse_remote_repo
2021
from gitingest.utils.auth import resolve_token
2122
from gitingest.utils.compat_func import removesuffix
@@ -48,7 +49,7 @@ async def ingest_async(
4849
This function analyzes a source (URL or local path), clones the corresponding repository (if applicable),
4950
and processes its files according to the specified query parameters. It returns a single digest string.
5051
51-
The output is generated lazily using a Context object and its .generate_digest() method.
52+
The output is generated lazily using a Context object and the generate_digest() function.
5253
5354
Parameters
5455
----------
@@ -112,7 +113,7 @@ async def ingest_async(
112113
if not include_gitignored:
113114
_apply_gitignores(query)
114115
context = ingest_query(query)
115-
digest = context.generate_digest()
116+
digest = generate_digest(context)
116117
await _write_output(digest, content=None, target=output)
117118
return digest
118119

@@ -135,7 +136,7 @@ def ingest(
135136
This function analyzes a source (URL or local path), clones the corresponding repository (if applicable),
136137
and processes its files according to the specified query parameters. It returns a single digest string.
137138
138-
The output is generated lazily using a Context object and its .generate_digest() method.
139+
The output is generated lazily using a Context object and the generate_digest() function.
139140
140141
Parameters
141142
----------
@@ -173,7 +174,7 @@ def ingest(
173174
``ingest_async`` : The asynchronous version of this function.
174175
175176
"""
176-
context = asyncio.run(ingest_async(
177+
digest = asyncio.run(ingest_async(
177178
source,
178179
max_file_size=max_file_size,
179180
include_patterns=include_patterns,
@@ -185,7 +186,7 @@ def ingest(
185186
token=token,
186187
output=output,
187188
))
188-
return context.generate_digest()
189+
return digest
189190

190191

191192
def _override_branch_and_tag(query: IngestionQuery, branch: str | None, tag: str | None) -> None:

src/gitingest/ingestion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def ingest_query(query: IngestionQuery) -> Context:
2929
Returns
3030
-------
3131
Context
32-
A Context object representing the ingested file system nodes. Call .generate_digest() to get the summary, directory structure, and file contents.
32+
A Context object representing the ingested file system nodes. Use generate_digest(context) to get the summary, directory structure, and file contents.
3333
3434
Raises
3535
------

src/gitingest/output_formatter.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from gitingest.utils.compat_func import readlink
1111
from functools import singledispatchmethod
1212
from gitingest.schemas import Source, FileSystemFile, FileSystemDirectory, FileSystemSymlink, FileSystemTextFile
13-
from gitingest.schemas.filesystem import SEPARATOR, FileSystemNodeType
13+
from gitingest.schemas.filesystem import SEPARATOR, FileSystemNodeType, CONTEXT_HEADER, CONTEXT_FOOTER
1414
from jinja2 import Environment, BaseLoader
1515

1616

@@ -281,3 +281,27 @@ def _(node: FileSystemFile, query):
281281
"""
282282
file_template = self.env.from_string(template)
283283
return file_template.render(SEPARATOR=SEPARATOR, node=node, query=query, formatter=self)
284+
285+
286+
def generate_digest(context) -> str:
287+
"""Generate a digest from a Context object.
288+
289+
Parameters
290+
----------
291+
context : Context
292+
The context object containing nodes, formatter, and query.
293+
294+
Returns
295+
-------
296+
str
297+
The formatted digest string with header, content, and footer.
298+
"""
299+
if context.query.user_name and context.query.repo_name:
300+
context_header = CONTEXT_HEADER.format(f"/{context.query.user_name}/{context.query.repo_name}")
301+
else:
302+
context_header = CONTEXT_HEADER.format("")
303+
context_footer = CONTEXT_FOOTER
304+
formatted = []
305+
for node in context.nodes:
306+
formatted.append(context.formatter.format(node, context.query))
307+
return context_header + "\n".join(formatted) + context_footer

src/gitingest/schemas/filesystem.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,22 @@ def _(self: 'FileSystemSymlink'):
129129

130130

131131
class Context:
132-
"""Context for holding a list of Source objects and generating a digest on demand using a Formatter.
132+
"""Context for holding a list of Source objects that can be formatted using a Formatter.
133133
134134
Attributes
135135
----------
136136
nodes : list[Source]
137-
The list of source objects to generate a digest for.
137+
The list of source objects to format.
138138
formatter : Formatter
139139
The formatter to use for formatting sources.
140140
query : IngestionQuery
141141
The query context.
142142
"""
143-
nodes: list[Source]
144-
formatter: Formatter
145-
query: IngestionQuery
146-
147-
def generate_digest(self) -> str:
148-
if self.query.user_name and self.query.repo_name:
149-
context_header = CONTEXT_HEADER.format(f"/{self.query.user_name}/{self.query.repo_name}")
150-
else:
151-
context_header = CONTEXT_HEADER.format("")
152-
context_footer = CONTEXT_FOOTER
153-
formatted = []
154-
for node in self.nodes:
155-
formatted.append(self.formatter.format(node, self.query))
156-
return context_header + "\n".join(formatted) + context_footer
143+
144+
def __init__(self, nodes: list[Source], formatter: Formatter, query: IngestionQuery):
145+
self.nodes = nodes
146+
self.formatter = formatter
147+
self.query = query
157148

158149
@property
159150
def summary(self):

src/server/query_processor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from gitingest.clone import clone_repo
99
from gitingest.ingestion import ingest_query
10+
from gitingest.output_formatter import generate_digest
1011
from gitingest.query_parser import parse_remote_repo
1112
from gitingest.utils.git_utils import validate_github_token
1213
from gitingest.utils.pattern_utils import process_patterns
@@ -84,7 +85,7 @@ async def process_query(
8485

8586
try:
8687
context = ingest_query(query)
87-
digest = context.generate_digest()
88+
digest = generate_digest(context)
8889

8990
# Store digest based on S3 configuration
9091
if is_s3_enabled():
@@ -141,7 +142,7 @@ async def process_query(
141142
summary="",
142143
digest_url=digest_url,
143144
tree="",
144-
content=content,
145+
content=digest,
145146
default_max_file_size=slider_position,
146147
pattern_type=pattern_type,
147148
pattern=pattern,

0 commit comments

Comments
 (0)