summaryrefslogtreecommitdiff
path: root/git-deploy-cache.sh
blob: 759c3f87d0767c3c469f0709156f688f133689a2 (plain)
    1 #!/usr/bin/env bash
    2 #
    3 # Copyright (C) 2015  Aaron Ball <nullspoon@iohq.net>
    4 # 
    5 # This program is free software: you can redistribute it and/or modify
    6 # it under the terms of the GNU General Public License as published by
    7 # the Free Software Foundation, either version 3 of the License, or
    8 # (at your option) any later version.
    9 # 
   10 # This program is distributed in the hope that it will be useful,
   11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   13 # GNU General Public License for more details.
   14 # 
   15 # You should have received a copy of the GNU General Public License
   16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
   17 # 
   18 
   19 
   20 #
   21 # == Description
   22 #
   23 # This script deploys the given bare git repo and branch to a working location.
   24 # It decides what needs to be deployed based on the variables received from a
   25 # git post-receive hook via STDIN.
   26 #
   27 # [STDIN]
   28 # @param oldref  SHA1 hash of commit being advanced from
   29 # @param newref  SHA1 hash of most recent git commit
   30 # @param refname Git name for ref. This is used to derive the branch name
   31 #                commited to
   32 #
   33 # @param $1 Path to the base path to deploy to
   34 #
   35 
   36 
   37 #
   38 # Sets up some handy variables for a git post-receive script to use.
   39 #
   40 # == Variables Provided
   41 #
   42 # REPOSITORY: Repository that was pushed to
   43 # BRANCH: Name of the branch that was modified
   44 # FILES_A: List of added files
   45 # FILES_M: List of modified files
   46 # FILES_D: List of deleted files
   47 #
   48 function enviro_setup {
   49   oldrev=${1}
   50   newrev=${2}
   51   refname=${3}
   52 
   53   # Repository containing the branch
   54   export REPOSITORY=$(basename $(pwd))
   55 
   56   # Name of the branch pushed to
   57   export BRANCH=${refname##*/}
   58 
   59   # Command to list files affected by this commit
   60   file_cmd="git diff --name-status ${oldrev} ${newrev}"
   61 
   62   # List of Added files
   63   export FILES_A=$(${file_cmd} | grep '^A' | cut -d '	' -f 2)
   64   # List of Modified files
   65   export FILES_M=$(${file_cmd} | grep '^M' | cut -d '	' -f 2)
   66   # List of Deleted files
   67   export FILES_D=$(${file_cmd} | grep '^D' | cut -d '	' -f 2)
   68   # List of all files changed (added or modified) but not deleted
   69   # Short for files_existing
   70   export FILES_E=$(${file_cmd} | grep '^[AM]' | cut -d '	' -f 2)
   71 }
   72 
   73 
   74 #
   75 # Logging function for everything here
   76 #
   77 function log {
   78   echo "$(date '+%s'): ${@}" >> ~/gitposthook.out
   79 }
   80 
   81 
   82 #
   83 # Deploys a working copy of the given repo and branch to the specified location
   84 #
   85 # @param remote Bare remote to be deployed locally
   86 # @param branch Branch to be deployed
   87 # @param dest   Destination location to deploy the working copy to
   88 #
   89 function deploy_cache {
   90   remote=${1}
   91   # Strip trailing / if one exists (this throws off our later parse of the path
   92   # to get the name of the repo
   93   if [[ ${remote:${#remote} - 1} == '/' ]]; then
   94     remote=${remote:0:-1}
   95   fi
   96 
   97   branch=${2}
   98   dest=${3}
   99 
  100   repo=${remote##*/}
  101   deploy=${dest}/${repo}/${branch}
  102 
  103   if [[ -d ${deploy} ]]; then
  104     log "Deleting ${deploy}"
  105     rm -rf ${deploy}
  106   fi
  107 
  108   log "Deploying to ${deploy}"
  109   mkdir -p ${deploy}
  110   cd ${deploy}
  111   log "Checkout branch ${branch}"
  112   git clone -l -b ${branch} ${remote} ./
  113   cd -
  114 }
  115 
  116 
  117 #
  118 # Main function
  119 #
  120 function main {
  121   # Read STDIN
  122   read oldrev newrev refname
  123   deploy_base="${1}"
  124 
  125   # Set up global enviro variables that will be of use later
  126   enviro_setup ${oldrev} ${newrev} ${refname}
  127 
  128   log "Deployment triggered by ${newrev}"
  129 
  130   # Deploy a working copy of the repo so we have something to work on
  131   deploy_cache $(pwd) ${BRANCH} ${deploy_base}
  132 }
  133 
  134 main ${@}
  135 #${oldrev} ${newrev} ${refname}

Generated by cgit