Skip to content

Commit c03c398

Browse files
committed
Allow display of custom message when no-commit-to-branch fails
This change should make the failures of no-commit-to-branch easier for people that might encounter it as maintainers can provide information about allowed branch names. Current behavior just gives an opaque error that is not very useful for people that do not have a lot of knowledge about the project they are contributing to.
1 parent e437b7e commit c03c398

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ Protect specific branches from direct checkins.
167167
branches.
168168
- `-p` / `--pattern` can be used to protect branches that match a supplied regex
169169
(e.g. `--pattern, release/.*`). May be specified multiple times.
170+
- `-m` / `--message` can be used to customize the error message in case of failure.
170171

171172
Note that `no-commit-to-branch` is configured by default to [`always_run`](https://pre-commit.com/#config-always_run).
172173
As a result, it will ignore any setting of [`files`](https://pre-commit.com/#config-files),

pre_commit_hooks/no_commit_to_branch.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,20 @@ def main(argv: Sequence[str] | None = None) -> int:
3737
'may be specified multiple times'
3838
),
3939
)
40+
parser.add_argument(
41+
'-m', '--message', default='',
42+
help=(
43+
'What message to display if the the check fails'
44+
),
45+
)
4046
args = parser.parse_args(argv)
4147

4248
protected = frozenset(args.branch or ('master', 'main'))
4349
patterns = frozenset(args.pattern or ())
44-
return int(is_on_branch(protected, patterns))
50+
result = int(is_on_branch(protected, patterns))
51+
if result:
52+
print(args.message)
53+
return result
4554

4655

4756
if __name__ == '__main__':

0 commit comments

Comments
 (0)