Skip to content

Commit 198f365

Browse files
authored
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 5e4aff9 commit 198f365

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ android_analysis = [
121121
"android_inspector==0.0.1"
122122
]
123123
mining = [
124-
"minecode_pipelines==0.0.1b1"
124+
"minecode_pipelines==0.0.1b8"
125125
]
126126

127127
[project.urls]

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

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

202214

203215
def commit_changes(
@@ -218,18 +230,9 @@ def commit_changes(
218230
author_name = settings.FEDERATEDCODE_GIT_SERVICE_NAME
219231
author_email = settings.FEDERATEDCODE_GIT_SERVICE_EMAIL
220232

221-
files_added = all(
222-
[
223-
True
224-
for changed_file in files_to_commit
225-
if changed_file in repo.untracked_files
226-
]
227-
)
228-
change_type = "Add" if files_added else "Update"
229-
230233
purls = "\n".join(purls)
231234
commit_message = f"""\
232-
{change_type} {mine_type} results for:
235+
Add {mine_type} results for:
233236
{purls}
234237
235238
Tool: {tool_name}@v{tool_version}
@@ -239,7 +242,11 @@ def commit_changes(
239242
"""
240243

241244
repo.index.add(files_to_commit)
242-
repo.index.commit(textwrap.dedent(commit_message))
245+
repo.git.commit(
246+
m=textwrap.dedent(commit_message),
247+
allow_empty=False,
248+
no_verify=True,
249+
)
243250

244251

245252
def delete_local_clone(repo):

0 commit comments

Comments
 (0)