|
| 1 | +import logging |
| 2 | +import os |
| 3 | + |
| 4 | +from codegen_git.repo_operator.local_repo_operator import LocalRepoOperator |
| 5 | +from graph_sitter.codebase.config import DefaultConfig, ProjectConfig |
| 6 | +from graph_sitter.core.codebase import Codebase |
| 7 | +from graph_sitter.utils import determine_project_language |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | +DEFAULT_CODEGEN_DIR = "/tmp/codegen" |
| 12 | + |
| 13 | + |
| 14 | +def fetch_codebase(repo_name: str, *, tmp_dir: str | None = None, shallow: bool = True, commit_hash: str | None = None) -> Codebase: |
| 15 | + """Fetches a codebase from GitHub and returns a Codebase instance. |
| 16 | +
|
| 17 | + Args: |
| 18 | + repo_name (str): The name of the repository in format "owner/repo" |
| 19 | + tmp_dir (Optional[str]): The directory to clone the repo into. Defaults to /tmp/codegen |
| 20 | + shallow (bool): Whether to do a shallow clone. Defaults to True |
| 21 | + commit_hash (Optional[str]): The specific commit hash to clone. Defaults to HEAD |
| 22 | + Returns: |
| 23 | + Codebase: A Codebase instance initialized with the cloned repository |
| 24 | + Example: |
| 25 | + ```python |
| 26 | + import graph_sitter |
| 27 | + import logging |
| 28 | + # Enable logging to see progress |
| 29 | + logging.basicConfig(level=logging.INFO) |
| 30 | + # Clone a repository to default location (/tmp/codegen) |
| 31 | + codebase = graph_sitter.fetch_codebase('facebook/react') |
| 32 | + # Or specify a custom directory |
| 33 | + codebase = graph_sitter.fetch_codebase('facebook/react', tmp_dir='~/my_repos') |
| 34 | + # Or clone a specific commit |
| 35 | + codebase = graph_sitter.fetch_codebase('facebook/react', commit_hash='abc123') |
| 36 | + ``` |
| 37 | + """ |
| 38 | + logger.info(f"Fetching codebase for {repo_name}") |
| 39 | + |
| 40 | + # Parse repo name |
| 41 | + if "/" not in repo_name: |
| 42 | + raise ValueError("repo_name must be in format 'owner/repo'") |
| 43 | + owner, repo = repo_name.split("/") |
| 44 | + |
| 45 | + # Setup temp directory |
| 46 | + if tmp_dir is None: |
| 47 | + tmp_dir = DEFAULT_CODEGEN_DIR |
| 48 | + os.makedirs(tmp_dir, exist_ok=True) |
| 49 | + logger.info(f"Using directory: {tmp_dir}") |
| 50 | + |
| 51 | + # Setup repo path and URL |
| 52 | + repo_path = os.path.join(tmp_dir, repo) |
| 53 | + repo_url = f"https://github.com/{repo_name}.git" |
| 54 | + logger.info(f"Will clone {repo_url} to {repo_path}") |
| 55 | + |
| 56 | + try: |
| 57 | + # Use LocalRepoOperator to fetch the repository |
| 58 | + logger.info("Cloning repository...") |
| 59 | + repo_operator = LocalRepoOperator.create_from_commit( |
| 60 | + repo_path=repo_path, |
| 61 | + default_branch="main", # We'll get the actual default branch after clone |
| 62 | + commit=commit_hash or "HEAD", |
| 63 | + url=repo_url, |
| 64 | + ) |
| 65 | + logger.info("Clone completed successfully") |
| 66 | + |
| 67 | + # Initialize and return codebase with proper context |
| 68 | + logger.info("Initializing Codebase...") |
| 69 | + project = ProjectConfig(repo_operator=repo_operator, programming_language=determine_project_language(repo_path)) |
| 70 | + codebase = Codebase(projects=[project], config=DefaultConfig) |
| 71 | + logger.info("Codebase initialization complete") |
| 72 | + return codebase |
| 73 | + except Exception as e: |
| 74 | + logger.error(f"Failed to initialize codebase: {e}") |
| 75 | + raise |
0 commit comments