I have two branches, a production branch, and a master branch. I want to prevent merging into production any new branch that checked out from master and master itself.
There is a git pre hook that can help with that?
I have two branches, a production branch, and a master branch. I want to prevent merging into production any new branch that checked out from master and master itself.
There is a git pre hook that can help with that?
chmod +x .git/hooks/prepare-commit-msg
git config branch.master.mergeoptions "--no-ff"
.git/hooks/prepare-commit-msg
#!/usr/bin/env ruby
# Only for exceptions. In your case is empty array
WHITELIST_BRANCH = ["hotfix"]
def merge?
ARGV[1] == "merge"
end
def into_master?
current_branch = `git branch | grep '*' | sed 's/* //'`.chop
current_branch == "master"
end
def merge_msg
@msg ||= `cat .git/MERGE_MSG`
end
def from_branch
@from_branch = merge_msg.match(/Merge branch '(.*?)'/)[1]
end
def whitelist_branch?
WHITELIST_BRANCH.include?(from_branch)
end
if merge? && into_master? && !whitelist_branch?
out = `git reset --merge`
puts
puts " Fail #{from_branch} into the *master* branch "
puts
puts " run the following command now to discard your working tree changes:"
puts
puts " git reset --merge"
puts
exit 1
end
In this code only WHITELIST_BRANCH is available to merge