Skip to content

Commit

Permalink
Add git branch checkout to preserve run.
Browse files Browse the repository at this point in the history
Added internal `_git_checkout` utility to checkout a new branch. The
`_click_submit` tool now checks out a new branch based on the name of
the run at the end of the tool. Still need to add unit tests for
`_git_checkout`.
  • Loading branch information
TimothyWillard committed Nov 12, 2024
1 parent 08ec61e commit b6c95ca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
21 changes: 20 additions & 1 deletion flepimop/gempyor_pkg/src/gempyor/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()),
)
Expand Down Expand Up @@ -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,
)
23 changes: 23 additions & 0 deletions flepimop/gempyor_pkg/src/gempyor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b6c95ca

Please sign in to comment.