Skip to content

Commit c6c64b9

Browse files
author
codegen-bot
committed
Automatically determine base_path and throw error on non-git repos
1 parent 6a28cfa commit c6c64b9

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/codegen/sdk/core/codebase.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
from codegen.sdk.typescript.interface import TSInterface
7171
from codegen.sdk.typescript.symbol import TSSymbol
7272
from codegen.sdk.typescript.type_alias import TSTypeAlias
73-
from codegen.sdk.utils import determine_project_language
73+
from codegen.sdk.utils import determine_project_language, split_git_path
7474
from codegen.shared.decorators.docs import apidoc, noapidoc
7575
from codegen.shared.exceptions.control_flow import MaxAIRequestsError
7676
from codegen.shared.performance.stopwatch_utils import stopwatch
@@ -139,11 +139,16 @@ def __init__(
139139

140140
# Initialize project with repo_path if projects is None
141141
if repo_path is not None:
142+
# Split repo_path into (git_root, base_path)
142143
repo_path = os.path.abspath(repo_path)
144+
git_root, base_path = split_git_path(repo_path)
145+
# Create repo_config
143146
repo_config = BaseRepoConfig()
147+
# Create main project
144148
main_project = ProjectConfig(
145-
repo_operator=LocalRepoOperator(repo_config=repo_config, repo_path=repo_path),
149+
repo_operator=LocalRepoOperator(repo_config=repo_config, repo_path=git_root),
146150
programming_language=determine_project_language(repo_path),
151+
base_path=base_path,
147152
)
148153
projects = [main_project]
149154
else:

src/codegen/sdk/utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,40 @@ def determine_project_language(folder_path: str):
290290
return language_counts.most_common(1)[0][0]
291291

292292

293+
def split_git_path(filepath: str) -> tuple[str, str | None]:
294+
"""Split a filepath into (git_root, base_path) tuple by finding .git directory.
295+
296+
Args:
297+
filepath (str): The full path to split
298+
299+
Returns:
300+
tuple: (git_root_path, relative_path)
301+
302+
Raises:
303+
ValueError: If the path is not in a git repository
304+
"""
305+
# Convert to absolute path and resolve any symlinks
306+
path = Path(filepath).resolve()
307+
308+
# Start from the given path and traverse up until we find .git
309+
current = path
310+
while current != current.parent:
311+
if (current / ".git").exists():
312+
# Found the git root
313+
git_root = str(current)
314+
rel_path = str(path.relative_to(current))
315+
316+
# Handle the case where filepath is the git root itself
317+
if rel_path == ".":
318+
rel_path = None
319+
320+
return (git_root, rel_path)
321+
current = current.parent
322+
323+
# If we get here, we didn't find a .git directory
324+
raise ValueError(f"Path '{filepath}' is not in a git repository!")
325+
326+
293327
def truncate_line(input: str, max_chars: int) -> str:
294328
input = str(input)
295329
if len(input) > max_chars:

0 commit comments

Comments
 (0)