Skip to content

Commit c0ea689

Browse files
fix: CG-12266 Logging for Empty Repos + Allow Codebase Parse (#904)
- Branch checked out to main on empty repos, however, no commits exist. In this case, parse the repo but log that no commits exist --------- Co-authored-by: Christine Wang <[email protected]>
1 parent 1a2a297 commit c0ea689

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/codegen/git/repo_operator/repo_operator.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,14 @@ def git_cli(self) -> GitCLI:
166166
return git_cli
167167

168168
@property
169-
def head_commit(self) -> GitCommit:
170-
return self.git_cli.head.commit
169+
def head_commit(self) -> GitCommit | None:
170+
try:
171+
return self.git_cli.head.commit
172+
except ValueError as e:
173+
if (f"Reference at {self.git_cli.head.ref.path!r} does not exist") in str(e):
174+
logger.info(f"Ref: {self.git_cli.head.ref.name} has no commits")
175+
return None
176+
raise
171177

172178
@property
173179
def git_diff(self) -> str:

src/codegen/sdk/core/codebase.py

+5
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,10 @@ def checkout(
898898
result = self._op.checkout_commit(commit_hash=commit)
899899
if result == CheckoutResult.SUCCESS:
900900
logger.info(f"Checked out {branch or commit}")
901+
if self._op.head_commit is None:
902+
logger.info(f"Ref: {self._op.git_cli.head.ref.name} has no commits")
903+
return CheckoutResult.SUCCESS
904+
901905
self.sync_to_commit(self._op.head_commit)
902906
elif result == CheckoutResult.NOT_FOUND:
903907
logger.info(f"Could not find branch {branch or commit}")
@@ -1435,6 +1439,7 @@ def from_string(
14351439

14361440
with tempfile.TemporaryDirectory(prefix="codegen_") as tmp_dir:
14371441
logger.info(f"Using directory: {tmp_dir}")
1442+
14381443
codebase = CodebaseFactory.get_codebase_from_files(repo_path=tmp_dir, files=files, programming_language=prog_lang)
14391444
logger.info("Codebase initialization complete")
14401445
return codebase

0 commit comments

Comments
 (0)