dycw-pre-commit-hooks 0.12.2__tar.gz → 0.12.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dycw-pre-commit-hooks might be problematic. Click here for more details.

Files changed (21) hide show
  1. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/PKG-INFO +1 -1
  2. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/pyproject.toml +2 -2
  3. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/pre_commit_hooks/__init__.py +1 -1
  4. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/pre_commit_hooks/common.py +25 -5
  5. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/pre_commit_hooks/format_requirements/__init__.py +1 -3
  6. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/pre_commit_hooks/run_bump_my_version/__init__.py +13 -29
  7. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/pre_commit_hooks/tag_commits/__init__.py +23 -9
  8. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/.gitignore +0 -0
  9. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/README.md +0 -0
  10. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/pre_commit_hooks/format_requirements/__main__.py +0 -0
  11. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/pre_commit_hooks/py.typed +0 -0
  12. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/pre_commit_hooks/replace_sequence_str/__init__.py +0 -0
  13. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/pre_commit_hooks/replace_sequence_str/__main__.py +0 -0
  14. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/pre_commit_hooks/run_bump_my_version/__main__.py +0 -0
  15. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/pre_commit_hooks/tag_commits/__main__.py +0 -0
  16. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/tests/__init__.py +0 -0
  17. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/tests/format_requirements/__init__.py +0 -0
  18. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/tests/format_requirements/in.toml +0 -0
  19. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/tests/format_requirements/out.toml +0 -0
  20. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/tests/format_requirements/test_format_requirements.py +0 -0
  21. {dycw_pre_commit_hooks-0.12.2 → dycw_pre_commit_hooks-0.12.4}/src/tests/test_main.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dycw-pre-commit-hooks
3
- Version: 0.12.2
3
+ Version: 0.12.4
4
4
  Author-email: Derek Wan <d.wan@icloud.com>
5
5
  Requires-Python: >=3.12
6
6
  Requires-Dist: click<8.3,>=8.2.1
