summaryrefslogtreecommitdiff
path: root/git-deploy-cache.sh
blob: 8540cf3d9bf840ab6ee0a6742b19427f3aac9a6b (plain)
    1 #!/usr/bin/env bash
    2 #
    3 # == Description
    4 #
    5 # This script deploys the given bare git repo and branch to a working location.
    6 # It decides what needs to be deployed based on the variables received from a
    7 # git post-receive hook via STDIN.
    8 #
    9 # @param oldref  SHA1 hash of commit being advanced from
   10 # @param newref  SHA1 hash of most recent git commit
   11 # @param refname Git name for ref. This is used to derive the branch name
   12 #                commited to
   13 #
   14 
   15 
   16 #
   17 # Sets up some handy variables for a git post-receive script to use.
   18 #
   19 # == Variables
   20 #
   21 # REPOSITORY: Repository that was pushed to
   22 # BRANCH: Name of the branch that was modified
   23 # FILES_A: List of added files
   24 # FILES_M: List of modified files
   25 # FILES_D: List of deleted files
   26 #
   27 function enviro_setup {
   28   oldrev=${1}
   29   newrev=${2}
   30   refname=${3}
   31 
   32   # Repository containing the branch
   33   export REPOSITORY=$(basename $(pwd))
   34 
   35   # Name of the branch pushed to
   36   export BRANCH=${refname##*/}
   37 
   38   # Command to list files affected by this commit
   39   file_cmd="git diff --name-status ${oldrev} ${newrev}"
   40 
   41   # List of Added files
   42   export FILES_A=$(${file_cmd} | grep '^A' | cut -d '	' -f 2)
   43   # List of Modified files
   44   export FILES_M=$(${file_cmd} | grep '^M' | cut -d '	' -f 2)
   45   # List of Deleted files
   46   export FILES_D=$(${file_cmd} | grep '^D' | cut -d '	' -f 2)
   47   # List of all files changed (added or modified) but not deleted
   48   # Short for files_existing
   49   export FILES_E=$(${file_cmd} | grep '^[AM]' | cut -d '	' -f 2)
   50 }
   51 
   52 
   53 #
   54 # Logging function for everything here
   55 #
   56 function log {
   57   echo "$(date '+%s'): ${@}" >> ~/gitposthook.out
   58 }
   59 
   60 
   61 #
   62 # Deploys a working copy of the given repo and branch to the specified location
   63 #
   64 # @param remote Bare remote to be deployed locally
   65 # @param branch Branch to be deployed
   66 # @param dest   Destination location to deploy the working copy to
   67 #
   68 function deploy_cache {
   69   remote=${1}
   70   # Strip trailing / if one exists (this throws off our later parse of the path
   71   # to get the name of the repo
   72   if [[ ${remote:${#remote} - 1} == '/' ]]; then
   73     remote=${remote:0:-1}
   74   fi
   75 
   76   branch=${2}
   77   dest=${3}
   78 
   79   repo=${remote##*/}
   80   deploy=${dest}/${repo}/${branch}
   81 
   82   if [[ -d ${deploy} ]]; then
   83     log "Deleting ${deploy}"
   84     rm -rf ${deploy}
   85   fi
   86 
   87   log "Deploying to ${deploy}"
   88   mkdir -p ${deploy}
   89   cd ${deploy}
   90   log "Checkout branch ${branch}"
   91   git clone -l -b ${branch} ${remote} ./
   92   cd -
   93 }
   94 
   95 
   96 #
   97 # Main function
   98 #
   99 function main {
  100   # Read STDIN
  101   read oldrev newrev refname
  102   deploy_base="${1}"
  103 
  104   # Set up global enviro variables that will be of use later
  105   enviro_setup ${oldrev} ${newrev} ${refname}
  106 
  107   log "Deployment triggered by ${newrev}"
  108 
  109   # Deploy a working copy of the repo so we have something to work on
  110   deploy_cache $(pwd) ${BRANCH} ${deploy_base}
  111 }
  112 
  113 main ${@}
  114 #${oldrev} ${newrev} ${refname}

Generated by cgit