Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/codegen/sdk/core/codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
from codegen.sdk.typescript.interface import TSInterface
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
from codegen.shared.exceptions.control_flow import MaxAIRequestsError
from codegen.shared.performance.stopwatch_utils import stopwatch
Expand Down Expand Up @@ -139,11 +139,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:
Expand Down
34 changes: 34 additions & 0 deletions src/codegen/sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down