@@ -30,7 +30,7 @@ dependencies = [
30
30
  name = "dycw-pre-commit-hooks"
31
31
  readme = "README.md"
32
32
  requires-python = ">= 3.12"
33
- version = "0.12.2"
33
+ version = "0.12.4"
34
34
 
35
35
  [project.scripts]
36
36
  format-requirements = "pre_commit_hooks.format_requirements:main"
@@ -41,7 +41,7 @@ tag-commits = "pre_commit_hooks.tag_commits:main"
41
41
  # bump-my-version
42
42
  [tool.bumpversion]
43
43
  allow_dirty = true
44
- current_version = "0.12.2"
44
+ current_version = "0.12.4"
45
45
 
46
46
  [[tool.bumpversion.files]]
47
47
  filename = "src/pre_commit_hooks/__init__.py"
@@ -1,3 +1,3 @@
1
1
  from __future__ import annotations
2
2
 
3
- __version__ = "0.12.2"
3
+ __version__ = "0.12.4"
@@ -1,20 +1,30 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from pathlib import Path
4
- from typing import assert_never
4
+ from typing import Literal, assert_never
5
5
 
6
+ from click import Choice, option
6
7
  from loguru import logger
7
8
  from tomlkit import TOMLDocument, parse
8
9
  from tomlkit.items import Table
9
- from utilities.pathlib import get_repo_root
10
+ from utilities.typing import get_literal_elements
10
11
  from utilities.version import Version, parse_version
11
12
 
12
- PYPROJECT_TOML = get_repo_root().joinpath("pyproject.toml")
13
+ type Mode = Literal["pyproject", "bumpversion"]
14
+ DEFAULT_MODE: Mode = "pyproject"
15
+ mode_option = option(
16
+ "--mode",
17
+ type=Choice(get_literal_elements(Mode), case_sensitive=False),
18
+ default=DEFAULT_MODE,
19
+ show_default=True,
20
+ )
13
21
 
14
22
 
15
- def get_version(source: Path | str | bytes | TOMLDocument, /) -> Version:
23
+ def get_version(source: Mode | Path | str | bytes | TOMLDocument, /) -> Version:
16
24
  """Get the `[tool.bumpversion]` version from a TOML file."""
17
25
  match source:
26
+ case "pyproject" | "bumpversion" as mode:
27
+ return get_version(get_toml_path(mode))
18
28
  case Path() as path:
19
29
  return get_version(path.read_text())
20
30
  case str() | bytes() as text:
@@ -53,4 +63,14 @@ def get_version(source: Path | str | bytes | TOMLDocument, /) -> Version:
53
63
  assert_never(never)
54
64
 
55
65
 
56
- __all__ = ["PYPROJECT_TOML", "get_version"]
66
+ def get_toml_path(mode: Mode = DEFAULT_MODE, /) -> Path:
67
+ match mode:
68
+ case "pyproject":
69
+ return Path("pyproject.toml")
70
+ case "bumpversion":
71
+ return Path(".bumpversion.toml")
72
+ case never: # pyright: ignore[reportUnnecessaryComparison]
73
+ assert_never(never)
74
+
75
+
76
+ __all__ = ["DEFAULT_MODE", "Mode", "get_toml_path", "get_version", "mode_option"]
@@ -15,8 +15,6 @@ from tomlkit import array, dumps, loads, string
15
15
  from tomlkit.items import Array, Table
16
16
  from utilities.atomicwrites import writer
17
17
 
18
- from pre_commit_hooks.common import PYPROJECT_TOML
19
-
20
18
  if TYPE_CHECKING:
21
19
  from collections.abc import Iterator
22
20
  from pathlib import Path
@@ -106,4 +104,4 @@ class _CustomSpecifierSet(SpecifierSet):
106
104
  return [">=", "<"].index(spec.operator)
107
105
 
108
106
 
109
- __all__ = ["PYPROJECT_TOML", "main"]
107
+ __all__ = ["main"]
@@ -1,35 +1,30 @@
1
1
  from __future__ import annotations
2
2
 
3
- from pathlib import Path
4
3
  from subprocess import PIPE, STDOUT, CalledProcessError, check_call, check_output
5
- from typing import Literal, assert_never
6
4
 
7
- from click import Choice, command, option
5
+ from click import command
8
6
  from loguru import logger
9
- from utilities.pathlib import get_repo_root
10
7
 
11
- from pre_commit_hooks.common import PYPROJECT_TOML, get_version
12
-
13
- type _Mode = Literal["pyproject", "bumpversion"]
14
- _MODE: _Mode = "pyproject"
8
+ from pre_commit_hooks.common import (
9
+ DEFAULT_MODE,
10
+ Mode,
11
+ get_toml_path,
12
+ get_version,
13
+ mode_option,
14
+ )
15
15
 
16
16
 
17
17
  @command()
18
- @option(
19
- "--mode",
20
- type=Choice(["pyproject", "bumpversion"], case_sensitive=False),
21
- default=_MODE,
22
- show_default=True,
23
- )
24
- def main(*, mode: _Mode = _MODE) -> bool:
18
+ @mode_option
19
+ def main(*, mode: Mode = DEFAULT_MODE) -> bool:
25
20
  """CLI for the `run_bump_my_version` hook."""
26
21
  return _process(mode=mode)
27
22
 
28
23
 
29
- def _process(*, mode: _Mode = _MODE) -> bool:
30
- path = _get_rel_path(mode=mode)
31
- current = get_version(path)
24
+ def _process(*, mode: Mode = DEFAULT_MODE) -> bool:
25
+ current = get_version(mode)
32
26
  commit = check_output(["git", "rev-parse", "origin/master"], text=True).rstrip("\n")
27
+ path = get_toml_path(mode)
33
28
  contents = check_output(["git", "show", f"{commit}:{path}"], text=True)
34
29
  master = get_version(contents)
35
30
  if current in {master.bump_patch(), master.bump_minor(), master.bump_major()}:
@@ -55,15 +50,4 @@ def _process(*, mode: _Mode = _MODE) -> bool:
55
50
  return False
56
51
 
57
52
 
58
- def _get_rel_path(*, mode: _Mode = _MODE) -> Path:
59
- match mode:
60
- case "pyproject":
61
- path = PYPROJECT_TOML
62
- case "bumpversion":
63
- path = get_repo_root().joinpath(".bumpversion.toml")
64
- case never: # pyright: ignore[reportUnnecessaryComparison]
65
- assert_never(never)
66
- return path.relative_to(Path.cwd())
67
-
68
-
69
53
  __all__ = ["main"]
@@ -13,7 +13,13 @@ from utilities.whenever import from_timestamp, get_now_local
13
13
  from whenever import DateTimeDelta, ZonedDateTime
14
14
  from xdg_base_dirs import xdg_cache_home
15
15
 
16
- from pre_commit_hooks.common import get_version
16
+ from pre_commit_hooks.common import (
17
+ DEFAULT_MODE,
18
+ Mode,
19
+ get_toml_path,
20
+ get_version,
21
+ mode_option,
22
+ )
17
23
 
18
24
  if TYPE_CHECKING:
19
25
  from collections.abc import Set as AbstractSet
@@ -35,26 +41,29 @@ _MAX_AGE: DateTimeDelta | None = None
35
41
  default=_MAX_AGE,
36
42
  show_default=True,
37
43
  )
44
+ @mode_option
38
45
  def main(
39
46
  *,
40
47
  run_every: DateTimeDelta | None = _RUN_EVERY,
41
48
  max_age: DateTimeDelta | None = _MAX_AGE,
49
+ mode: Mode = DEFAULT_MODE,
42
50
  ) -> bool:
43
51
  """CLI for the `tag_commits` hook."""
44
- return _process(run_every=run_every, max_age=max_age)
52
+ return _process(run_every=run_every, max_age=max_age, mode=mode)
45
53
 
46
54
 
47
55
  def _process(
48
56
  *,
49
57
  run_every: DateTimeDelta | None = _RUN_EVERY,
50
58
  max_age: DateTimeDelta | None = _MAX_AGE,
59
+ mode: Mode = DEFAULT_MODE,
51
60
  ) -> bool:
52
61
  if run_every is not None:
53
62
  last = _get_last_run()
54
63
  min_date_time = get_now_local() - run_every
55
64
  if (last is not None) and (min_date_time <= last):
56
65
  return True
57
- return _process_commits(max_age=max_age)
66
+ return _process_commits(max_age=max_age, mode=mode)
58
67
 
59
68
 
60
69
  def _get_last_run() -> ZonedDateTime | None:
@@ -70,13 +79,16 @@ def _get_last_run() -> ZonedDateTime | None:
70
79
  return None
71
80
 
72
81
 
73
- def _process_commits(*, max_age: DateTimeDelta | None = None) -> bool:
82
+ def _process_commits(
83
+ *, max_age: DateTimeDelta | None = None, mode: Mode = DEFAULT_MODE
84
+ ) -> bool:
74
85
  repo = Repo(".", search_parent_directories=True)
75
86
  tagged = {tag.commit.hexsha for tag in repo.tags}
76
87
  min_date_time = None if max_age is None else (get_now_local() - max_age)
77
88
  commits = reversed(list(repo.iter_commits(repo.refs["origin/master"])))
78
89
  results = [
79
- _process_commit(c, tagged, repo, min_date_time=min_date_time) for c in commits
90
+ _process_commit(c, tagged, repo, min_date_time=min_date_time, mode=mode)
91
+ for c in commits
80
92
  ] # run all
81
93
  return all(results)
82
94
 
@@ -88,13 +100,14 @@ def _process_commit(
88
100
  /,
89
101
  *,
90
102
  min_date_time: ZonedDateTime | None = None,
103
+ mode: Mode = DEFAULT_MODE,
91
104
  ) -> bool:
92
105
  if (commit.hexsha in tagged) or (
93
106
  (min_date_time is not None) and (_get_date_time(commit) < min_date_time)
94
107
  ):
95
108
  return True
96
109
  try:
97
- _tag_commit(commit, repo)
110
+ _tag_commit(commit, repo, mode=mode)
98
111
  except GitCommandError:
99
112
  return False
100
113
  return True
@@ -104,13 +117,14 @@ def _get_date_time(commit: Commit, /) -> ZonedDateTime:
104
117
  return from_timestamp(commit.committed_date, time_zone=LOCAL_TIME_ZONE_NAME)
105
118
 
106
119
 
107
- def _tag_commit(commit: Commit, repo: Repo, /) -> None:
120
+ def _tag_commit(commit: Commit, repo: Repo, /, *, mode: Mode = DEFAULT_MODE) -> None:
108
121
  sha = commit.hexsha[:7]
109
122
  date = _get_date_time(commit)
123
+ path = get_toml_path(mode)
110
124
  try:
111
- joined = commit.tree.join("pyproject.toml")
125
+ joined = commit.tree.join(str(path))
112
126
  except KeyError:
113
- logger.exception(f"`pyproject.toml` not found; failed to tag {sha!r} ({date})")
127
+ logger.exception(f"`{str(path)!r}` not found; failed to tag {sha!r} ({date})")
114
128
  return
115
129
  text = joined.data_stream.read()
116
130
  version = get_version(text)