77import typer
88from 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
1013from util import get_demo_name
14+ from util import get_last_cruft_update_commit
1115from util import git
1216from util import FolderOption
1317from util import REPO_FOLDER
@@ -27,13 +31,28 @@ def update_demo(
2731) -> None :
2832 """Runs precommit in a generated project and matches the template to the results."""
2933 try :
30- develop_branch : str = os .getenv ("COOKIECUTTER_ROBUST_PYTHON_DEVELOP_BRANCH" , "develop" )
3134 demo_name : str = get_demo_name (add_rust_extension = add_rust_extension )
3235 demo_path : Path = demos_cache_folder / demo_name
36+ develop_branch : str = os .getenv ("COOKIECUTTER_ROBUST_PYTHON_DEVELOP_BRANCH" , "develop" )
37+
38+ current_branch : str = get_current_branch ()
39+ current_commit : str = get_current_commit ()
40+
41+ _validate_is_feature_branch (branch = current_branch )
42+
3343 typer .secho (f"Updating demo project at { demo_path = } ." , fg = "yellow" )
3444 with work_in (demo_path ):
3545 require_clean_and_up_to_date_repo ()
3646 git ("checkout" , develop_branch )
47+
48+ last_update_commit : str = get_last_cruft_update_commit (demo_path = demo_path )
49+ if not is_ancestor (last_update_commit , current_commit ):
50+ raise ValueError (
51+ f"The last update commit '{ last_update_commit } ' is not an ancestor of the current commit "
52+ f"'{ current_commit } '."
53+ )
54+
55+ git ("checkout" , "-b" , current_branch )
3756 uv ("python" , "pin" , min_python_version )
3857 uv ("python" , "install" , min_python_version )
3958 cruft .update (
@@ -56,5 +75,11 @@ def update_demo(
5675 sys .exit (1 )
5776
5877
78+ def _validate_is_feature_branch (branch : str ) -> None :
79+ """Validates that the cookiecutter has a feature branch checked out."""
80+ if not branch .startswith ("feature/" ):
81+ raise ValueError (f"Received branch '{ branch } ' is not a feature branch." )
82+
83+
5984if __name__ == '__main__' :
6085 cli ()
0 commit comments