diff --git a/src/codegen/sdk/core/codebase.py b/src/codegen/sdk/core/codebase.py index 54675dbf1..1e38a64a1 100644 --- a/src/codegen/sdk/core/codebase.py +++ b/src/codegen/sdk/core/codebase.py @@ -74,7 +74,7 @@ from codegen.sdk.typescript.statements.import_statement import TSImportStatement from codegen.sdk.typescript.symbol import TSSymbol from codegen.sdk.typescript.type_alias import TSTypeAlias -from codegen.sdk.utils import determine_project_language +from codegen.sdk.utils import determine_project_language, split_git_path from codegen.shared.decorators.docs import apidoc, noapidoc, py_noapidoc from codegen.shared.exceptions.control_flow import MaxAIRequestsError from codegen.shared.performance.stopwatch_utils import stopwatch @@ -148,11 +148,16 @@ def __init__( # Initialize project with repo_path if projects is None if repo_path is not None: + # Split repo_path into (git_root, base_path) repo_path = os.path.abspath(repo_path) + git_root, base_path = split_git_path(repo_path) + # Create repo_config repo_config = BaseRepoConfig() + # Create main project main_project = ProjectConfig( - repo_operator=LocalRepoOperator(repo_config=repo_config, repo_path=repo_path), + repo_operator=LocalRepoOperator(repo_config=repo_config, repo_path=git_root), programming_language=determine_project_language(repo_path), + base_path=base_path, ) projects = [main_project] else: diff --git a/src/codegen/sdk/utils.py b/src/codegen/sdk/utils.py index 32d7538df..80985e20b 100644 --- a/src/codegen/sdk/utils.py +++ b/src/codegen/sdk/utils.py @@ -290,6 +290,40 @@ def determine_project_language(folder_path: str): return language_counts.most_common(1)[0][0] +def split_git_path(filepath: str) -> tuple[str, str | None]: + """Split a filepath into (git_root, base_path) tuple by finding .git directory. + + Args: + filepath (str): The full path to split + + Returns: + tuple: (git_root_path, relative_path) + + Raises: + ValueError: If the path is not in a git repository + """ + # Convert to absolute path and resolve any symlinks + path = Path(filepath).resolve() + + # Start from the given path and traverse up until we find .git + current = path + while current != current.parent: + if (current / ".git").exists(): + # Found the git root + git_root = str(current) + rel_path = str(path.relative_to(current)) + + # Handle the case where filepath is the git root itself + if rel_path == ".": + rel_path = None + + return (git_root, rel_path) + current = current.parent + + # If we get here, we didn't find a .git directory + raise ValueError(f"Path '{filepath}' is not in a git repository!") + + def truncate_line(input: str, max_chars: int) -> str: input = str(input) if len(input) > max_chars: