Skip to content

Instantly share code, notes, and snippets.

@hujuice
Last active March 9, 2021 00:57
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hujuice/bddeffb378df37c17d93909180455ea0 to your computer and use it in GitHub Desktop.
Save hujuice/bddeffb378df37c17d93909180455ea0 to your computer and use it in GitHub Desktop.
Integration manager repository Git hooks for a PHP project

Here are suggested hooks for integration manager repository, in a composer based PHP project.

The IM has all the developer hooks, more the prepare-commit-msg to intercept unwanted merges.

#!/usr/bin/env bash
# -------------------------------------------------------------#
# Avoid merge from remote branches other than master to master #
# -------------------------------------------------------------#
# Sergio Vaccaro <sergio.vaccaro@istat.it>
# Translation in bash and customization from this ruby script:
# https://gist.github.com/mwise/69ec35b646b52d98050d#file-prepare-commit-msg
# This git hook will prevent merging remotes branches (pull) into local master
# Only remote/master can be pulled in master
# TODO: detect custom tracking branches
# Put this file in your local repo, in the .git/hooks folder
# and make sure it is executable.
# The name of the file *must* be "prepare-commit-msg" for Git to pick it up.
# Branch to protect
PROTECTED_BRANCH="master"
# Remote
REMOTE=""
# Check for merges
if [[ $2 != 'merge' ]]; then
# Not a merge
exit 0
fi
# Current branch
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Check if in PROTECTED_BRANCH
if [[ "$CURRENT_BRANCH" != "$PROTECTED_BRANCH" ]]; then
# Not in PROTECTED_BRANCH: can proceed
exit 0
fi
# Merging from
FROM_BRANCH=$(head -n 1 $1 | sed -e "s/^\s*Merge .*\?branch '\(.*\?\)'.*$/\1/")
# Check if the source is a local branch
if [[ -e ".git/refs/heads/${FROM_BRANCH}" ]]; then
# Merge from a local branch: can proceed
exit 0
fi
# Check if merging from remote/PROTECTED_BRANCH
if [[ "${FROM_BRANCH%$PROTECTED_BRANCH}" != ${FROM_BRANCH} ]]; then
# Remote PROTECTED_BRANCH to local PROTECTED_BRANCH: can proceed
exit 0
fi
echo "Merge from a remote branches other than ${PROTECTED_BRANCH} to ${PROTECTED_BRANCH}"
echo
echo "[POLICY] Forbidden cross merge"
echo " You are trying to merge from a remote branch to the local protected branch ${PROTECTED_BRANCH}."
echo " You can merge to ${PROTECTED_BRANCH} from local branches or from the ${PROTECTED_BRANCH} of the remote."
echo " The operation has been interrupted."
echo
echo " The merged content has been added to your working directory and to the index."
echo " You can:"
echo " - switch to another branch to continue"
echo " git checkout --track ${FROM_BRANCH}"
echo " - abort the operation"
echo " git reset --merge"
echo " - make a manual commit (discouraged)"
echo " git commit"
echo
echo "The following message is generated by git and is discouraged."
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment