#!/bin/sh # Git-Replace-Author : pre-receive hook # P532 - Sub-project # Made By: Ankit Sadana # Username: asadana # Made On: 12/17/2015 # Git Source Repository : git@github.com:asadana/Git-Check-Author.git # Last Edited By: Ankit Sadana # Username: asadana # Last Edited On: 1/13/2016 # ============================================================ # The "pre-receive" script is runis invoked by git-receive-pack on the remote repository, which happens when a git push is done on a local repository. # Just before starting to update refs on the remote repository, the pre-receive hook is invoked. Its exit status determines the success or failure of the update. # # This hook executes once for the receive operation. It takes no arguments, but for each ref to be updated it receives on standard input a line of the format: # # SP SP LF # # where is the old object name stored in the ref, # is the new object name to be stored in the ref # and is the full name of the ref. When creating a new ref, is 40 0. # # If the hook exits with non-zero status, none of the refs will be updated. If the hook exits with zero, updating of individual refs can still be prevented by the update hook. # # Both standard output and standard error output are forwarded to git send-pack on the other end, so you can simply echo messages for the user. # ============================================================ # Variable to store the file path of authorized names authorFile=~/.ssh/authorized_names # checkEmail function checks if the email address is registered in authorized_keys checkEmail() { # If condition checks if the authorEmail is available in the authorized_keys if [[ $(grep -w $authorEmail ~/.ssh/authorized_keys | wc -w) -gt 1 ]]; then # Uncomment this for debugging # echo "Author Email: $authorEmail" echo else echo "##########################################" echo "You are using an unauthorized email." echo "##########################################" echo "Expected commit author format: FirstName LastName " echo "Please configure your git user.email to IU-email used to generate the SSH-key." echo "You can fix your email using: git config user.email IU-ID@indiana.edu" echo "Then fix the last commit with: git commit --amend --no-edit --reset-author" echo "##########################################" exit 1 fi } # checkName function checks if authorEmail is in the authorized_names # and forces the listed users to use the corresponding name checkName() { # Retrieving the author name from SHA-1 (newsha) authorName=$(git log -1 --pretty=format:%an $newsha) # flag variable. Is raised to 1 when email address is found in authorFile flag=0 # If condition checks if the file exists if [[ -a $authorFile ]]; then # While loop reads the authorFile one line at a time while read -r line || [[ -n $line ]] do # IFS sets the delimeter used to split the line using the delimeter # The split up values are stored into email and name IFS=' ' read email name <<< "$line" # If condition checks email against authorEmail of the commit if [[ $authorEmail == $email ]]; then # Uncomment this for debugging # echo "Email found: $email" # expectedName stores the name from the authorFile expectedName=$name # flag is raised to 1, to let the script know email was found flag=1 # Break jumps out of the while loop # This prevents duplicate email entries to overwrite expectedName break fi # authorFile is fed to the while loop to be read done < $authorFile fi # Uncomment for debugging # echo "AuthorName: $expectedName" # echo "Flag: $flag" # If condition compares authorName against expectedName and if flag is 1 if [[ $authorName == $expectedName && $flag -eq 1 ]]; then # Uncomment this for debugging # echo "Author Name: $expectedName" exit 0 elif [[ $authorName != $expectedName && $flag -eq 1 ]]; then echo "##########################################" echo "You are using wrong name for commits." echo "Expected Name: $expectedName" echo "Name used: $authorName" echo "##########################################" echo "Expected commit author format: FirstName LastName " echo "Please configure your git user.name to your Full Name before proceeding" echo "You can fix your name using: git config user.name \"FirstName LastName\"" echo "Then fix the last commit with: git commit --amend --no-edit --reset-author" echo "##########################################" exit 1 # else condition is for people not in the authorFile else exit 0 fi } # While loop reads the input from given to pre-receive when it is triggered while read oldsha newsha refname; do # Retrieving the author email from SHA-1 (newsha) authorEmail=$(git log -1 --pretty=format:%ae $newsha) # Comment/Uncomment this for AUTHOR EMAIL check checkEmail # Comment/Uncomment this for AUTHOR NAME check checkName done