Skip to content

Commit

Permalink
Add support for commit and branches
Browse files Browse the repository at this point in the history
  • Loading branch information
cyclotruc committed Dec 10, 2024
1 parent b422608 commit c2432cf
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 30 deletions.
28 changes: 12 additions & 16 deletions src/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,19 @@ def create_summary_string(query: dict, nodes: Dict, files: List[Dict], ) -> str:
"""Creates a summary string with file counts and content size."""
total_lines = sum(len(file["content"].splitlines()) for file in files)

if query['branch']:
if len(query['branch']) > 20:
branch = query['branch'][:20] + '...'
else:
branch = query['branch']
else:
branch = "main"
summary = f"Repository: {query['user_name']}/{query['repo_name']}\n"
summary += f"Files analyzed: {nodes['file_count']}\n"
summary += f"Directories analyzed: {nodes['dir_count']}\n"
summary += f"Total lines of content: {total_lines:,}\n"
if query['subpath'] != '/':
summary += f"Subpath: {query['subpath']}\n"
if query['commit']:
summary += f"Commit: {query['commit']}\n"
elif query['branch'] != 'main' and query['branch'] != 'master':
summary += f"Branch: {query['branch']}\n"
return summary



return (
f"Repository: {query['user_name']}/{query['repo_name']}\n"
f"Files analyzed: {nodes['file_count']}\n"
f"Directories analyzed: {nodes['dir_count']}\n"
f"Total lines of content: {total_lines:,}\n"
f"Subpath: {query['subpath']}\n"
# f"branch: {branch}\n"
)

def create_tree_structure(query: dict, node: Dict, prefix: str = "", is_last: bool = True) -> str:
"""Creates a tree-like string representation of the file structure."""
Expand Down
63 changes: 49 additions & 14 deletions src/utils/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,57 @@
import os

from .decorators import async_timeout
from config import TMP_BASE_PATH, CLONE_TIMEOUT
from config import CLONE_TIMEOUT


@async_timeout(CLONE_TIMEOUT)
async def clone_repo(query: dict) -> str:
#Clean up any existing repo

proc = await asyncio.create_subprocess_exec(
"git",
"clone",
"--depth=1",
"--single-branch",
query['url'],
query['local_path'],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)

stdout, stderr = await proc.communicate()
if query['commit']:
proc = await asyncio.create_subprocess_exec(
"git",
"clone",
"--single-branch",
query['url'],
query['local_path'],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await proc.communicate()

proc = await asyncio.create_subprocess_exec(
"git",
"-C",
query['local_path'],
"checkout",
query['branch'],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await proc.communicate()
elif query['branch'] != 'main' and query['branch'] != 'master':
proc = await asyncio.create_subprocess_exec(
"git",
"clone",
"--depth=1",
"--single-branch",
"--branch",
query['branch'],
query['url'],
query['local_path'],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
else:
proc = await asyncio.create_subprocess_exec(
"git",
"clone",
"--depth=1",
"--single-branch",
query['url'],
query['local_path'],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)

await proc.communicate()
4 changes: 4 additions & 0 deletions src/utils/parse_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def parse_url(url: str) -> dict:
"repo_name": None,
"type": None,
"branch": None,
"commit": None,
"subpath": "/",
"local_path": None,
"url": None,
Expand Down Expand Up @@ -40,5 +41,8 @@ def parse_url(url: str) -> dict:
if len(path_parts) > 3:
parsed["type"] = path_parts[2]
parsed["branch"] = path_parts[3]
if len(parsed['branch']) == 40 and all(c in '0123456789abcdefABCDEF' for c in parsed['branch']):
parsed["commit"] = parsed['branch']

parsed["subpath"] = "/" + "/".join(path_parts[4:])
return parsed

0 comments on commit c2432cf

Please sign in to comment.