Skip to content

Commit

Permalink
Merge pull request #14 from Tranquility2/fix_missing_file_index
Browse files Browse the repository at this point in the history
Fix missing file index
  • Loading branch information
Tranquility2 authored Apr 20, 2024
2 parents dc45bcd + 61e7a83 commit 42878a7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ I wanted a simple tool that I could use in my CI/CD pipeline to bump the version
❇️ Bump the version number and update the git tag in one command
❇️ Dry run mode
❇️ Verbose mode
❇️ Custom commit message
❇️ Auto commit message
🆕 Version 0.4+ introduces the ability to automatically update the version number in a file
❇️ Custom commit message*
❇️ Auto commit message*
🆕 Version 0.4+ introduces the ability to automatically update the version number in a file*

Please keep in mind it is designed to be used in a CI/CD pipeline (but not limited to...)
<sup>*Please see the [limitations](#Limitations) section below</sup>

## How to use

Expand Down Expand Up @@ -69,6 +69,13 @@ Options:
--help Show this message and exit.
```

## Limitations
Please keep in mind that when using features like `commit message` / `auto commit message` and `version file` the tool will try and commit the changes to the git repo.

Even though this is quite handy, it should be used mannually as it **cannot be used directly in a CI/CD pipeline directed at `master` or `main` branches** as it will likly fail due to the commit not being allowed without a PR.

💡 Only git tags can be pushed to the remote without a PR *(and this is the main use case for this tool)*.

## Development

Please see [CONTRIBUTING.md](CONTRIBUTING.md)
Expand Down
2 changes: 2 additions & 0 deletions src/semvergit/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from semvergit.file_utils import update_verion_file
from semvergit.git_utils import (
add_file,
get_active_branch,
get_repo,
get_tags_with_prefix,
Expand Down Expand Up @@ -88,6 +89,7 @@ def update(
if version_file:
logger.info(f"📝 Writing version to {version_file}...")
update_verion_file(version_file, new_version, dry_run)
add_file(repo=self.current_repo, file_path=version_file, dry_run=dry_run)

if not commit_message:
# Upading the version file requires a commit message
Expand Down
7 changes: 7 additions & 0 deletions src/semvergit/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def new_commit(repo: Repo, message: str) -> str:
return short_commit_id


@drywrap
def add_file(repo: Repo, file_path: str) -> None:
"""Add file."""
repo.index.add([file_path])
logger.debug(f"Added file {file_path}")


@drywrap
def set_tag(repo: Repo, tag: str) -> VersionInfo:
"""Set tag."""
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ def new_commit(repo: Repo, message: str, dry_run: bool) -> None: # pylint: disa
monkeypatch.setattr("semvergit.app.new_commit", new_commit)


@pytest.fixture(autouse=True)
def mock_add_file(monkeypatch: MonkeyPatch) -> None:
"""Mock add_file."""

def add_file(repo: Repo, file_path: str, dry_run: bool) -> None: # pylint: disable=unused-argument
pass

monkeypatch.setattr("semvergit.app.add_file", add_file)


@pytest.fixture()
def mock_update_verion_file(monkeypatch: MonkeyPatch) -> None:
"""Mock update_verion_file."""
Expand Down
18 changes: 18 additions & 0 deletions tests/test_git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pytest import MonkeyPatch, mark

from semvergit.git_utils import (
add_file,
drywrap,
get_active_branch,
get_repo,
Expand Down Expand Up @@ -147,6 +148,23 @@ def commit(message: str) -> MockCommit: # pylint: disable=unused-argument
assert result == "112233"


def test_add_file() -> None:
"""Test add_file."""

class MockIndex: # pylint: disable=too-few-public-methods
"""Mock index."""

@staticmethod
def add(files: List[str]) -> None: # pylint: disable=unused-argument
"""Add."""
pass # pylint: disable=unnecessary-pass

test_repo = Repo()

MonkeyPatch().setattr("semvergit.git_utils.Repo.index", MockIndex)
add_file(test_repo, "testfile")


def test_set_tag() -> None:
"""Test set_tag."""

Expand Down

0 comments on commit 42878a7

Please sign in to comment.