|
1 | 1 | """Module containing utility functions used throughout cookiecutter_robust_python scripts.""" |
| 2 | +import json |
2 | 3 | import os |
3 | 4 | import shutil |
4 | 5 | import stat |
|
17 | 18 | import cruft |
18 | 19 | import typer |
19 | 20 | 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 |
20 | 23 | from dotenv import load_dotenv |
21 | 24 | from typer.models import OptionInfo |
22 | 25 |
|
@@ -106,7 +109,34 @@ def is_branch_synced_with_remote(branch: str) -> bool: |
106 | 109 |
|
107 | 110 | def is_ancestor(ancestor: str, descendent: str) -> bool: |
108 | 111 | """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 |
110 | 140 |
|
111 | 141 |
|
112 | 142 | @contextmanager |
|
0 commit comments