Skip to content

Commit 4fc565f

Browse files
committed
chore: move the checkout code to the online mode block and fix docs
Signed-off-by: behnazh-w <behnaz.hassanshahi@oracle.com>
1 parent 93e621a commit 4fc565f

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

docs/source/pages/using.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,6 @@ An example configuration file for utilising this feature:
304304
Analyzing a repository on the local file system
305305
-----------------------------------------------
306306

307-
.. warning::
308-
During the analysis, Macaron can check out different commits, which can reset the index and working tree of the repository.
309-
Therefore, any uncommitted changes in the repository need to be backed up to prevent loss (these include unstaged changes, staged changes and untracked files).
310-
However, Macaron will not modify the history of the repository.
311-
312307
.. note::
313308
We assume that the ``origin`` remote exists in the cloned repository and checkout the relevant commits from ``origin`` only.
314309

@@ -378,7 +373,12 @@ With ``rest_of_args`` being the arguments to the ``analyze`` command (e.g. ``--b
378373

379374
The ``--local-repos-path/-lr`` flag tells Macaron to look into ``./boo/foo`` for local repositories. For more information, please see :ref:`Command Line Usage <cli-usage>`.
380375

381-
.. note:: If ``--local-repos-path/-lr`` is not provided, Macaron will looks inside ``<current_working_directory>/output/git_repos/local_repos/`` whenever you provide a local path to ``--repo-path/-rp``.
376+
.. note:: If ``--local-repos-path/-lr`` is not provided, Macaron will look inside ``<current_working_directory>/output/git_repos/local_repos/`` whenever you provide a local path to ``--repo-path/-rp``.
377+
378+
.. warning::
379+
Macaron by default analyzes the current state of the local repository. However, if the user provides a branch or commit hash as input, Macaron may reset the index and working tree of the repository to check out a specific commit.
380+
Therefore, any uncommitted changes in the repository need to be backed up to prevent loss (these include unstaged changes, staged changes and untracked files).
381+
However, Macaron will not modify the history of the repository.
382382

383383
-------------------------
384384
Running the policy engine

src/macaron/slsa_analyzer/git_url.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ def check_out_repo_target(
123123
will prune and update all references (e.g. tags, branches) to make sure that the local repository is up-to-date
124124
with the repository specified by origin remote.
125125
126-
If ``branch_name`` and a commit are not provided, this function will not do anything and the latest local commit
127-
will be analyzed. If there are uncommitted local changes, the latest commit will appear in the report but the repo
128-
with local changes will be analyzed. We leave it up to the user to decide whether to commit the changes or not.
126+
If ``offline_mode`` is True and neither ``branch_name`` nor commit are provided, this function will not do anything
127+
and the latest local commit will be analyzed. If there are uncommitted local changes, the latest commit will
128+
appear in the report but the repo with local changes will be analyzed. We leave it up to the user to decide
129+
whether to commit the changes or not.
129130
130131
If ``branch_name`` is provided and a commit is not provided, this function will checkout that branch from origin
131132
remote (i.e. origin/<branch_name).
@@ -182,6 +183,15 @@ def check_out_repo_target(
182183
logger.error("Unable to fetch from the origin remote of the repository.")
183184
return False
184185

186+
# By default check out the commit at origin/HEAD only when offline_mode is False.
187+
if not branch_name and not digest:
188+
try:
189+
git_obj.repo.git.checkout("--force", "origin/HEAD")
190+
except GitCommandError:
191+
logger.debug("Cannot checkout the default branch at origin/HEAD")
192+
return False
193+
194+
# The following checkout operations will be done whether offline_mode is False or not.
185195
if branch_name and not digest:
186196
try:
187197
git_obj.repo.git.checkout("--force", f"origin/{branch_name}")
@@ -223,7 +233,7 @@ def check_out_repo_target(
223233
logger.critical("The current HEAD at %s. Expect %s.", final_head_commit.hexsha, digest)
224234
return False
225235

226-
logger.info("Successfully checked out commit %s.", final_head_commit.hexsha)
236+
logger.info("The latest commit is %s.", final_head_commit.hexsha)
227237
return True
228238

229239

0 commit comments

Comments
 (0)