Skip to content

Commit

Permalink
Merge pull request #561 from projectsyn/fix/package-update-commit
Browse files Browse the repository at this point in the history
Don't create empty commits with `package update`
  • Loading branch information
simu authored Jul 20, 2022
2 parents 8e5a75a + d0d3faf commit ae70c2a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
21 changes: 18 additions & 3 deletions commodore/dependency_templater.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import tempfile
import shutil
import textwrap

from abc import ABC, abstractmethod
from pathlib import Path
Expand Down Expand Up @@ -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
15 changes: 11 additions & 4 deletions commodore/package/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
6 changes: 5 additions & 1 deletion tests/test_package_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit ae70c2a

Please sign in to comment.