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 write_changelog( self, changelog_out: str, lines: List[str], changelog_meta: Dict ): if not isinstance(self.file_name, str): raise NotAllowed( "Changelog file name is broken.\n" "Check the flag `--file-name` in the terminal " f"or the setting `changelog_file` in {self.config.path}" ) changelog_hook: Optional[Callable] = self.cz.changelog_hook 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): 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")