Skip to content

Commit 44c07b4

Browse files
authored
chore: use --force for running git checkout to prevent issues like #530 (#536)
Signed-off-by: Trong Nhan Mai <trong.nhan.mai@oracle.com>
1 parent ec4e190 commit 44c07b4

File tree

3 files changed

+9
-45
lines changed

3 files changed

+9
-45
lines changed

docs/source/pages/using.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ An example configuration file for utilising this feature:
292292
Analyzing a locally cloned repository
293293
-------------------------------------
294294

295+
.. warning::
296+
During the analysis, Macaron can check out different commits, which can reset the index and working tree of the repository.
297+
Therefore, any uncommitted changes in the repository need to be backed up to prevent loss (these include unstaged changes, staged changes and untracked files).
298+
However, Macaron will not modify the history of the repository.
299+
295300
If you have a local repository that you want to analyze, Macaron also supports running the analysis against a local repository.
296301

297302
Assume that the dir tree at the local repository has the following components:

src/macaron/slsa_analyzer/analyzer.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,10 +695,6 @@ def _prepare_repo(
695695
logger.error("The target repository does not have any commit.")
696696
return None
697697

698-
if not git_url.reset_git_repo(git_obj):
699-
logger.error("Cannot reset the target repository.")
700-
return None
701-
702698
# Checking out the specific branch or commit. This operation varies depends on the git service that the
703699
# repository uses.
704700
if not is_remote:

src/macaron/slsa_analyzer/git_url.py

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,6 @@
2525
logger: logging.Logger = logging.getLogger(__name__)
2626

2727

28-
def reset_git_repo(git_obj: Git, stash: bool = True, index: bool = True, working_tree: bool = True) -> bool:
29-
"""Reset the index and working tree of the target repository.
30-
31-
Note that this method does not reset any untracked or ignored files.
32-
33-
Parameters
34-
----------
35-
git_obj : Git
36-
The pydriller.Git object of the repository.
37-
stash : bool
38-
If True, any uncommitted changes will be stashed.
39-
index : bool
40-
If True, the index of the repository will be reset.
41-
working_tree : bool
42-
If True, the working tree will be forcefully adjusted to match HEAD, possibly overwriting uncommitted changes.
43-
If working_tree is True, index must be true as well.
44-
45-
Returns
46-
-------
47-
bool
48-
True if no errors encountered, else False.
49-
"""
50-
try:
51-
if stash:
52-
logger.info("Stashing any uncommitted changes.")
53-
stash_out = git_obj.repo.git.stash(message="Stashing uncommitted changes by Macaron.")
54-
logger.debug("\t Git CMD output: %s", stash_out)
55-
56-
logger.info("Forcefully reset the repository.")
57-
git_obj.repo.head.reset(index=index, working_tree=working_tree)
58-
return True
59-
except GitCommandError as error:
60-
logger.error("Error while trying to reset untracked changes in the repository: %s", error)
61-
return False
62-
except ValueError as error:
63-
logger.error(error)
64-
return False
65-
66-
6728
def check_out_repo_target(git_obj: Git, branch_name: str = "", digest: str = "", offline_mode: bool = False) -> bool:
6829
"""Checkout the branch and commit specified by the user.
6930
@@ -123,7 +84,8 @@ def check_out_repo_target(git_obj: Git, branch_name: str = "", digest: str = "",
12384

12485
try:
12586
# Switch to the target branch by running ``git checkout <branch_name>`` in the target repository.
126-
git_obj.repo.git.checkout(res_branch)
87+
# We need to use force checkout to prevent issues similar to https://github.com/oracle/macaron/issues/530.
88+
git_obj.repo.git.checkout("--force", res_branch)
12789
except GitCommandError as error:
12890
logger.error("Cannot checkout branch %s. Error: %s", res_branch, error)
12991
return False
@@ -153,8 +115,9 @@ def check_out_repo_target(git_obj: Git, branch_name: str = "", digest: str = "",
153115

154116
if digest:
155117
# Checkout the specific commit that the user want by running ``git checkout <commit>`` in the target repository.
118+
# We need to use force checkout to prevent issues similar to https://github.com/oracle/macaron/issues/530.
156119
try:
157-
git_obj.repo.git.checkout(digest)
120+
git_obj.repo.git.checkout("--force", digest)
158121
except GitCommandError as error:
159122
logger.error(
160123
"Commit %s cannot be checked out. Error: %s",

0 commit comments

Comments
 (0)