Skip to content

Commit b199631

Browse files
committed
feat: swap to making a new feature branch in demo rather than working from develop always
1 parent 4496785 commit b199631

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

scripts/update-demo.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
import typer
88
from cookiecutter.utils import work_in
99

10+
from util import is_ancestor
11+
from util import get_current_branch
12+
from util import get_current_commit
1013
from util import get_demo_name
14+
from util import get_last_cruft_update_commit
1115
from util import git
1216
from util import FolderOption
1317
from util import REPO_FOLDER
@@ -24,13 +28,28 @@ def update_demo(
2428
) -> None:
2529
"""Runs precommit in a generated project and matches the template to the results."""
2630
try:
27-
develop_branch: str = os.getenv("COOKIECUTTER_ROBUST_PYTHON_DEVELOP_BRANCH", "develop")
2831
demo_name: str = get_demo_name(add_rust_extension=add_rust_extension)
2932
demo_path: Path = demos_cache_folder / demo_name
33+
develop_branch: str = os.getenv("COOKIECUTTER_ROBUST_PYTHON_DEVELOP_BRANCH", "develop")
34+
35+
current_branch: str = get_current_branch()
36+
current_commit: str = get_current_commit()
37+
38+
_validate_is_feature_branch(branch=current_branch)
39+
3040
typer.secho(f"Updating demo project at {demo_path=}.", fg="yellow")
3141
with work_in(demo_path):
3242
require_clean_and_up_to_date_repo()
3343
git("checkout", develop_branch)
44+
45+
last_update_commit: str = get_last_cruft_update_commit(demo_path=demo_path)
46+
if not is_ancestor(last_update_commit, current_commit):
47+
raise ValueError(
48+
f"The last update commit '{last_update_commit}' is not an ancestor of the current commit "
49+
f"'{current_commit}'."
50+
)
51+
52+
git("checkout", "-b", current_branch)
3453
cruft.update(
3554
project_dir=demo_path,
3655
template_path=REPO_FOLDER,
@@ -45,5 +64,11 @@ def update_demo(
4564
sys.exit(1)
4665

4766

67+
def _validate_is_feature_branch(branch: str) -> None:
68+
"""Validates that the cookiecutter has a feature branch checked out."""
69+
if not branch.startswith("feature/"):
70+
raise ValueError(f"Received branch '{branch}' is not a feature branch.")
71+
72+
4873
if __name__ == '__main__':
4974
cli()

scripts/util.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Module containing utility functions used throughout cookiecutter_robust_python scripts."""
2+
import json
23
import os
34
import shutil
45
import stat
@@ -17,6 +18,8 @@
1718
import cruft
1819
import typer
1920
from cookiecutter.utils import work_in
21+
from cruft._commands.utils.cruft import get_cruft_file
22+
from cruft._commands.utils.cruft import json_dumps
2023
from dotenv import load_dotenv
2124
from typer.models import OptionInfo
2225

@@ -106,7 +109,34 @@ def is_branch_synced_with_remote(branch: str) -> bool:
106109

107110
def is_ancestor(ancestor: str, descendent: str) -> bool:
108111
"""Checks if the branch is synced with its remote."""
109-
return git("merge-base", "--is-ancestor", ancestor, descendent).returncode == 0
112+
return git("merge-base", "--is-ancestor", ancestor, descendent, ignore_error=True) is not None
113+
114+
115+
def get_current_branch() -> str:
116+
"""Returns the current branch name."""
117+
return git("branch", "--show-current").stdout.strip()
118+
119+
120+
def get_current_commit() -> str:
121+
"""Returns the current commit reference."""
122+
return git("rev-parse", "HEAD").stdout.strip()
123+
124+
125+
def get_last_cruft_update_commit(demo_path: Path) -> str:
126+
"""Returns the commit id for the last time cruft update was ran."""
127+
existing_cruft_config: dict[str, Any] = _read_cruft_file(demo_path)
128+
last_cookiecutter_commit: Optional[str] = existing_cruft_config.get("commit", None)
129+
if last_cookiecutter_commit is None:
130+
raise ValueError("Could not find last commit id used to generate demo.")
131+
return last_cookiecutter_commit
132+
133+
134+
def _read_cruft_file(project_path: Path) -> dict[str, Any]:
135+
"""Reads the cruft file for the project path provided and returns the results."""
136+
cruft_path: Path = get_cruft_file(project_dir_path=project_path)
137+
cruft_text: str = cruft_path.read_text()
138+
cruft_config: dict[str, Any] = json.loads(cruft_text)
139+
return cruft_config
110140

111141

112142
@contextmanager

0 commit comments

Comments
 (0)