|
1 | | -# from commitizen import bump |
| 1 | +from packaging.version import Version |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import questionary |
| 5 | +from commitizen import bump, git, config, out |
| 6 | + |
| 7 | +NO_COMMITS_FOUND = 3 |
| 8 | +NO_VERSION_SPECIFIED = 4 |
2 | 9 |
|
3 | 10 |
|
4 | 11 | class Bump:
|
5 | 12 | """Show prompt for the user to create a guided commit."""
|
6 | 13 |
|
7 | | - def __init__(self, config: dict): |
| 14 | + def __init__(self, config: dict, arguments: dict): |
8 | 15 | self.config: dict = config
|
9 | | - # self.cz = factory.commiter_factory(self.config) |
| 16 | + self.arguments: dict = arguments |
| 17 | + self.parameters: dict = { |
| 18 | + **config, |
| 19 | + **{ |
| 20 | + key: arguments[key] |
| 21 | + for key in ["dry_run", "tag_format", "prerelease", "increment"] |
| 22 | + if arguments[key] is not None |
| 23 | + }, |
| 24 | + } |
| 25 | + |
| 26 | + def __call__(self): |
| 27 | + """Steps executed to bump.""" |
| 28 | + try: |
| 29 | + current_version_instance: Version = Version(self.parameters["version"]) |
| 30 | + except TypeError: |
| 31 | + out.error("[NO_VERSION_SPECIFIED]") |
| 32 | + out.error("Check if current version is specified in config file, like:") |
| 33 | + out.error("version = 0.4.3") |
| 34 | + raise SystemExit(NO_VERSION_SPECIFIED) |
| 35 | + current_version: str = self.config["version"] |
| 36 | + tag_format: str = self.parameters["tag_format"] |
| 37 | + current_tag_version: str = bump.create_tag( |
| 38 | + current_version, tag_format=tag_format |
| 39 | + ) |
| 40 | + files: list = self.parameters["files"] |
| 41 | + dry_run: bool = self.parameters["dry_run"] |
| 42 | + |
| 43 | + prerelease: str = self.arguments["prerelease"] |
| 44 | + increment: Optional[str] = self.arguments["increment"] |
| 45 | + is_initial: bool = False |
| 46 | + |
| 47 | + # Check if reading the whole git tree up to HEAD is needed. |
| 48 | + if not git.tag_exist(current_tag_version): |
| 49 | + out.info(f"Tag {current_tag_version} could not be found. ") |
| 50 | + out.info( |
| 51 | + ( |
| 52 | + "Possible causes:\n" |
| 53 | + "- version in configuration is not the current version\n" |
| 54 | + "- tag_format is missing, check them using 'git tag --list'\n" |
| 55 | + ) |
| 56 | + ) |
| 57 | + is_initial = questionary.confirm("Is this the first tag created?").ask() |
| 58 | + |
| 59 | + commits = git.get_commits(current_tag_version, from_beginning=is_initial) |
| 60 | + |
| 61 | + # No commits, there is no need to create an empty tag. |
| 62 | + # Unless we previously had a prerelease. |
| 63 | + if not commits and not current_version_instance.is_prerelease: |
| 64 | + out.error("[NO_COMMITS_FOUND]") |
| 65 | + out.error("No new commits found.") |
| 66 | + raise SystemExit(NO_COMMITS_FOUND) |
| 67 | + |
| 68 | + if increment is None: |
| 69 | + increment = bump.find_increment(commits) |
| 70 | + |
| 71 | + # Increment is removed when current and next version |
| 72 | + # are expected to be prereleases. |
| 73 | + if prerelease and current_version_instance.is_prerelease: |
| 74 | + increment = None |
| 75 | + |
| 76 | + new_version = bump.generate_version( |
| 77 | + current_version, increment, prerelease=prerelease |
| 78 | + ) |
| 79 | + new_tag_version = bump.create_tag(new_version, tag_format=tag_format) |
| 80 | + message = f"Bump version {current_version} → {new_version}" |
| 81 | + |
| 82 | + # Report found information |
| 83 | + out.write(message) |
| 84 | + out.write(f"Tag to create: {new_tag_version}") |
| 85 | + out.write(f"Increment detected: {increment}") |
| 86 | + |
| 87 | + # Do not perform operations over files or git. |
| 88 | + if dry_run: |
| 89 | + raise SystemExit() |
10 | 90 |
|
11 | | - def __call__(self, *args, **kwargs): |
12 | | - # self.cz.run(*args, **kwargs) |
13 | | - print(*args, **kwargs) |
| 91 | + config.set_key("version", new_version.public) |
| 92 | + bump.update_version_in_files(current_version, new_version.public, files) |
| 93 | + git.commit(message, args="-a") |
| 94 | + git.tag(new_tag_version) |
| 95 | + out.success("Done!") |
0 commit comments