Skip to content

Commit a424e11

Browse files
authored
Merge branch 'main' into slack_webhook_debugging
2 parents 46c1e59 + 461f495 commit a424e11

File tree

1 file changed

+78
-2
lines changed

1 file changed

+78
-2
lines changed

socketsecurity/core/git_interface.py

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,21 @@ def __init__(self, path: str):
218218
log.debug(f"Failed to get changed files via git diff (Bitbucket): {error}")
219219
# Fallback to git show for single commit
220220
if not detected:
221-
self.show_files = self.repo.git.show(self.commit, name_only=True, format="%n").splitlines()
222-
log.debug(f"Changed files detected via git show: {self.show_files}")
221+
# Check if this is a merge commit first
222+
if self._is_merge_commit():
223+
# For merge commits, use git diff with parent
224+
if self._detect_merge_commit_changes():
225+
detected = True
226+
else:
227+
# Fallback to git show if merge detection fails
228+
self.show_files = self.repo.git.show(self.commit, name_only=True, format="%n").splitlines()
229+
log.debug(f"Changed files detected via git show (merge commit fallback): {self.show_files}")
230+
detected = True
231+
else:
232+
# Regular single commit
233+
self.show_files = self.repo.git.show(self.commit, name_only=True, format="%n").splitlines()
234+
log.debug(f"Changed files detected via git show: {self.show_files}")
235+
detected = True
223236
self.changed_files = []
224237
for item in self.show_files:
225238
if item != "":
@@ -380,6 +393,69 @@ def get_formatted_committer(self) -> str:
380393
log.debug("Using fallback committer: unknown")
381394
return "unknown"
382395

396+
def _is_merge_commit(self) -> bool:
397+
"""
398+
Check if the current commit is a merge commit.
399+
400+
Returns:
401+
True if this is a merge commit (has multiple parents), False otherwise
402+
"""
403+
try:
404+
# A merge commit has multiple parents
405+
is_merge = len(self.commit.parents) > 1
406+
log.debug(f"Commit {self.commit.hexsha[:8]} has {len(self.commit.parents)} parents, is_merge: {is_merge}")
407+
return is_merge
408+
except Exception as error:
409+
log.debug(f"Error checking if commit is merge commit: {error}")
410+
return False
411+
412+
def _detect_merge_commit_changes(self) -> bool:
413+
"""
414+
Detect changed files in a merge commit using git diff with parent.
415+
416+
This method handles the case where git show --name-only doesn't work
417+
for merge commits (expected Git behavior).
418+
419+
Returns:
420+
True if detection was successful, False otherwise
421+
"""
422+
try:
423+
if not self._is_merge_commit():
424+
log.debug("Not a merge commit, skipping merge commit detection")
425+
return False
426+
427+
# For merge commits, we need to diff against a parent
428+
# We'll use the first parent (typically the target branch)
429+
if not self.commit.parents:
430+
log.debug("Merge commit has no parents - cannot perform merge-aware diff")
431+
return False
432+
433+
parent_commit = self.commit.parents[0]
434+
435+
# Verify parent commit is accessible
436+
try:
437+
parent_sha = parent_commit.hexsha
438+
# Quick validation that parent exists
439+
self.repo.commit(parent_sha)
440+
except Exception as parent_error:
441+
log.error(f"Cannot resolve parent commit {parent_sha}: {parent_error}")
442+
return False
443+
444+
# Use git diff to show changes from parent to merge commit
445+
diff_range = f'{parent_sha}..{self.commit.hexsha}'
446+
log.debug(f"Attempting merge commit diff: git diff --name-only {diff_range}")
447+
448+
diff_files = self.repo.git.diff('--name-only', diff_range)
449+
self.show_files = diff_files.splitlines()
450+
451+
log.debug(f"Changed files detected via git diff (merge commit): {self.show_files}")
452+
log.info(f"Changed file detection: method=merge-diff, source=merge-commit-fallback, files={len(self.show_files)}")
453+
return True
454+
455+
except Exception as error:
456+
log.debug(f"Failed to detect merge commit changes: {error}")
457+
return False
458+
383459
def get_default_branch_name(self) -> str:
384460
"""
385461
Get the default branch name from the remote origin.

0 commit comments

Comments
 (0)