-
Notifications
You must be signed in to change notification settings - Fork 52
feat: adds fetch_codebase #24
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
Merged
jayhack
merged 8 commits into
develop
from
jayhack-cg-10349-support-codegenfetch_codebaseposthogposthog
Jan 23, 2025
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eb4073e
no tests
jayhack f760516
added commit_hash
jayhack 28c400e
.
jayhack 39eb040
.
jayhack a277d22
Merge branch 'develop' into jayhack-cg-10349-support-codegenfetch_cod…
jayhack 500e63b
.
jayhack f1589ca
.
jayhack c47598e
Delete jayhack-cg-10349-support-codegenfetch_codebaseposthogposthog
jayhack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import logging | ||
import os | ||
from codegen_git.repo_operator.local_repo_operator import LocalRepoOperator | ||
from graph_sitter.core.codebase import Codebase | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
DEFAULT_CODEGEN_DIR = "/tmp/codegen" | ||
|
||
|
||
def fetch_codebase(repo_name: str, *, tmp_dir: str | None = None, shallow: bool = True, commit_hash: str | None = None) -> Codebase: | ||
"""Fetches a codebase from GitHub and returns a Codebase instance. | ||
Args: | ||
repo_name (str): The name of the repository in format "owner/repo" | ||
tmp_dir (Optional[str]): The directory to clone the repo into. Defaults to /tmp/codegen | ||
shallow (bool): Whether to do a shallow clone. Defaults to True | ||
commit_hash (Optional[str]): The specific commit hash to clone. Defaults to HEAD | ||
Returns: | ||
Codebase: A Codebase instance initialized with the cloned repository | ||
Example: | ||
```python | ||
import graph_sitter | ||
import logging | ||
# Enable logging to see progress | ||
logging.basicConfig(level=logging.INFO) | ||
# Clone a repository to default location (/tmp/codegen) | ||
codebase = graph_sitter.fetch_codebase('facebook/react') | ||
# Or specify a custom directory | ||
codebase = graph_sitter.fetch_codebase('facebook/react', tmp_dir='~/my_repos') | ||
# Or clone a specific commit | ||
codebase = graph_sitter.fetch_codebase('facebook/react', commit_hash='abc123') | ||
``` | ||
""" | ||
logger.info(f"Fetching codebase for {repo_name}") | ||
|
||
# Parse repo name | ||
if "/" not in repo_name: | ||
raise ValueError("repo_name must be in format 'owner/repo'") | ||
owner, repo = repo_name.split("/") | ||
|
||
# Setup temp directory | ||
if tmp_dir is None: | ||
tmp_dir = DEFAULT_CODEGEN_DIR | ||
os.makedirs(tmp_dir, exist_ok=True) | ||
logger.info(f"Using directory: {tmp_dir}") | ||
|
||
# Setup repo path and URL | ||
repo_path = os.path.join(tmp_dir, repo) | ||
repo_url = f"https://github.com/{repo_name}.git" | ||
logger.info(f"Will clone {repo_url} to {repo_path}") | ||
|
||
try: | ||
# Use LocalRepoOperator to fetch the repository | ||
logger.info("Cloning repository...") | ||
LocalRepoOperator.create_from_commit( | ||
repo_path=repo_path, | ||
default_branch="main", # We'll get the actual default branch after clone | ||
commit=commit_hash or "HEAD", | ||
url=repo_url | ||
) | ||
logger.info("Clone completed successfully") | ||
|
||
# Initialize and return codebase | ||
logger.info("Initializing Codebase...") | ||
codebase = Codebase(repo_path) | ||
logger.info("Codebase initialization complete") | ||
return codebase | ||
except Exception as e: | ||
logger.error(f"Failed to initialize codebase: {e}") | ||
raise |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.