Skip to content

Commit bfc77ee

Browse files
Edward-Codegencodegen-bot
and
codegen-bot
committed
Automatically determine base_path and throw error on non-git repos (#121)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed - [ ] I have read and agree to the [Contributor License Agreement](../CLA.md) Co-authored-by: codegen-bot <[email protected]>
1 parent c04b0f4 commit bfc77ee

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
@@ -74,7 +74,7 @@
7474
from codegen.sdk.typescript.statements.import_statement import TSImportStatement
7575
from codegen.sdk.typescript.symbol import TSSymbol
7676
from codegen.sdk.typescript.type_alias import TSTypeAlias
77-
from codegen.sdk.utils import determine_project_language
77+
from codegen.sdk.utils import determine_project_language, split_git_path
7878
from codegen.shared.decorators.docs import apidoc, noapidoc, py_noapidoc
7979
from codegen.shared.exceptions.control_flow import MaxAIRequestsError
8080
from codegen.shared.performance.stopwatch_utils import stopwatch
@@ -148,11 +148,16 @@ def __init__(
148148

149149
# Initialize project with repo_path if projects is None
150150
if repo_path is not None:
151+
# Split repo_path into (git_root, base_path)
151152
repo_path = os.path.abspath(repo_path)
153+
git_root, base_path = split_git_path(repo_path)
154+
# Create repo_config
152155
repo_config = BaseRepoConfig()
156+
# Create main project
153157
main_project = ProjectConfig(
154-
repo_operator=LocalRepoOperator(repo_config=repo_config, repo_path=repo_path),
158+
repo_operator=LocalRepoOperator(repo_config=repo_config, repo_path=git_root),
155159
programming_language=determine_project_language(repo_path),
160+
base_path=base_path,
156161
)
157162
projects = [main_project]
158163
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)