Skip to content

Instantly share code, notes, and snippets.

@drmercer
Last active June 15, 2018 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmercer/65672f41210c2538ba4188eb8e750e44 to your computer and use it in GitHub Desktop.
Save drmercer/65672f41210c2538ba4188eb8e750e44 to your computer and use it in GitHub Desktop.
A pair of git hooks for emulating receive.denyCurrentBranch=updateInstead on older git versions (MIT)
These two git hooks could be placed in your (remote) repository's `hooks` directory to emulate the behavior of
receive.denyCurrentBranch=updateInstead on older versions of git that don't support that option for that setting.
Make sure both are executable, and don't put any file extensions on them.
These hooks are licensed under the MIT license (found in the `~LICENSE` file).
#!/bin/sh
#
# A post-receive hook that simply updates the working directory
#
# Clear pesky variables:
unset GIT_DIR
unset GIT_WORK_TREE
cd .. # Change to repo root
# Update the working directory:
git reset --hard HEAD
#!/bin/sh
#
# A pre-receive hook for emulating receive.denyCurrentBranch=updateInstead on older
# git versions. I have used it on version 1.7.1.
#
# This script aborts the receive if there are local unsaved changes in the repository.
#
# Remove pesky environment variables
unset GIT_DIR
unset GIT_WORK_TREE
cd .. # Change dir to the repo root
# Check that the denyCurrentBranch setting is 'ignore'
dcb=$(git config --get receive.denyCurrentBranch)
if [ "$dcb" != "ignore" ]; then
echo 'WARNING: Your receive.denyCurrentBranch config value is not set to "ignore".'
fi
# Check for unsaved local changes
if ! git diff-index --quiet HEAD -- ; then
echo "There are unsaved changes in the remote worktree."
exit 1
else
exit 0
fi
The MIT License
Copyright (c) 2016 Dan Mercer
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS
IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment