Skip to content

Commit 812d82f

Browse files
authored
Merge pull request #12 from igordertigor/break/change-project-reading-interface
Maybe-Break/change project reading interface
2 parents 6150512 + 6f212b5 commit 812d82f

File tree

4 files changed

+95
-8
lines changed

4 files changed

+95
-8
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,22 @@ If you are inside a git repository, you can use semv to print the semantic versi
2525
v1.0.5 (no-eol)
2626

2727
Note that this will have not change anything about your repository. It is up to you to use the printed version. An example for using the printed version is given in semv's own [release workflow](https://github.com/igordertigor/semv/blob/master/.github/workflows/attempt-release.yml).
28+
29+
## Configuration
30+
31+
You can configure semv via the `pyproject.toml` config file. Here are the defaults:
32+
```toml
33+
[tool.semv]
34+
invalid_commit_action = "warning" # Could also be "error" or "skip"
35+
36+
[tool.types]
37+
feat = "minor"
38+
fix = "patch"
39+
perf = "patch"
40+
chore = "valid"
41+
test = "valid"
42+
docs = "valid"
43+
ci = "valid"
44+
refactor = "valid"
45+
style = "valid"
46+
```

src/semv/commands.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def version_string() -> Version:
2020
config = Config.parse(pyproject.read_text())
2121
else:
2222
config = Config()
23+
24+
if len(sys.argv) > 1 and sys.argv[1] == '--list-types':
25+
print(config.format_types())
26+
sys.exit(0)
27+
2328
vcs = Git()
2429
cp = AngularCommitParser(config.invalid_commit_action)
2530
vi = DefaultIncrementer(

src/semv/config.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from typing import Dict, Any, Optional, Set
2-
import os
1+
from typing import Dict, Any, Set, List
32
import tomli
43
from dataclasses import dataclass, field
54
from .types import InvalidCommitAction
65

76

87
@dataclass
98
class Config:
9+
commit_types_major: Set[str] = field(default_factory=set)
1010
commit_types_minor: Set[str] = field(default_factory=lambda: {'feat'})
1111
commit_types_patch: Set[str] = field(
1212
default_factory=lambda: {'fix', 'perf'}
@@ -26,12 +26,51 @@ class Config:
2626
@classmethod
2727
def parse(cls, text: str):
2828
cfg = parse_toml_section(text)
29-
for key in cfg:
30-
if key.startswith('commit_types'):
31-
cfg[key] = set(cfg[key])
32-
elif key == 'invalid_commit_action':
33-
cfg[key] = InvalidCommitAction(cfg[key])
34-
return cls(**cfg)
29+
if 'invalid_commit_action' in cfg:
30+
cfg['invalid_commit_action'] = InvalidCommitAction(
31+
cfg['invalid_commit_action']
32+
)
33+
if 'types' in cfg:
34+
types_cfg = cls._reorganize_types(cfg.pop('types'))
35+
else:
36+
types_cfg = {}
37+
38+
return cls(**{**types_cfg, **cfg})
39+
40+
@staticmethod
41+
def _reorganize_types(d: Dict[str, str]) -> Dict[str, Set[str]]:
42+
out: Dict[str, List[str]] = {
43+
'commit_types_major': [],
44+
'commit_types_minor': [],
45+
'commit_types_patch': [],
46+
'commit_types_skip': [],
47+
}
48+
for type, level in d.items():
49+
if level == 'valid':
50+
level = 'skip'
51+
out[f'commit_types_{level}'].append(type)
52+
return {key: set(types) for key, types in out.items()}
53+
54+
def format_types(self) -> str:
55+
out = []
56+
57+
def fmt(level: str, types: Set[str]) -> List[str]:
58+
t = ', '.join(sorted(list(types)))
59+
return [
60+
f'Implies {level} increment:',
61+
f' {t}',
62+
]
63+
64+
if self.commit_types_major:
65+
out.extend(fmt('major', self.commit_types_major))
66+
if self.commit_types_minor:
67+
out.extend(fmt('minor', self.commit_types_minor))
68+
if self.commit_types_patch:
69+
out.extend(fmt('patch', self.commit_types_patch))
70+
if self.commit_types_skip:
71+
out.extend(fmt('skip', self.commit_types_skip))
72+
out[-2] = 'Other valid types:'
73+
return '\n'.join(out)
3574

3675

3776
def parse_toml_section(s: str) -> Dict[str, Any]:

tests/cram/test_config2.t

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
$ semv --list-types
2+
Implies minor increment:
3+
feat
4+
Implies patch increment:
5+
fix, perf
6+
Other valid types:
7+
chore, ci, docs, refactor, style, test
8+
9+
$ echo "[tool.semv.types]" > pyproject.toml
10+
$ echo 'break = "major"' >> pyproject.toml
11+
$ echo 'feat = "minor"' >> pyproject.toml
12+
$ echo 'perf = "minor"' >> pyproject.toml
13+
$ echo 'fix = "patch"' >> pyproject.toml
14+
$ echo 'chore = "valid"' >> pyproject.toml
15+
$ echo 'docs = "valid"' >> pyproject.toml
16+
$ semv --list-types
17+
Implies major increment:
18+
break
19+
Implies minor increment:
20+
feat, perf
21+
Implies patch increment:
22+
fix
23+
Other valid types:
24+
chore, docs

0 commit comments

Comments
 (0)