-
-
Notifications
You must be signed in to change notification settings - Fork 723
COMP: Python commit message update #5434
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| #!/usr/bin/env python3 | ||
| import os | ||
| import sys | ||
| import subprocess | ||
| import re | ||
|
|
||
| from pathlib import Path | ||
| import textwrap | ||
|
|
||
| DEFAULT_LINE_LENGTH: int = 78 | ||
|
|
||
|
|
||
| def die(message, commit_msg_path): | ||
| print("commit-msg hook failure", file=sys.stderr) | ||
| print("-----------------------", file=sys.stderr) | ||
| print(message, file=sys.stderr) | ||
| print("-----------------------", file=sys.stderr) | ||
| print( | ||
| f""" | ||
| To continue editing, run the command | ||
| git commit -e -F "{commit_msg_path}" | ||
| (assuming your working directory is at the top).""", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def get_max_length(): | ||
| try: | ||
| result = subprocess.run( | ||
| ["git", "config", "--get", "hooks.commit-msg.ITKCommitSubjectMaxLength"], | ||
| capture_output=True, | ||
| text=True, | ||
| check=True, | ||
| ) | ||
| except subprocess.CalledProcessError as _: | ||
| return DEFAULT_LINE_LENGTH | ||
|
|
||
|
|
||
| def main(): | ||
| git_dir_path: Path = Path(os.environ.get("GIT_DIR", ".git")).resolve() | ||
| commit_msg_path: Path = git_dir_path / "COMMIT_MSG" | ||
|
|
||
| if len(sys.argv) < 2: | ||
| die(f"Usage: {sys.argv[0]} <git_commit_message_file>", commit_msg_path) | ||
|
|
||
| input_file: Path = Path(sys.argv[1]) | ||
| if not input_file.exists(): | ||
| die( | ||
| f"Missing input_file {sys.argv[1]} for {sys.argv[0]} processing", | ||
| commit_msg_path, | ||
| ) | ||
| max_subjectline_length: int = get_max_length() | ||
|
|
||
| original_input_file_lines: list[str] = [] | ||
| with open(input_file) as f_in: | ||
| original_input_file_lines = f_in.readlines() | ||
|
|
||
| input_file_lines: list[str] = [] | ||
| for test_line in original_input_file_lines: | ||
| test_line = test_line.strip() | ||
| is_empty_line_before_subject: bool = ( | ||
| len(input_file_lines) == 0 and len(test_line) == 0 | ||
| ) | ||
| if test_line.startswith("#") or is_empty_line_before_subject: | ||
| continue | ||
| input_file_lines.append(f"{test_line}\n") | ||
|
|
||
| with open(commit_msg_path, "w") as f_out: | ||
| f_out.writelines(input_file_lines) | ||
|
|
||
| subject_line: str = input_file_lines[0] | ||
|
|
||
| if len(subject_line) < 8: | ||
| die( | ||
| f"The first line must be at least 8 characters:\n--------\n{subject_line}\n--------", | ||
| commit_msg_path, | ||
| ) | ||
| if ( | ||
| len(subject_line) > max_subjectline_length | ||
| and not subject_line.startswith("Merge ") | ||
| and not subject_line.startswith("Revert ") | ||
| ): | ||
| die( | ||
| f"The first line may be at most {max_subjectline_length} characters:\n" | ||
| + "-" * max_subjectline_length | ||
| + f"\n{subject_line}\n" | ||
| + "-" * max_subjectline_length, | ||
| commit_msg_path, | ||
| ) | ||
| if re.match(r"^[ \t]|[ \t]$", subject_line): | ||
| die( | ||
| f"The first line may not have leading or trailing space:\n[{subject_line}]", | ||
| commit_msg_path, | ||
| ) | ||
| if not re.match( | ||
| r"^(Merge|Revert|BUG:|COMP:|DOC:|ENH:|PERF:|STYLE:|WIP:)\s", subject_line | ||
| ): | ||
| die( | ||
| f"""Start ITK commit messages with a standard prefix (and a space): | ||
| BUG: - fix for runtime crash or incorrect result | ||
| COMP: - compiler error or warning fix | ||
| DOC: - documentation change | ||
| ENH: - new functionality | ||
| PERF: - performance improvement | ||
| STYLE: - no logic impact (indentation, comments) | ||
| WIP: - Work In Progress not ready for merge | ||
| To reference GitHub issue XXXX, add "Issue #XXXX" to the commit message. | ||
| If the issue addresses an open issue, add "Closes #XXXX" to the message.""", | ||
| commit_msg_path, | ||
| ) | ||
| if re.match(r"^BUG: [0-9]+\.", subject_line): | ||
| die( | ||
| f'Do not put a "." after the bug number:\n\n {subject_line}', | ||
| commit_msg_path, | ||
| ) | ||
| del subject_line | ||
|
|
||
hjmjohnson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if len(input_file_lines) > 1: | ||
| second_line: str = input_file_lines[ | ||
| 1 | ||
| ].strip() # Remove whitespace at begining and end | ||
| if len(second_line) == 0: | ||
| input_file_lines[1] = "\n" # Replace line with only newline | ||
| else: | ||
| die( | ||
| f'The second line of the commit message must be empty:\n"{second_line}" with length {len(second_line)}', | ||
| commit_msg_path, | ||
| ) | ||
| del second_line | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.