Show prompt for the user to create a guided commit.
| 46 | |
| 47 | |
| 48 | class Commit: |
| 49 | """Show prompt for the user to create a guided commit.""" |
| 50 | |
| 51 | def __init__(self, config: BaseConfig, arguments: CommitArgs) -> None: |
| 52 | if not git.is_git_project(): |
| 53 | raise NotAGitProjectError() |
| 54 | |
| 55 | self.config: BaseConfig = config |
| 56 | self.cz = factory.committer_factory(self.config) |
| 57 | self.arguments = arguments |
| 58 | self.backup_file_path = get_backup_file_path() |
| 59 | |
| 60 | def _get_commit_encoding(self) -> str: |
| 61 | """Return the encoding of the files that hold the commit message. |
| 62 | |
| 63 | `commit_encoding` falls back to `encoding` when it is not set. |
| 64 | """ |
| 65 | return ( |
| 66 | self.config.settings["commit_encoding"] or self.config.settings["encoding"] |
| 67 | ) |
| 68 | |
| 69 | def _read_backup_message(self) -> str | None: |
| 70 | # Check the commit backup file exists |
| 71 | if not self.backup_file_path.is_file(): |
| 72 | return None |
| 73 | |
| 74 | # Read commit message from backup |
| 75 | return self.backup_file_path.read_text( |
| 76 | encoding=self._get_commit_encoding() |
| 77 | ).strip() |
| 78 | |
| 79 | def _get_message_by_prompt_commit_questions(self) -> str: |
| 80 | # Prompt user for the commit message |
| 81 | questions = self.cz.questions() |
| 82 | for question in (q for q in questions if q["type"] == "list"): |
| 83 | question["use_shortcuts"] = self.config.settings["use_shortcuts"] |
| 84 | try: |
| 85 | answers = questionary.prompt(questions, style=self.cz.style) |
| 86 | except ValueError as err: |
| 87 | root_err = err.__context__ |
| 88 | if isinstance(root_err, CzException): |
| 89 | raise CustomError(str(root_err)) |
| 90 | raise err |
| 91 | |
| 92 | if not answers: |
| 93 | raise NoAnswersError() |
| 94 | |
| 95 | message = self.cz.message(answers) |
| 96 | self._validate_subject_length(message) |
| 97 | message = self._wrap_body(message) |
| 98 | return message |
| 99 | |
| 100 | def _validate_subject_length(self, message: str) -> None: |
| 101 | message_length_limit = self.arguments.get( |
| 102 | "message_length_limit", self.config.settings.get("message_length_limit", 0) |
| 103 | ) |
| 104 | # By the contract, message_length_limit is set to 0 for no limit |
| 105 | if ( |
nothing calls this directly
no outgoing calls
no test coverage detected