Skip to content

Commit 65782dd

Browse files
keshav-spaceAyanSinhaMahapatra
authored andcommitted
Avoid creating empty commits when pushing changes to FederatedCode (#1888)
* Do not enumerate untracked files to compute commit message - Untracked file enumeration is a very costly operation for large git repo Signed-off-by: Keshav Priyadarshi <[email protected]> * Set allow_empty to false to avoid empty commits Signed-off-by: Keshav Priyadarshi <[email protected]> * Bump minecode_pipelines to 0.0.1b8 Signed-off-by: Keshav Priyadarshi <[email protected]> --------- Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent 36afc5b commit 65782dd

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

scanpipe/pipes/federatedcode.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import requests
3434
import saneyaml
35+
from git import GitCommandError
3536
from git import Repo
3637
from packageurl import PackageURL
3738

@@ -199,9 +200,20 @@ def commit_and_push_changes(
199200
remote_name="origin",
200201
logger=None,
201202
):
202-
"""Commit and push changes to remote repository."""
203-
commit_changes(repo, files_to_commit, commit_message, purls)
204-
push_changes(repo, remote_name)
203+
"""
204+
Commit and push changes to remote repository.
205+
Returns True if changes are successfully pushed, False otherwise.
206+
"""
207+
try:
208+
commit_changes(repo, files_to_commit, commit_message, purls, logger)
209+
push_changes(repo, remote_name)
210+
except GitCommandError as e:
211+
if "nothing to commit" in e.stdout.lower():
212+
logger("Nothing to commit, working tree clean.")
213+
else:
214+
logger(f"Error while committing change: {e}")
215+
return False
216+
return True
205217

206218

207219
def commit_changes(
@@ -222,18 +234,9 @@ def commit_changes(
222234
author_name = settings.FEDERATEDCODE_GIT_SERVICE_NAME
223235
author_email = settings.FEDERATEDCODE_GIT_SERVICE_EMAIL
224236

225-
files_added = all(
226-
[
227-
True
228-
for changed_file in files_to_commit
229-
if changed_file in repo.untracked_files
230-
]
231-
)
232-
change_type = "Add" if files_added else "Update"
233-
234237
purls = "\n".join(purls)
235238
commit_message = f"""\
236-
{change_type} {mine_type} results for:
239+
Add {mine_type} results for:
237240
{purls}
238241
239242
Tool: {tool_name}@v{tool_version}
@@ -243,7 +246,11 @@ def commit_changes(
243246
"""
244247

245248
repo.index.add(files_to_commit)
246-
repo.index.commit(textwrap.dedent(commit_message))
249+
repo.git.commit(
250+
m=textwrap.dedent(commit_message),
251+
allow_empty=False,
252+
no_verify=True,
253+
)
247254

248255

249256
def delete_local_clone(repo):

0 commit comments

Comments
 (0)