Skip to content

Instantly share code, notes, and snippets.

@jumanjiman
Created July 11, 2018 13:17
Show Gist options
  • Save jumanjiman/75616ec91f115abba0cde6f47bea2cb4 to your computer and use it in GitHub Desktop.
Save jumanjiman/75616ec91f115abba0cde6f47bea2cb4 to your computer and use it in GitHub Desktop.
pre-commit hook to check mailmap
---
# Configure various overrides and exceptions in this file.
# See https://pre-commit.com/ for details.
fail_fast: false
repos:
- repo: local
hooks:
- id: check-mailmap
name: Detect if an email address needs to be added to mailmap
language: script
entry: ./check-mailmap
always_run: true
exclude: .*
#!/bin/sh
if [ -n "${DEBUG}" ]; then
set -x
fi
# Global return code.
RC=0
main() {
check_email
}
check_email() {
# Check if any email address is associated with more than one name.
#
# Example:
# John Doe <jdoe@example.com>
# jdoe <jdoe@example.com>
#
# The above example can be fixed by adding these two lines to .mailmap file:
# John Doe <jdoe@example.com> John Doe <jdoe@example.com>
# John Doe <jdoe@example.com> jdoe <jdoe@example.com>
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
# How we want it to appear How it appears in git commit
#
# See git-shortlog(1) for more details.
output="$(! 2>&1 git log --use-mailmap --pretty='%aN %aE' | tr '[:upper:]' '[:lower:]' | sort -u | awk '{print $NF}' | sort | uniq -c | sed -e 's/^[ \t]*//' | grep -v '^1[[:space:]]' | awk '{print $NF}')"
if [ -n "${output}" ]; then
RC=1
echo 'The following email addresses are associated with more than one name:'
echo
for email in ${output}; do
echo " ${email}"
done
echo
echo 'The associations include:'
for email in ${output}; do
echo
git log --pretty='%aN <%aE>' --regexp-ignore-case --author="${email}" | sort | uniq -c
done
fi
}
main
exit ${RC}
@jumanjiman
Copy link
Author

jumanjiman commented Jul 11, 2018

for a good condition

$ pre-commit run check-mailmap --all-files --verbose
[check-mailmap] Detect if an email address needs to be added to mailmap.......................Passed

for a bad condition

$ pre-commit run check-mailmap --all-files --verbose
[check-mailmap] Detect if an email address needs to be added to mailmap.......................Failed
hookid: check-mailmap

The following email addresses are associated with more than one name:

        jdoe@example.com
        billy.bob@example.com

The associations include:

     13 John Doe <jdoe@example.com>
      4 jdoe <jdoe@example.com>

      2 Billy Bob <billy.bob@example.com>
      2 Bubba <billy.bob@example.com>

@jumanjiman
Copy link
Author

I've added this hook to https://github.com/jumanjihouse/pre-commit-hooks#check-mailmap

Update .pre-commit-config.yaml to include:

---
- repo: https://github.com/jumanjihouse/pre-commit-hooks
      sha: 1.8.0
      hooks:
        - id: check-mailmap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment