Skip to content

Allow sumi to check if user email matches a given regex #404

Description

@marovira

Feature Request

Summary

The idea is to provide a new entry into the config that allows the user to set an email regex that all commits must satisfy.

Motivation

There's a couple of reasons why this feature is extremely useful:

  • Consistency: at my day job, our gitlab instance isn't particularly well coordinated in terms of the emails that users use, which makes it hard to track who is making what changes and finding them in the company registry. By enforcing that all emails must come from a certain domain, it makes it much simpler to track who's making changes.
  • Less mistakes: as part of my job, I sometimes contribute to open source projects using my GitHub account. As a result, my user config uses my GitHub email. This causes issues when I have to work on our own private repos as I sometimes forget to run git config user.email "me@work_email.com" causing me to have to back-out commits or (in extreme cases) have to run git filter-repo to change the email address. By having sumi be able to ensure the email comes from my work address, it would prevent this from ever happening.

A side-motivation is that I'm trying to explore alternatives to gitlint, which is what we're currently using. The project is great, but hasn't been updated in over 3 years and has some features that I need (such as checking for imperative mode). Sumi is in a perfect position to replace gitlint since it almost covers all my needs and is also a standalone executable that I can just install to simplify development in certain cases.

Detailed Description

There are two sides to implementing this feature, so I'll describe how each of these could be done separately.

The Config

This one is easy. Reading through the code, it would just be a matter of cloning Config::header_pattern and renaming it as user_email_pattern or something similar. As the two are effectively the same (just regexes) the existing code and tests that cover header_pattern can just be duplicated to cover the new entry. In principle the code could be refactored to merge these two together and cover them with the same code instead of duplicating, but the simplest option is to just duplicate the code.

The Check

To implement the check, we first need a way to obtain the user email address. I can think of two ways of accomplishing this (with varying levels of difficulty):

  1. Add a function similar to get_git_commentchar that runs git config --get user.email to grab the email address from the current git config.
  2. A more complex version of this would involve peeking into the git log and parsing the information from there, similar to what gitlint is doing here.

Version 1 is the easiest but has the drawback that it depends exclusively on the user.email value being set. This is (probably) true in local environments, but it is almost certainly not true in CI. Since you've recently added the ability to check ranges of commits, this could be problematic. Version 2 is the most robust as it allows extracting the email address directly from the log, negating the need for user.email to be set.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions