Restrict Commit Emails to specific domains


Platform Notice: Cloud - This article applies to Atlassian products on the cloud platform.

Summary

Currently, there is no option to restrict users from a different domain from pushing changes to the Bitbucket Cloud repository. There is a feature request to implement a server-side "pre-receive" hook, which can help enforce specific commit policies.

Solution

The following is a workaround to a server-side "pre-receive" hook.

  • Create a directory called ".githooks" inside your repository folder from the terminal/git bash. In this directory, put your hooks "pre-commit" file with this name, which is a shell script. 
  • Commit this to your repository
  • Access the repository directory and run the following command. 

    git config core.hooksPath  <path_to_.gitHooks>
  • Now, when a user makes a commit with an email that does not match the domain in the .githooks file, the commits will not be recorded. 

  • To implement this across the team, repository members need to clone this repository or pull the changes that contain the ".githooks" folder and then run the git config command referenced above. 

Example:

% git add file.sh 
% git commit -m "TestPreCommitHook"
You are using a NON Company domain as your email. This commit will not be recorded. Please change it and then commit again
% git log --oneline -1
******** (HEAD -> master) Test)_1
% git commit -m "TestPreCommitHook"
You are using company domain as your email, proceeding with committing
[master *****] TestPreCommitHook
 1 file changed, 3 insertions(+), 2 deletions(-)
% git log --oneline -1             
******* (HEAD -> master) TestPreCommitHook


Pre-commit hook-script

  •  If the "git config with "user.email" does not match your organization's domain then it will not allow the commit

    #!/bin/sh
    domain="company_email.com"
    
    email="$(git config --list | grep user.email | awk -F'@' '{print $2}')"
    if [[ "$email" == "$domain" ]]; then
      echo "You are using company domain as your email, proceeding with committing"
      exit 0
    else
      echo "You are using a NON-Company domain as your email. This commit will not be recorded. Please change it and then commit again"
      exit 1
    fi
  • If you're getting the following message instead of the message above, provide permission to the file and then try to commit again

    hint: The '.gitHooks/pre-commit' hook was ignored because it's not set as executable.
    hint: You can disable this warning with `git config advice.ignoredHook false`.
    chmod +x .gitHooks/pre-commit 




Last modified on Feb 8, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.