Skip to content

Instantly share code, notes, and snippets.

@japboy
Created November 21, 2012 00:40
Show Gist options
  • Save japboy/4122302 to your computer and use it in GitHub Desktop.
Save japboy/4122302 to your computer and use it in GitHub Desktop.
Useful Git hook script snippets that I can easily forget :P
#!/bin/bash
# Apply administrative privilege to a command line user
# 1) Add `git` user to sudoers:
# sudo /usr/sbin/visudo
# 2) Give permissions to `git` user:
# git ALL=NOPASSWD: /usr/local/bin/curlftpfs, /bin/mount, /bin/umount
# 3) Disable to prevent `sudo` w/o tty:
# Defaults requiretty # Comment out this
FTP_HOSTADDR='example.com'
FTP_USERNAME='username'
FTP_PASSWORD='password'
FS_TEMPDIR="git-temp-$(date '+%Y%m%d%I%M%S')"
FS_MOUNTPOINT="/mnt/${FS_TEMPDIR}"
FS_WORKPOINT="/tmp/${FS_TEMPDIR}"
RSYNC_SOURCE="${FS_WORKPOINT}/htdocs/shinsotsu"
RSYNC_DESTINATION="${FS_MOUNTPOINT}/www/shinsotsu_test/rsync_test"
while read OLDREV NEWREV REF
do
BRANCH=$(echo ${REF} | cut -d / -f 3,4)
# Branch `master`
if [ 'master' == "${BRANCH}" ]
then
if [ '0000000000000000000000000000000000000000' != ${OLDREV} ]
then
echo 'do something.'
fi
fi
# Branch `release/YYYYMMDD-hhmm`
if echo ${BRANCH} | grep -q '^release\/'
then
if ! test -d ${FS_MOUNTPOINT}
then
mkdir -p ${FS_MOUNTPOINT}
fi
if ! test -d ${FS_WORKPOINT}
then
mkdir -p ${FS_WORKPOINT}
fi
if ! sudo curlftpfs -o user=${FTP_USERNAME}:${FTP_PASSWORD} ${FTP_HOSTADDR} ${FS_MOUNTPOINT}
then
echo 'release ブランチのデプロイに失敗しました。#1'
rm -rf ${FS_WORKPOINT}
exit 1
fi
if ! git --work-tree=${FS_WORKPOINT} checkout -f ${BRANCH}
then
echo 'release ブランチのデプロイに失敗しました。#2'
rm -rf ${FS_WORKPOINT}
sudo umount ${FS_MOUNTPOINT}
exit 1
fi
cd ${FS_WORKPOINT}/brunch-backbone
if ! npm install && brunch build --minify
then
echo 'release ブランチのデプロイに失敗しました。#3'
rm -rf ${FS_WORKPOINT}
sudo umount ${FS_MOUNTPOINT}
exit 1
fi
cd ${FS_WORKPOINT}
if ! rsync --checksum \
--recursive \
--links \
--hard-links \
--perms \
--owner \
--group \
--times \
--delete \
--compress \
--progress \
${RSYNC_SOURCE} ${RSYNC_DESTINATION}
then
echo 'release ブランチのデプロイに失敗しました。'
rm -rf ${FS_WORKPOINT}
sudo umount ${FS_MOUNTPOINT}
exit 1
fi
rm -rf ${FS_WORKPOINT}
sudo umount ${FS_MOUNTPOINT}
echo 'ブランチのデプロイが完了しました。'
fi
# Branch `develop`
if [ 'develop' == "${BRANCH}" ]
then
git --work-tree=${DEVELOP_PATH} checkout -f ${BRANCH}
fi
# Branch `preview`
if [ 'preview' == "${BRANCH}" ]
then
git --work-tree=${PREVIEW_PATH} checkout -f ${BRANCH}
fi
# Branch `docs`
if [ 'docs' == "${BRANCH}" ]
then
git --work-tree=${DOCS_PATH} checkout -f ${BRANCH}
fi
done
#!/bin/bash
while read OLDREV NEWREV REF
do
# Linter
for FILE in $(git diff-index --cached --name-only ${NEWREV})
do
# PHP
if echo ${FILE} | egrep -q '\.(php|ctp)$'
then
if ! git show ${NEWREV}:${FILE} | php -l
then
"Error: ${FILE}"
exit 1
fi
fi
# CoffeeScript
if echo ${FILE} | egrep -q '\.coffee$'
then
if ! git show ${NEWREV}:${FILE} | coffeelint --stdin
then
"Error: ${FILE}"
exit 1
fi
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment