diff --git a/commodore/dependency_templater.py b/commodore/dependency_templater.py index 19c8a740..3553c236 100644 --- a/commodore/dependency_templater.py +++ b/commodore/dependency_templater.py @@ -4,6 +4,7 @@ import re import tempfile import shutil +import textwrap from abc import ABC, abstractmethod from pathlib import Path @@ -158,11 +159,25 @@ def create(self) -> None: f"{self.deptype.capitalize()} {self.name} successfully added 🎉", bold=True ) - def commit(self, msg: str, amend=False, init=True) -> None: + def commit(self, msg: str, amend=False, init=True) -> bool: # If we're amending an existing commit, we don't want to force initialize the # repo. repo = GitRepo(self.repo_url, self.target_dir, force_init=not amend and init) - repo.stage_all() + # stage_all() returns the full diff compared to the last commit. Therefore, we + # do stage_files() first and then stage_all(), to ensure we get the complete + # diff. repo.stage_files(self.additional_files) - repo.commit(msg, amend=amend) + diff_text, changed = repo.stage_all() + + if changed: + indented = textwrap.indent(diff_text, " ") + message = f" > Changes:\n{indented}" + else: + message = " > No changes." + click.echo(message) + + if changed: + # Only create a new commmit if there are any changes. + repo.commit(msg, amend=amend) + return changed diff --git a/commodore/package/template.py b/commodore/package/template.py index 0c613643..23dd17dd 100644 --- a/commodore/package/template.py +++ b/commodore/package/template.py @@ -157,12 +157,19 @@ def update(self): extra_context=self.cookiecutter_args, ) - self.commit( + updated = self.commit( "Update from template\n\n" + f"Template version: {self.template_version} ({self.template_commit[:7]})", init=False, ) - click.secho( - f"{self.deptype.capitalize()} {self.name} successfully updated 🎉", bold=True - ) + if updated: + click.secho( + f"{self.deptype.capitalize()} {self.name} successfully updated 🎉", + bold=True, + ) + else: + click.secho( + f"{self.deptype.capitalize()} {self.name} already up-to-date 🎉", + bold=True, + ) diff --git a/tests/test_package_template.py b/tests/test_package_template.py index 39182be9..c1589108 100644 --- a/tests/test_package_template.py +++ b/tests/test_package_template.py @@ -248,6 +248,7 @@ def test_package_update_copyright_holder( @pytest.mark.parametrize( "initial_test_cases,additional_test_cases,remove_test_cases", ( + ([], [], []), ([], ["foo"], []), (["foo"], ["bar"], ["foo"]), (["foo", "bar"], ["baz"], ["foo", "bar"]), @@ -291,7 +292,10 @@ def test_package_update_test_cases( r = GitRepo(None, pkg_dir) assert not r.repo.is_dirty() assert len(r.repo.untracked_files) == 0 - assert r.repo.head.commit.message.startswith("Update from template\n\n") + if additional_test_cases == [] and remove_test_cases == []: + assert r.repo.head.commit.message.startswith("Initial commit") + else: + assert r.repo.head.commit.message.startswith("Update from template\n\n") def test_package_templater_from_existing_nonexistent(tmp_path: Path, config: Config):