-
Notifications
You must be signed in to change notification settings - Fork 3
[fact] Refactor SanitizeRepo.command #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -19,14 +19,15 @@ | |||||
| _syntax, | ||||||
| _format, | ||||||
| _gitutils, | ||||||
| sanitizer, | ||||||
| ) | ||||||
|
|
||||||
| PLUGIN_NAME = "sanitizer" | ||||||
|
|
||||||
| LOGGER = daiquiri.getLogger(__file__) | ||||||
|
|
||||||
|
|
||||||
| def check_repo_state(repo_root) -> Optional[str]: | ||||||
| def check_repo_state(repo_root, ignore_commit_errors) -> Optional[str]: | ||||||
| try: | ||||||
| repo = git.Repo(repo_root) | ||||||
| except git.InvalidGitRepositoryError as exc: | ||||||
|
|
@@ -42,7 +43,8 @@ def check_repo_state(repo_root) -> Optional[str]: | |||||
| if repo.index.diff(None): | ||||||
| message = "There are uncommitted unstaged files in the repo" | ||||||
|
|
||||||
| return message + help_message if message else None | ||||||
| if message and not ignore_commit_errors: | ||||||
| raise sanitizer.SanitizeError(msg=message + help_message) | ||||||
|
|
||||||
|
|
||||||
| def discover_dirty_files( | ||||||
|
|
@@ -64,7 +66,7 @@ def discover_dirty_files( | |||||
|
|
||||||
| def sanitize_files( | ||||||
| basedir: pathlib.Path, file_relpaths: List[_fileutils.RelativePath] | ||||||
| ) -> List[_format.FileWithErrors]: | ||||||
| ): | ||||||
|
||||||
| ): | |
| ) -> None: |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ): | |
| ) -> None: |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,13 @@ | |||||||||||||||
| PLUGIN_NAME = "sanitizer" | ||||||||||||||||
| LOGGER = daiquiri.getLogger(__file__) | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| class SanitizeError(plug.PlugError): | ||||||||||||||||
| def __init__(self, msg: str, cause: Optional[Exception] = None): | ||||||||||||||||
| self.msg = msg | ||||||||||||||||
| super().__init__(msg, cause) | ||||||||||||||||
|
||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| sanitize_category = plug.cli.category( | ||||||||||||||||
| "sanitize", | ||||||||||||||||
| action_names=["repo", "file"], | ||||||||||||||||
|
|
@@ -45,8 +52,8 @@ class SanitizeRepo(plug.Plugin, plug.cli.Command): | |||||||||||||||
| repo_root = plug.cli.option( | ||||||||||||||||
| short_name="-r", | ||||||||||||||||
| help="manually provide path to the root of git repository", | ||||||||||||||||
| converter=pathlib.Path, | ||||||||||||||||
| default=pathlib.Path("."), | ||||||||||||||||
| converter=lambda s: pathlib.Path(s).resolve(strict=True), | ||||||||||||||||
| default=".", | ||||||||||||||||
| ) | ||||||||||||||||
| commit_message = plug.cli.option( | ||||||||||||||||
| short_name="-m", default="Update task template", configurable=True | ||||||||||||||||
|
|
@@ -72,79 +79,71 @@ class SanitizeRepo(plug.Plugin, plug.cli.Command): | |||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| def command(self) -> Optional[plug.Result]: | ||||||||||||||||
| repo_root = self.repo_root.absolute() | ||||||||||||||||
|
|
||||||||||||||||
| input_error = self._validate_input(repo_root) | ||||||||||||||||
| if input_error: | ||||||||||||||||
| return input_error | ||||||||||||||||
|
|
||||||||||||||||
| if self.no_commit: | ||||||||||||||||
| LOGGER.info("Executing dry run") | ||||||||||||||||
| file_relpaths = _sanitize_repo.discover_dirty_files(repo_root) | ||||||||||||||||
| errors = _sanitize_repo.sanitize_files(repo_root, file_relpaths) | ||||||||||||||||
| else: | ||||||||||||||||
| LOGGER.info(f"Sanitizing repo and updating {self.target_branch}") | ||||||||||||||||
| effective_target_branch = self._resolve_effective_target_branch( | ||||||||||||||||
| repo_root | ||||||||||||||||
| try: | ||||||||||||||||
| self._validate_input() | ||||||||||||||||
| result_message = ( | ||||||||||||||||
| self._sanitize_no_commit() | ||||||||||||||||
| if self.no_commit | ||||||||||||||||
| else self._sanitize_to_target_branch() | ||||||||||||||||
| ) | ||||||||||||||||
| try: | ||||||||||||||||
| errors = _sanitize_repo.sanitize_to_target_branch( | ||||||||||||||||
| repo_root, effective_target_branch, self.commit_message | ||||||||||||||||
| ) | ||||||||||||||||
| except _gitutils.EmptyCommitError: | ||||||||||||||||
| return plug.Result( | ||||||||||||||||
| name="sanitize-repo", | ||||||||||||||||
| msg="No diff between target branch and sanitized output. " | ||||||||||||||||
| f"No changes will be made to branch: {self.target_branch}", | ||||||||||||||||
| status=plug.Status.WARNING, | ||||||||||||||||
| ) | ||||||||||||||||
| status = plug.Status.SUCCESS | ||||||||||||||||
| except SanitizeError as err: | ||||||||||||||||
| result_message = err.msg | ||||||||||||||||
| status = plug.Status.ERROR | ||||||||||||||||
|
||||||||||||||||
| status = plug.Status.SUCCESS | |
| except SanitizeError as err: | |
| result_message = err.msg | |
| status = plug.Status.ERROR | |
| return plug.Result(name="sanitize-repo", msg=result_message, status=plug.Status.SUCCESS) | |
| except SanitizeError as err: | |
| return plug.Result(name="sanitize-repo", msg=result_message, status=plug.Status.ERROR) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I felt one return statement was clearner. But when you think of them as completely different i think youre right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This introduces a circular import.
sanitizerimports_sanitize_repo, and vice versa. That's in general not a good sign, and can sometimes cause the program to not be able to execute at all (infinite import loop).