Skip to content

Automatically determine base_path and throw error on non-git repos #121

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
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -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
Expand Down Expand Up @@ -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:
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
Loading