User Tools

Site Tools


git_hooks

This is an old revision of the document!


Git Hooks

pre-receive Hook (Server-Side)

This is a hook i use along with Gitlab. Its global and will be triggerd for all repositories when data ist send to the server. For Gitlab Omnibus installations place 'pre-receive' file in this Folder (if it does not exist, create it)

/opt/gitlab/embedded/service/gitlab-shell/hooks/pre-receive.d/

Make sure the file ist executable using 'chmod +x pre-receive'

pre-receive
#!/bin/sh
 
read oldrev newrev refname wusel
 
merges=`git log --oneline --merges $oldrev..$newrev`
 
readarray -t COMMIT <<< "$merges"
 
if [ -z "$COMMIT" ]; then
    # IF ARRAY EMPTY
    # echo ">>>>>" $COMMIT "<<<<<"
    exit 0
else
    # IF MERGE EXISTS
    for i in "$COMMIT"
    do
 
        REGEX_MERGE_DEVELOP="\bMerge.*?.branch.*?.develop\b"
 
        if [[ $COMMIT =~ $REGEX_MERGE_DEVELOP ]];
        then
 
            echo -e "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️"
        	echo -e "\e[31m"
        	echo -e "Please do not merge 'develop' into any other branch \e[0m"
        	echo -e "Review your commit:"
        	echo -e ""
 
            for i in "$COMMIT"
            do        	
            	echo -e "  $COMMIT"
            done
 
        	echo -e ""
            echo -e "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️"
 
        	exit 1
        else
            exit 0
        fi
    done
fi

Another Version

#!/bin/sh
 
read oldrev newrev refname protocol
 
#### http://api.icndb.com/jokes/random
 
merges=`git diff --name-only --stat $oldrev..$newrev`
 
for changed_file in `echo $merges`; do        
 
        REGEX_MERGE_DEVELOP="^.do_not_merge"
 
        # If $changed_file matches regex $REGEX_MERGE_DEVELOP
        if [[ $changed_file =~ $REGEX_MERGE_DEVELOP ]];
        then
 
            # MERGE_PREVENT_BRANCH=$(echo $changed_file | tr "_into_" "\n")
            MERGE_PREVENT_BRANCH=$(echo $changed_file | tr "_" "\n" | tail -1)
            MERGE_TO_BRANCH=$(echo $refname | tr "/" "\n" | tail -1)
 
            if [[ "$MERGE_PREVENT_BRANCH" -eq "$MERGE_TO_BRANCH" ]];
            then
 
                echo -e "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️"
            	echo -e "\e[31m"
            	echo -e "Please do not merge '$MERGE_PREVENT_BRANCH' into any other branch "
            	echo -e "This file prevents merging: $changed_file \e[0m"
            	echo -e "FIX IT: git reset HEAD $changed_file"
            	echo -e ""
                echo -e "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️"
                exit 1
            fi
        else
            exit 0
        fi
done
git_hooks.1511900014.txt.gz · Last modified: 2017/11/28 20:13 by admin