Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cruft compatibility, read from .cruft.json #756

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ when you're only importing a handful of commits at a time.

**Important:**

Retrocookie relies on a ``.cookiecutter.json`` file in the generated project
to work out how to rewrite commits.
This file is similar to the ``cookiecutter.json`` file in the template,
Retrocookie relies on a ``.cookiecutter.json`` or ``.cruft.json`` context file in
the generated project to work out how to rewrite commits.
The ``.cookiecutter.json`` file is similar to ``cookiecutter.json`` in the template,
but contains the specific values chosen during project generation.
You can generate this file by putting it into the template directory in the Cookiecutter,
with the following contents:
Expand All @@ -72,6 +72,9 @@ with the following contents:

{{ cookiecutter | jsonify }}

The ``.cruft.json`` file is generated automatically by cruft_ when using it instead of
Cookiecutter.


Requirements
------------
Expand Down Expand Up @@ -315,6 +318,7 @@ This project was generated from `@cjolowicz`_'s `Hypermodern Python Cookiecutter

.. _@cjolowicz: https://github.com/cjolowicz
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _cruft: https://github.com/cruft/cruft
.. _Dependabot: https://dependabot.com/
.. _GitHub: https://github.com/
.. _Hypermodern Python Cookiecutter: https://github.com/cjolowicz/cookiecutter-hypermodern-python
Expand Down
4 changes: 2 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def mypy(session: Session) -> None:
def tests(session: Session) -> None:
"""Run the test suite."""
session.install(".[pr]")
session.install("cookiecutter", "coverage[toml]", "pygments", "pytest")
session.install("cookiecutter", "cruft", "coverage[toml]", "pygments", "pytest")
try:
session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs)
finally:
Expand All @@ -157,7 +157,7 @@ def coverage(session: Session) -> None:
def typeguard(session: Session) -> None:
"""Runtime type checking using Typeguard."""
session.install(".[pr]")
session.install("cookiecutter", "pytest", "typeguard", "pygments")
session.install("cookiecutter", "cruft", "pytest", "typeguard", "pygments")
session.run("pytest", f"--typeguard-packages={package}", *session.posargs)


Expand Down
78 changes: 71 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sphinx = ">=4.3.0"
sphinx-autobuild = ">=2020.9.1"
pre-commit = ">=2.15.0"
cookiecutter = ">=1.7.3"
cruft = ">=2.15.0"
pygments = ">=2.10.0"
flake8 = ">=4.0.1"
black = ">=21.10b0"
Expand Down
6 changes: 3 additions & 3 deletions src/retrocookie/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def main(

The path to the source repository is passed as a positional argument.
This should be an instance of the Cookiecutter template with a
.cookiecutter.json file.
.cookiecutter.json or .cruft.json context file.

Additional positional arguments are the commits to be imported.
See gitrevisions(7) for a list of ways to spell commits and commit
Expand All @@ -98,11 +98,11 @@ def main(
\b
- Files are moved to the template directory
- Tokens with special meaning in Jinja are escaped
- Values from .cookiecutter.json are replaced by templating tags
- Values from the context file are replaced by templating tags
with the corresponding variable name

Use the --include-variable and --exclude-variable options to include or
exclude specific variables from .cookiecutter.json.
exclude specific variables from the context file.
"""
if create:
if create_branch:
Expand Down
17 changes: 11 additions & 6 deletions src/retrocookie/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ def find_template_directory(repository: git.Repository) -> Path:


def load_context(repository: git.Repository, ref: str) -> Dict[str, Any]:
"""Load the context from the .cookiecutter.json file."""
path = Path(".cookiecutter.json")
text = repository.read_text(path, ref=ref)
data = json.loads(text)
"""Load the context from the .cookiecutter.json or .cruft.json file."""
try:
path = Path(".cookiecutter.json")
text = repository.read_text(path, ref=ref)
data = json.loads(text)
except KeyError:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why KeyError?

Copy link
Owner

@cjolowicz cjolowicz Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that what git.Repository.read_text raises when it doesn't find the file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be!

repository.read_text(path, ref=ref)
Traceback (most recent call last):
  File "/home/micael/projects/retrocookie/src/retrocookie/core.py", line 42, in load_context
    text = repository.read_text(path, ref=ref)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/micael/projects/retrocookie/src/retrocookie/git.py", line 200, in read_text
    blob = functools.reduce(operator.truediv, path.parts, commit.tree)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: '.cookiecutter.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/micael/projects/retrocookie/src/retrocookie/git.py", line 200, in read_text
    blob = functools.reduce(operator.truediv, path.parts, commit.tree)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: '.cookiecutter.json'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it'd be better to, for example, modify the read_text method to raise a clearer error, like a FileNotFoundError, instead?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly, but not in this PR. Sorry for the slow review, have little bandwidth atm

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. And sorry for pinging you. I had forgotten about this PR, then realized it had been over a year with no activity.

path = Path(".cruft.json")
text = repository.read_text(path, ref=ref)
data = json.loads(text)["context"]["cookiecutter"]
if not isinstance(data, dict) or not all(isinstance(key, str) for key in data):
raise TypeError(".cookiecutter.json does not contain an object")
raise TypeError(f"{path} does not contain an object")
return cast(Dict[str, Any], data)


Expand Down Expand Up @@ -125,7 +130,7 @@ def retrocookie(

Args:
instance_path: The source repository, an instance of the Cookiecutter
template with a ``.cookiecutter.json`` file.
template with a ``.cookiecutter.json`` or ``.cruft.json`` context file.

commits: The commits to be imported from the source repository.

Expand Down
6 changes: 4 additions & 2 deletions src/retrocookie/pr/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ def _(event: events.TemplateNotFound) -> None:
console.failure(
f"Project template for [repository]{event.project.full_name}[/] not found"
)
console.hint("Does the project contain a .cookiecutter.json file?")
console.hint(
'Does the .cookiecutter.json contain the repository URL under "_template"?'
"Does the project contain a .cookiecutter.json or .cruft.json context file?"
)
console.hint(
'Does the context file contain the repository URL under "_template"?'
)
console.hint("Is the template repository on GitHub?")

Expand Down
Loading