danger-mailmap
A Danger plugin to check if .mailmap has a canonical name of author and committer.
Installation
gem install danger-mailmapOr write the following code in your Gemfile.
gem 'danger-mailmap'Usage
The easiest way to use is just add this to your Dangerfile:
mailmap.checkIf your repository has a mailmap file located in the place other than .mailmap, you can pass the path as argument.
mailmap.check '/path/to/mailmap'If you want danger-mailmap to ignore a particular user regardless of mailmap, set allowed_patterns.
mailmap.allowed_patterns = [
/.+@(users\.noreply\.)?github\.com/,
'good@example.com'
]
mailimap.checkWhat is mailmap?
mailmap is a file that maps Git author and committer names and/or email addresses.
See man 5 gitmailmap or https://git-scm.com/docs/gitmailmap for the detail.
How to fix warnings
If you encountered warnings like email@example.com is not included in .mailmap, basically you have 4 options.
- Rewrite author and/or committer of each commit in the pull request.
- Add new entry to mailmap.
- Add new allow-list entry to Dangerfile.
- Do nothing and remain everything as-is.
If you don't want to continue using email@example.com, 1. is the most preferable option.
See How to rewrite author and/or committers section.
If it is the first time for you to contribute to the repository, you may want to choose the option 2..
Just add Your Name <email@example.com> to the mailmap file and commit it.
If email@example.com is an email address of a bot user and it can vary, you can add it to allow-list.
For example, renovate bot has variable email like 29139614+renovate[bot]@users.noreply.github.com.
See Usage section to know how to use mailmap.allowed_patterns.
Lastly, when you clearly know what you are doing or you have your teammates' permission, the option 4. is obviously the most easiest way.
How to avoid making commits with unintended author/committer email
You need to tell Git to use the correct name and email like the following:
git config --global user.email 'correct@example.com'
git config --global user.name 'Correct Name'If you have multiple names or emails and changes them by repository, you may want to set name and email to the specific repository.
Use --global option instead of --local in this case.
git config --local user.email 'correct@example.com'
git config --local user.name 'Correct Name'How to rewrite author and/or committers
You can rewrite existing commits' author and/or committer in a pull request with git filter-branch command.
Let's say that you made a pull request with wip branch based on master branch and danger-mailmap complains old@example.com is not included in mailmap. You want to change old@example.com to new@example.com with the proper name New One.
In this case, the following script works well.
git filter-branch --env-filter '
if [ "$GIT_AUTHOR_EMAIL" = "old@example.com" ]; then
GIT_AUTHOR_EMAIL="new@example.com"
GIT_AUTHOR_NAME="New One"
fi
if [ "$GIT_COMMITTER_EMAIL" = "old@example.com" ]; then
GIT_COMMITTER_EMAIL="new@example.com"
GIT_COMMITTER_NAME="New One"
fi
' --tag-name-filter cat master...wipPerhaps you may want to undo the changes.
Don't worry, git filter-branch automatically backups the original history.
The following command rollbacks the previous changes.
git reset --hard original/refs/heads/wipBy default, git filter-branch does not run when a backup for the current branch exists.
You can delete it by running:
git update-ref -d refs/original/refs/heads/wipOr just pass --force option to the next git filter-branch command to overwrite existing backups.
See the official document for other examples of git filter-branch.
Development
- Clone this repo
- Run
bundle installto setup dependencies. - Run
bundle exec rake specto run the tests. - Use
bundle exec guardto automatically have tests run as you make changes. - Make your changes.
