diff --git a/flepimop/gempyor_pkg/src/gempyor/batch.py b/flepimop/gempyor_pkg/src/gempyor/batch.py index 2510dfc33..7fccea7c9 100644 --- a/flepimop/gempyor_pkg/src/gempyor/batch.py +++ b/flepimop/gempyor_pkg/src/gempyor/batch.py @@ -29,7 +29,7 @@ from .file_paths import run_id from .info import Cluster, get_cluster_info from .logging import get_script_logger -from .utils import _format_cli_options, _git_head, _shutil_which, config +from .utils import _format_cli_options, _git_checkout, _git_head, _shutil_which, config from .shared_cli import ( NONNEGATIVE_DURATION, cli, @@ -981,6 +981,13 @@ def _submit_scenario_job( default=False, help="Flag to reset chimerics on global accept.", ), + click.Option( + param_decls=["--skip-checkout", "skip_checkout"], + type=bool, + default=False, + is_flag=True, + help="Flag to skip checking out a new branch in the project git directory.", + ), ] + list(verbosity_options.values()), ) @@ -1123,3 +1130,15 @@ def _click_submit(ctx: click.Context = mock_context, **kwargs: Any) -> None: kwargs["dry_run"], now, ) + + # Checkout a new branch to preserve run information + if kwargs["skip_checkout"]: + logger.debug("Skipped checking out a new branch to preserve run.") + else: + branch = f"run_{job_name}" + _git_checkout(kwargs["project_path"], branch) + logger.info( + "Checked out a new branch, '%s', in the project " + "repository to preserve run details.", + branch, + ) diff --git a/flepimop/gempyor_pkg/src/gempyor/utils.py b/flepimop/gempyor_pkg/src/gempyor/utils.py index 20ed5de58..5b9b75fa1 100644 --- a/flepimop/gempyor_pkg/src/gempyor/utils.py +++ b/flepimop/gempyor_pkg/src/gempyor/utils.py @@ -1118,6 +1118,29 @@ def _git_head(repository: Path) -> str: return proc.stdout.decode().strip() +def _git_checkout(repository: Path, branch: str) -> None: + """ + Checkout a new branch from a given git repository. + + Args: + repository: A directory under version control with git to + checkout a new branch in. + branch: The name of the new branch to checkout. + + Examples: + >>> from pathlib import Path + >>> _git_checkout(Path("~/Desktop/GitHub/HopkinsIDD/flepiMoP"), "my-new-branch") + """ + git_cmd = _shutil_which("git") + subprocess.run( + [git_cmd, "checkout", "-b", branch], + cwd=repository.expanduser().absolute(), + check=True, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + + def _format_cli_options(options: dict[str, Any]) -> list[str]: """ Convert a dictionary of CLI options into a formatted list.