Skip to content

Commit 4767697

Browse files
committed
add ingest_async and made ingest function sync by default
1 parent 1da1c39 commit 4767697

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,19 @@ This will write the digest in a text file (default `digest.txt`) in your current
6464
## 🐛 Python package usage
6565

6666
```python
67+
# Synchronous usage
6768
from gitingest import ingest
6869

6970
summary, tree, content = ingest("path/to/directory")
7071

7172
# or from URL
7273
summary, tree, content = ingest("https://github.com/cyclotruc/gitingest")
74+
75+
# Asynchronous usage
76+
from gitingest import ingest_async
77+
import asyncio
78+
79+
result = asyncio.run(ingest_async("path/to/directory"))
7380
```
7481

7582
By default, this won't write a file but can be enabled with the `output` argument.

src/gitingest/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
from gitingest.query_ingestion import run_ingest_query
44
from gitingest.query_parser import parse_query
55
from gitingest.repository_clone import clone_repo
6-
from gitingest.repository_ingest import ingest
6+
from gitingest.repository_ingest import ingest, ingest_async
77

8-
__all__ = ["run_ingest_query", "clone_repo", "parse_query", "ingest"]
8+
__all__ = ["run_ingest_query", "clone_repo", "parse_query", "ingest", "ingest_async"]

src/gitingest/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import click
88

99
from gitingest.config import MAX_FILE_SIZE, OUTPUT_FILE_PATH
10-
from gitingest.repository_ingest import ingest
10+
from gitingest.repository_ingest import ingest_async
1111

1212

1313
@click.command()
@@ -92,7 +92,7 @@ async def _async_main(
9292

9393
if not output:
9494
output = OUTPUT_FILE_PATH
95-
summary, _, _ = await ingest(source, max_size, include_patterns, exclude_patterns, branch, output=output)
95+
summary, _, _ = await ingest_async(source, max_size, include_patterns, exclude_patterns, branch, output=output)
9696

9797
click.echo(f"Analysis complete! Output written to: {output}")
9898
click.echo("\nSummary:")

src/gitingest/repository_ingest.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from gitingest.repository_clone import CloneConfig, clone_repo
1111

1212

13-
async def ingest(
13+
async def ingest_async(
1414
source: str,
1515
max_file_size: int = 10 * 1024 * 1024, # 10 MB
1616
include_patterns: set[str] | str | None = None,
@@ -96,3 +96,58 @@ async def ingest(
9696
if parsed_query.url:
9797
# Clean up the temporary directory
9898
shutil.rmtree(TMP_BASE_PATH, ignore_errors=True)
99+
100+
101+
def ingest(
102+
source: str,
103+
max_file_size: int = 10 * 1024 * 1024, # 10 MB
104+
include_patterns: set[str] | str | None = None,
105+
exclude_patterns: set[str] | str | None = None,
106+
branch: str | None = None,
107+
output: str | None = None,
108+
) -> tuple[str, str, str]:
109+
"""
110+
Synchronous version of ingest_async.
111+
112+
This function analyzes a source (URL or local path), clones the corresponding repository (if applicable),
113+
and processes its files according to the specified query parameters. It returns a summary, a tree-like
114+
structure of the files, and the content of the files. The results can optionally be written to an output file.
115+
116+
Parameters
117+
----------
118+
source : str
119+
The source to analyze, which can be a URL (for a Git repository) or a local directory path.
120+
max_file_size : int
121+
Maximum allowed file size for file ingestion. Files larger than this size are ignored, by default
122+
10*1024*1024 (10 MB).
123+
include_patterns : set[str] | str | None, optional
124+
Pattern or set of patterns specifying which files to include. If `None`, all files are included.
125+
exclude_patterns : set[str] | str | None, optional
126+
Pattern or set of patterns specifying which files to exclude. If `None`, no files are excluded.
127+
branch : str | None, optional
128+
The branch to clone and ingest. If `None`, the default branch is used.
129+
output : str | None, optional
130+
File path where the summary and content should be written. If `None`, the results are not written to a file.
131+
132+
Returns
133+
-------
134+
tuple[str, str, str]
135+
A tuple containing:
136+
- A summary string of the analyzed repository or directory.
137+
- A tree-like string representation of the file structure.
138+
- The content of the files in the repository or directory.
139+
140+
See Also
141+
--------
142+
ingest_async : The asynchronous version of this function.
143+
"""
144+
return asyncio.run(
145+
ingest_async(
146+
source=source,
147+
max_file_size=max_file_size,
148+
include_patterns=include_patterns,
149+
exclude_patterns=exclude_patterns,
150+
branch=branch,
151+
output=output,
152+
)
153+
)

0 commit comments

Comments
 (0)