@@ -128,7 +128,56 @@ def __init__(self, path: str):
128
128
self .commit_sha = self .commit .binsha
129
129
self .commit_message = self .commit .message
130
130
self .committer = self .commit .committer
131
- self .show_files = self .repo .git .show (self .commit , name_only = True , format = "%n" ).splitlines ()
131
+ # Detect changed files in PR/MR context for GitHub, GitLab, Bitbucket; fallback to git show
132
+ self .show_files = []
133
+ detected = False
134
+ # GitHub Actions PR context
135
+ github_base_ref = os .getenv ('GITHUB_BASE_REF' )
136
+ github_head_ref = os .getenv ('GITHUB_HEAD_REF' )
137
+ if github_base_ref and github_head_ref :
138
+ try :
139
+ self .repo .git .fetch ('origin' , github_base_ref , github_head_ref )
140
+ diff_range = f"origin/{ github_base_ref } ...{ self .commit .hexsha } "
141
+ diff_files = self .repo .git .diff ('--name-only' , diff_range )
142
+ self .show_files = diff_files .splitlines ()
143
+ log .debug (f"Changed files detected via git diff (GitHub): { self .show_files } " )
144
+ detected = True
145
+ except Exception as error :
146
+ log .debug (f"Failed to get changed files via git diff (GitHub): { error } " )
147
+ # GitLab CI Merge Request context
148
+ if not detected :
149
+ gitlab_target = os .getenv ('CI_MERGE_REQUEST_TARGET_BRANCH_NAME' )
150
+ gitlab_source = os .getenv ('CI_MERGE_REQUEST_SOURCE_BRANCH_NAME' )
151
+ if gitlab_target and gitlab_source :
152
+ try :
153
+ self .repo .git .fetch ('origin' , gitlab_target , gitlab_source )
154
+ diff_range = f"origin/{ gitlab_target } ...origin/{ gitlab_source } "
155
+ diff_files = self .repo .git .diff ('--name-only' , diff_range )
156
+ self .show_files = diff_files .splitlines ()
157
+ log .debug (f"Changed files detected via git diff (GitLab): { self .show_files } " )
158
+ detected = True
159
+ except Exception as error :
160
+ log .debug (f"Failed to get changed files via git diff (GitLab): { error } " )
161
+ # Bitbucket Pipelines PR context
162
+ if not detected :
163
+ bitbucket_pr_id = os .getenv ('BITBUCKET_PR_ID' )
164
+ bitbucket_source = os .getenv ('BITBUCKET_BRANCH' )
165
+ bitbucket_dest = os .getenv ('BITBUCKET_PR_DESTINATION_BRANCH' )
166
+ # BITBUCKET_BRANCH is the source branch in PR builds
167
+ if bitbucket_pr_id and bitbucket_source and bitbucket_dest :
168
+ try :
169
+ self .repo .git .fetch ('origin' , bitbucket_dest , bitbucket_source )
170
+ diff_range = f"origin/{ bitbucket_dest } ...origin/{ bitbucket_source } "
171
+ diff_files = self .repo .git .diff ('--name-only' , diff_range )
172
+ self .show_files = diff_files .splitlines ()
173
+ log .debug (f"Changed files detected via git diff (Bitbucket): { self .show_files } " )
174
+ detected = True
175
+ except Exception as error :
176
+ log .debug (f"Failed to get changed files via git diff (Bitbucket): { error } " )
177
+ # Fallback to git show for single commit
178
+ if not detected :
179
+ self .show_files = self .repo .git .show (self .commit , name_only = True , format = "%n" ).splitlines ()
180
+ log .debug (f"Changed files detected via git show: { self .show_files } " )
132
181
self .changed_files = []
133
182
for item in self .show_files :
134
183
if item != "" :
0 commit comments