Skip to content

Commit d517e9e

Browse files
Merge pull request #77 from ModusCreate-Perdigao-GHAS-Playground/feature/fix-origin-ado
Enhance current commit resolution logic in GitUtils with fallback
2 parents 5c11ac7 + 84b3e9b commit d517e9e

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/codeql_wrapper/infrastructure/git_utils.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,34 @@ def get_diff_files(self, git_info: GitInfo) -> List[str]:
135135
base_ref_commit = self.repo.commit(base_ref_to_use)
136136

137137
# Use HEAD for current commit in detached HEAD state
138-
if git_info.current_ref == "HEAD" or git_info.current_ref.startswith(
139-
"refs/pull"
140-
):
138+
# Resolve current commit with fallback logic
139+
try:
140+
if git_info.current_ref == "HEAD" or git_info.current_ref.startswith(
141+
"refs/pull"
142+
):
143+
current_commit = self.repo.head.commit
144+
self.logger.debug("Using HEAD for current commit")
145+
else:
146+
# Try to resolve the current_ref directly first
147+
try:
148+
current_commit = self.repo.commit(git_info.current_ref)
149+
self.logger.debug(
150+
f"Successfully resolved current ref: {git_info.current_ref}"
151+
)
152+
except Exception:
153+
# If that fails, try alternative formats
154+
if git_info.current_ref.startswith("refs/heads/"):
155+
# Try as remote branch
156+
remote_ref = git_info.current_ref.replace(
157+
"refs/heads/", "origin/"
158+
)
159+
self.logger.debug(f"Trying remote ref: {remote_ref}")
160+
current_commit = self.repo.commit(remote_ref)
161+
except Exception as e:
162+
self.logger.warning(
163+
f"Failed to resolve current commit, using HEAD: {e}"
164+
)
141165
current_commit = self.repo.head.commit
142-
else:
143-
current_commit = self.repo.commit(git_info.current_ref)
144166

145167
# Get the diff from base_ref to current
146168
diff = base_ref_commit.diff(current_commit)

0 commit comments

Comments
 (0)