question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

provide an example for `commit-msg` hook

See original GitHub issue

Hello and thank you for creating and maintaining this project.

I’ve started using it very recently and I am having issues with setting up a hook that would allow me and my colleagues to verify Git commit messages (with regex).

Reading the documentation at pre-commit.com didn’t really help; I was able to install the hook and it’s contents is below:

#!/usr/bin/env python3.7
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03
import os
import sys

# we try our best, but the shebang of this script is difficult to determine:
# - macos doesn't ship with python3
# - windows executables are almost always `python.exe`
# therefore we continue to support python2 for this small script
if sys.version_info < (3, 3):
    from distutils.spawn import find_executable as which
else:
    from shutil import which

# work around https://github.com/Homebrew/homebrew-core/issues/30445
os.environ.pop('__PYVENV_LAUNCHER__', None)

# start templated
INSTALL_PYTHON = '/Users/ivica/PATH_TO/venv/bin/python3.7'
ARGS = ['hook-impl', '--config=.pre-commit-config.yaml', '--hook-type=commit-msg']
# end templated
ARGS.extend(('--hook-dir', os.path.realpath(os.path.dirname(__file__))))
ARGS.append('--')
ARGS.extend(sys.argv[1:])

DNE = '`pre-commit` not found.  Did you forget to activate your virtualenv?'
if os.access(INSTALL_PYTHON, os.X_OK):
    CMD = [INSTALL_PYTHON, '-mpre_commit']
elif which('pre-commit'):
    CMD = ['pre-commit']
else:
    raise SystemExit(DNE)

CMD.extend(ARGS)
if sys.platform == 'win32':  # https://bugs.python.org/issue19124
    import subprocess

    if sys.version_info < (3, 7):  # https://bugs.python.org/issue25942
        raise SystemExit(subprocess.Popen(CMD).wait())
    else:
        raise SystemExit(subprocess.call(CMD))
else:
    os.execvp(CMD[0], CMD)

What i’m missing from the docs is how to use this hook. As I said before, I’d like to verify that commit messages are in a correct format with some regex rules.

There is an existing hook for black configured like so

repos:
  - repo: https://github.com/psf/black
    rev: 20.8b1
    hooks:
      - id: black
        language_version: python3

and it is working well.

I am probably missing or misunderstanding something - i’m just not sure what.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MarcoGorellicommented, Oct 29, 2020

Good to see --negate come in handy 😄

          - id: validate-commit-msg
            name: validate-commit-msg
            language: pygrep
            entry: ^(CDD|CDTS)-[0-9]+\s-\s
            stages: [commit-msg]
            args: [--negate]
1reaction
asottilecommented, Oct 29, 2020

cool, you can do it even easier with pygrep – then you don’t need a script at all!

Read more comments on GitHub >

github_iconTop Results From Across the Web

commit-msg Hook - gerrit-review
The Gerrit Code Review supplied implementation of this hook is a short shell script which automatically inserts a globally unique Change-Id tag in...
Read more >
Get commit message in Git hook - Stack Overflow
commit -msg This hook is invoked by git commit, and can be bypassed with the --no-verify option. It takes a single parameter, the...
Read more >
Git Hooks | Atlassian Git Tutorial
Git Hooks are scripts that run automatically every time a particular event occurs in a Git repository. Learn what they do and how...
Read more >
Git commit-msg hook to validate for jira issue or the word merge
It auto inserts into the message Jira issues found in branch's name but not in the content otherwise behaves like yours. For example...
Read more >
Git Hooks - Git SCM
The commit-msg hook takes one parameter, which again is the path to a temporary file that contains the commit message written by the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found