def test_get_tags(mocker): tag_str = ( "v1.0.0---inner_delimiter---333---inner_delimiter---2020-01-20\n" "v0.5.0---inner_delimiter---222---inner_delimiter---2020-01-17\n" "v0.0.1---inner_delimiter---111---inner_delimiter---2020-01-17\n") mocker.patch("commitizen.cmd.run", return_value=FakeCommand(out=tag_str)) git_tags = git.get_tags() latest_git_tag = git_tags[0] assert latest_git_tag.rev == "333" assert latest_git_tag.name == "v1.0.0" assert latest_git_tag.date == "2020-01-20" mocker.patch("commitizen.cmd.run", return_value=FakeCommand(out="", err="No tag available")) assert git.get_tags() == []
def __call__(self): commit_parser = self.cz.commit_parser changelog_pattern = self.cz.changelog_pattern start_rev = self.start_rev unreleased_version = self.unreleased_version changelog_meta: Dict = {} if not changelog_pattern or not commit_parser: out.error( f"'{self.config.settings['name']}' rule does not support changelog" ) raise SystemExit(NO_PATTERN_MAP) tags = git.get_tags() if not tags: tags = [] if self.incremental: changelog_meta = changelog.get_metadata(self.file_name) latest_version = changelog_meta.get("latest_version") if latest_version: start_rev = self._find_incremental_rev(latest_version, tags) commits = git.get_commits(start=start_rev, args="--author-date-order") if not commits: out.error("No commits found") raise SystemExit(NO_COMMITS_FOUND) tree = changelog.generate_tree_from_commits(commits, tags, commit_parser, changelog_pattern, unreleased_version) changelog_out = changelog.render_changelog(tree) if self.dry_run: out.write(changelog_out) raise SystemExit(0) lines = [] if self.incremental and os.path.isfile(self.file_name): with open(self.file_name, "r") as changelog_file: lines = changelog_file.readlines() with open(self.file_name, "w") as changelog_file: if self.incremental: new_lines = changelog.incremental_build( changelog_out, lines, changelog_meta) changelog_file.writelines(new_lines) else: changelog_file.write(changelog_out)
def __call__(self): commit_parser = self.cz.commit_parser changelog_pattern = self.cz.changelog_pattern start_rev = self.start_rev unreleased_version = self.unreleased_version changelog_meta: Dict = {} change_type_map: Optional[Dict] = self.change_type_map changelog_message_builder_hook: Optional[ Callable] = self.cz.changelog_message_builder_hook changelog_hook: Optional[Callable] = self.cz.changelog_hook if not changelog_pattern or not commit_parser: out.error( f"'{self.config.settings['name']}' rule does not support changelog" ) raise SystemExit(NO_PATTERN_MAP) tags = git.get_tags() if not tags: tags = [] if self.incremental: changelog_meta = changelog.get_metadata(self.file_name) latest_version = changelog_meta.get("latest_version") if latest_version: start_rev = self._find_incremental_rev(latest_version, tags) commits = git.get_commits(start=start_rev, args="--author-date-order") if not commits: out.error("No commits found") raise SystemExit(NO_COMMITS_FOUND) tree = changelog.generate_tree_from_commits( commits, tags, commit_parser, changelog_pattern, unreleased_version, change_type_map=change_type_map, changelog_message_builder_hook=changelog_message_builder_hook, ) changelog_out = changelog.render_changelog(tree) changelog_out = changelog_out.lstrip("\n") if self.dry_run: out.write(changelog_out) raise SystemExit(0) lines = [] if self.incremental and os.path.isfile(self.file_name): with open(self.file_name, "r") as changelog_file: lines = changelog_file.readlines() with open(self.file_name, "w") as changelog_file: partial_changelog: Optional[str] = None if self.incremental: new_lines = changelog.incremental_build( changelog_out, lines, changelog_meta) changelog_out = "".join(new_lines) partial_changelog = changelog_out if changelog_hook: changelog_out = changelog_hook(changelog_out, partial_changelog) changelog_file.write(changelog_out)
def __call__( self, chlog_path, latest_version, start_rev=None, incremental=False, dry_run=False, change_type_map=None, ): """ :param start_rev: If None, changelog from beginning """ # THE FOLLOWING CODE DOESN'T HAVE SIDE EFFECTS TO FILESYS OR GIT: commit_parser = self.cz.commit_parser changelog_pattern = self.cz.changelog_pattern changelog_meta = {} changelog_message_builder_hook = self.cz.changelog_message_builder_hook changelog_hook = self.cz.changelog_hook if not changelog_pattern or not commit_parser: raise NoPatternMapError( f"'{self.commiter_name}' rule doesn't support changelog") # tags = git.get_tags() if not tags: tags = [] # if incremental: changelog_meta = changelog.get_metadata(chlog_path) latest_version = changelog_meta.get("latest_version") if latest_version: start_rev = self._find_incremental_rev(latest_version, tags) # commits = git.get_commits(start=start_rev, args="--author-date-order") if not commits: raise NoCommitsFoundError("No commits found") # tree = changelog.generate_tree_from_commits( commits, tags, commit_parser, changelog_pattern, latest_version, change_type_map=change_type_map, changelog_message_builder_hook=changelog_message_builder_hook, ) changelog_out = changelog.render_changelog(tree) changelog_out = changelog_out.lstrip("\n") # if dry_run: out.write(changelog_out) raise DryRunExit() # # CHANGES TO FILESYSTEM: WRITE TO CHLOG_PATH (AFTER READING) lines = [] if incremental and os.path.isfile(chlog_path): with open(chlog_path, "r") as changelog_file: lines = changelog_file.readlines() # with open(chlog_path, "w") as changelog_file: partial_changelog = None if incremental: new_lines = changelog.incremental_build( changelog_out, lines, changelog_meta) changelog_out = "".join(new_lines) partial_changelog = changelog_out if changelog_hook: changelog_out = changelog_hook(changelog_out, partial_changelog) changelog_file.write(changelog_out) out.write(f"Wrote changelog to {chlog_path}!\n")
# parser.add_argument( # "-w", # "--first_version", # type=str, # required=True, # help="First chlog version", # ) parser.add_argument( "--dry_run", action="store_true", help="If given, the command will print but not change anything.", ) args = parser.parse_args() # OUT_PATH = args.out_path LAST_VERSION = args.last_version # FIRST_VERSION = args.first_version # INCREMENTAL = args.incremental DRY_RUN = args.dry_run # if LAST_VERSION is None: gtags = git.get_tags() assert gtags, "Changelog not possible because no git tags found!" sorted_versions = sorted((t.name for t in gtags), key=lambda elt: Version(elt)) LAST_VERSION = sorted_versions[-1] # chlogger = ConfiglessChangelog() chlogger(OUT_PATH, LAST_VERSION, dry_run=DRY_RUN)
def __call__(self): commit_parser = self.cz.commit_parser changelog_pattern = self.cz.changelog_pattern start_rev = self.start_rev unreleased_version = self.unreleased_version changelog_meta: Dict = {} change_type_map: Optional[Dict] = self.change_type_map changelog_message_builder_hook: Optional[ Callable ] = self.cz.changelog_message_builder_hook if not changelog_pattern or not commit_parser: raise NoPatternMapError( f"'{self.config.settings['name']}' rule does not support changelog" ) if self.incremental and self.rev_range: raise NotAllowed("--incremental cannot be combined with a rev_range") tags = git.get_tags() if not tags: tags = [] end_rev = "" if self.incremental: changelog_meta = changelog.get_metadata(self.file_name) latest_version = changelog_meta.get("latest_version") if latest_version: start_rev = self._find_incremental_rev(latest_version, tags) if self.rev_range and self.tag_format: start_rev, end_rev = changelog.get_oldest_and_newest_rev( tags, version=self.rev_range, tag_format=self.tag_format, ) commits = git.get_commits( start=start_rev, end=end_rev, args="--author-date-order" ) if not commits: raise NoCommitsFoundError("No commits found") tree = changelog.generate_tree_from_commits( commits, tags, commit_parser, changelog_pattern, unreleased_version, change_type_map=change_type_map, changelog_message_builder_hook=changelog_message_builder_hook, ) if self.change_type_order: tree = changelog.order_changelog_tree(tree, self.change_type_order) changelog_out = changelog.render_changelog(tree) changelog_out = changelog_out.lstrip("\n") if self.dry_run: out.write(changelog_out) raise DryRunExit() lines = [] if self.incremental and os.path.isfile(self.file_name): with open(self.file_name, "r") as changelog_file: lines = changelog_file.readlines() self.write_changelog(changelog_out, lines, changelog_meta)