From 683b06a51ff740dffebdc8998b4cf9ebba809168 Mon Sep 17 00:00:00 2001 From: Markus Treinen Date: Sun, 10 Jun 2018 16:49:03 +0200 Subject: [PATCH 1/3] Make file loop space-safe See https://askubuntu.com/questions/343727/filenames-with-spaces-breaking-for-loop-find-command --- bash/git-deploy-hook.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash/git-deploy-hook.sh b/bash/git-deploy-hook.sh index b42aead..945b6b0 100755 --- a/bash/git-deploy-hook.sh +++ b/bash/git-deploy-hook.sh @@ -196,7 +196,7 @@ do if [ "${timestamps}" == "true" ] then # Set modification times to last-changed - for file in $(find ./ -type f) + find ./ -type f -print0 | while IFS= read -r -d '' file do # Get the date of the last commit last=$(git log ${branch} --pretty=format:%ad --date=rfc -1 -- ${file}) From fd4ed759f717b9a36601069456ac4cb6c29e96ea Mon Sep 17 00:00:00 2001 From: Markus Treinen Date: Thu, 14 Jun 2018 20:27:06 +0200 Subject: [PATCH 2/3] Removed some outputs and sanitized a few variable uses against space splitting --- bash/git-deploy-hook.sh | 62 ++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/bash/git-deploy-hook.sh b/bash/git-deploy-hook.sh index 945b6b0..81a4398 100755 --- a/bash/git-deploy-hook.sh +++ b/bash/git-deploy-hook.sh @@ -4,10 +4,15 @@ # # git post-receive hook to check out branches to a rsync destination # -# Copyright 2012 K and H Research Company. +# Copyright 2012 K and H Research Company, 2018 Markus Treinen. # License: WTFPL, any version or GNU General Public License, version 2+ # +##### TODO MT +# - Selektiver Source-Pfad (Option) +# - Doku +##### + ## ## Documentation ## @@ -83,17 +88,17 @@ GIT=$(which git) RSYNC=$(which rsync) # Temporary directory -TMP="/tmp" +TMP='/tmp' -# Repo directory -export GIT_DIR=$(pwd) - - -## -## Variables -## +# Umask +UMASK='022' +# Rsync default opts +#RSYNC_DEFAULT='-rt --delete' +RSYNC_DEFAULT='-vrtEF --delete-after --delay-updates' +# Repo directory +export GIT_DIR=$(pwd) ## @@ -145,39 +150,39 @@ do # Find branch name branch=${ref#"refs/heads/"} - # Check branch name - if [ -z "${branch}" ] + # Check branch name (skip if not a branch) + if [ -z "${branch}" ] || [[ ! "${ref}" =~ ^refs/heads/.* ]] then - echo "Refspec ${ref} is not a branch. Skipped!" + #echo "Refspec ${ref} is not a branch. Skipped!" + continue fi # Don't attempt to handle deleted branches - if [ "${new}" = "0000000000000000000000000000000000000000" ] + if [[ "${new}" =~ ^0+$ ]] then # Error && skip branch echo "Branch ${branch} deleted. Skipped!" continue fi - ## Attempt to update - echo "Branch ${branch} updated. Deploying..." - - # Deploy destination + # Deploy destination (skip if not set) dest=$(git config --get "deploy.${branch}.uri") if [ -z "${dest}" ] then - echo "Error: Destination not set! Deploy failed." + #echo "Warning: Destination not set! Deploy skipped." continue fi - echo "Destination: "${dest} - + # Rsync options opts=$(git config --get "deploy.${branch}.opts") if [ -z "${opts}" ] then - opts="-rt --delete" + opts="$RSYNC_DEFAULT" + fi - echo "Options: "${opts} + + ## Attempt to update + echo "Branch ${branch} updated. Deploying to ${dest}..." # Create directory to archive into mkdir "${scratch}/${branch}" @@ -186,7 +191,7 @@ do cd "${scratch}/${branch}" # Set umask - umask 007 + umask "${UMASK}" # Get a copy of worktree $GIT archive --format=tar ${new} | tar xf - @@ -199,14 +204,14 @@ do find ./ -type f -print0 | while IFS= read -r -d '' file do # Get the date of the last commit - last=$(git log ${branch} --pretty=format:%ad --date=rfc -1 -- ${file}) + last=$(git log ${branch} --pretty=format:%ad --date=rfc -1 -- "${file}") # Set the modification time - touch -t $(date -d "${last}" +%Y%m%d%H%M.%S) ${file} + touch -t $(date -d "${last}" +%Y%m%d%H%M.%S) "${file}" done fi # Copy worktree to destination - $RSYNC $opts "${scratch}/${branch}/" "${dest}" + "$RSYNC" $opts "${scratch}/${branch}/" "${dest}" status=$? if [ "${status}" -ne "0" ] @@ -224,8 +229,7 @@ done ## # Remove scratch dir -rm ${scratch} -rf +rm "${scratch}" -rf # Unset environment variables -unset GIT RSYNC TMP GIT_DIR scratch old new ref branch dest optstimestamps file -unset last +unset GIT RSYNC TMP GIT_DIR scratch old new ref branch dest optstimestamps file last From 27b8ac766f319ea24e82c5431aa76e4710512760 Mon Sep 17 00:00:00 2001 From: Markus Treinen Date: Fri, 15 Jun 2018 00:32:43 +0200 Subject: [PATCH 3/3] Add branch.$FOO.subdir option to deploy only one specific subdirectory. --- bash/git-deploy-hook.sh | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/bash/git-deploy-hook.sh b/bash/git-deploy-hook.sh index 81a4398..57f968e 100755 --- a/bash/git-deploy-hook.sh +++ b/bash/git-deploy-hook.sh @@ -8,11 +8,6 @@ # License: WTFPL, any version or GNU General Public License, version 2+ # -##### TODO MT -# - Selektiver Source-Pfad (Option) -# - Doku -##### - ## ## Documentation ## @@ -59,6 +54,11 @@ # scheme which is known to 'rsync', including a local filesystem path, or # a remote host(via SSH) # +# deploy.$FOO.subdir +# Only rsync the specified subdirectory of the worktree for branch $FOO. +# This is useful if you have other stuff that should not be deployed +# and all relevant things are under this subdirectory. +# ## Usage # @@ -173,6 +173,11 @@ do continue fi + # Extract from different directory? + subdir=$(git config --get "deploy.${branch}.subdir") + subdir=${subdir:-"/"} + subdir=${subdir#"/"} + # Rsync options opts=$(git config --get "deploy.${branch}.opts") if [ -z "${opts}" ] @@ -181,6 +186,7 @@ do fi + ## Attempt to update echo "Branch ${branch} updated. Deploying to ${dest}..." @@ -211,7 +217,7 @@ do fi # Copy worktree to destination - "$RSYNC" $opts "${scratch}/${branch}/" "${dest}" + "$RSYNC" $opts "${scratch}/${branch}/${subdir}" "${dest}" status=$? if [ "${status}" -ne "0" ] @@ -232,4 +238,4 @@ done rm "${scratch}" -rf # Unset environment variables -unset GIT RSYNC TMP GIT_DIR scratch old new ref branch dest optstimestamps file last +unset GIT RSYNC TMP GIT_DIR scratch old new ref branch dest optstimestamps file last subdir