-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
Added prepare-commit-msg hook to check commit prefixes. #20381
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: main
Are you sure you want to change the base?
Changes from all 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,8 +19,8 @@ REV=$1 | |
|
|
||
| TMPFILE=tmplog.tmp | ||
|
|
||
| # Cherry-pick the commit | ||
| git cherry-pick ${REV} | ||
| # Cherry-pick the commit, but skip hooks | ||
| git -c core.hooksPath=/dev/null cherry-pick ${REV} | ||
|
Comment on lines
+22
to
+23
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://adamj.eu/tech/2023/02/13/git-skip-hooks/#skip-unskippable-hooks-with-core-hookspath The hooks will run below, when
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It occurs to me this isn't windows-friendly, but we can cross that bridge later. Perhaps we would want to just replace the backport script entirely with a fleshed out prepare-commit-msg hook. |
||
|
|
||
| # Create new log message by modifying the old one | ||
| git log --pretty=format:"[${BRANCH_NAME}] %s%n%n%b%nBackport of ${REV} from main." HEAD^..HEAD \ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #! /usr/bin/env python | ||
|
|
||
| # Install this prepare-commit-msg hook with | ||
| # pre-commit install --hook-type prepare-commit-msg | ||
|
Comment on lines
+3
to
+4
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's the install step. |
||
|
|
||
| import subprocess | ||
| import sys | ||
|
|
||
| with open(sys.argv[1], "r", encoding="utf-8") as f: | ||
| first_line = f.readline() | ||
|
|
||
| commit_msg_text = subprocess.run( | ||
| ["git", "branch", "--show-current"], | ||
| capture_output=True, | ||
| ).stdout.decode(encoding="utf-8") | ||
|
|
||
| stripped_branch_name = commit_msg_text.strip("stable/").strip() | ||
| if stripped_branch_name and stripped_branch_name[0].isnumeric(): | ||
| expected_prefix = f"[{stripped_branch_name}] " | ||
| assert first_line.startswith( | ||
| expected_prefix | ||
| ), f"Expected {first_line!r} to start with {expected_prefix!r}" | ||
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 shows what we want -- I normally want the hook to catch naive uses of git cherry-pick, so I have to skip the hook here (where a naive cherry-pick is done intentionally).