[rt18107] added branch syncing
authorLiam Whalen <liam.whalen@bc.libraries.coop>
Fri, 13 Dec 2013 23:29:02 +0000 (15:29 -0800)
committerLiam Whalen <liam.whalen@bc.libraries.coop>
Fri, 13 Dec 2013 23:30:39 +0000 (15:30 -0800)
By specifying -s you can now sync all files that have a mapping in
pathmap.ini with the server.

deployment/git-deploy.sh

index ad68998..a389ba6 100755 (executable)
@@ -38,7 +38,7 @@ function create_tmp_file() {
     echo "$TMP_DIR/$FILENAME"
 }
 
-function get_full_path() {
+function find_base_path() {
     RELATIVE_PATH=$1
 
     for COMPONENT in `access_pathmap.pl --config $PATH_MAP_FILE`
@@ -46,27 +46,40 @@ function get_full_path() {
         BASE_PATH=`access_pathmap.pl --config $PATH_MAP_FILE --component $COMPONENT --srcpath $RELATIVE_PATH`
         if [[ $BASE_PATH ]]
         then
-            #We extract the last directory in the base path so we can
-            #use parameter substitution to remove everything up to the
-            #last directory in the BASE_PATH from the RELATIVE_PATH
-            #this leaves us with the part of the git source tree path
-            #that needs to be appended to the base path in order to locate
-            #the file on the server
-            LAST_DIR_IN_BASE_PATH=`basename $BASE_PATH`
-            RELATIVE_PART=${RELATIVE_PATH/*$LAST_DIR_IN_BASE_PATH/}
-            echo $BASE_PATH$RELATIVE_PART
-            return;
+            echo $BASE_PATH
+            return
         fi
     done
 }
 
+
+function get_full_path() {
+    RELATIVE_PATH=$1
+    BASE_PATH=$2
+
+    if [[ $BASE_PATH ]]
+    then
+        #We extract the last directory in the base path so we can
+        #use parameter substitution to remove everything up to the
+        #last directory in the BASE_PATH from the RELATIVE_PATH
+        #this leaves us with the part of the git source tree path
+        #that needs to be appended to the base path in order to locate
+        #the file on the server
+        LAST_DIR_IN_BASE_PATH=`basename $BASE_PATH`
+        RELATIVE_PART=${RELATIVE_PATH/*$LAST_DIR_IN_BASE_PATH/}
+        echo $BASE_PATH$RELATIVE_PART
+        return
+    fi
+}
+
 function deployfile() {
     REMOTE=$1
     BRANCH=$2
-    RELATIVE_PATH=$3
-    FILENAME=$4
-    TMP_DIR=$5
-    HOSTS=$6
+    BASE_PATH=$3
+    RELATIVE_PATH=$4
+    FILENAME=$5
+    TMP_DIR=$6
+    HOSTS=$7
 
     PERL_FILE_RE='^.*.pm$'
     SQL_FILE_RE='^.*.sql$'
@@ -84,7 +97,7 @@ function deployfile() {
         USER='opensrf'
     fi
 
-    FULL_PATH=`get_full_path $RELATIVE_PATH`
+    FULL_PATH=`get_full_path $RELATIVE_PATH $BASE_PATH`
 
     TEMPFILE=`create_tmp_file $TMP_DIR $FILENAME`
     git --git-dir $REPO show $REMOTE/$BRANCH:$RELATIVE_PATH/$FILENAME > $TEMPFILE
@@ -106,7 +119,7 @@ function deployfile() {
     done
 }
 
-while getopts ":g:r:b:c:Pdh" opt
+while getopts ":g:r:b:c:Pdhs" opt
 do
     case $opt in
         g ) REPO=$OPTARG;;
@@ -115,6 +128,7 @@ do
         c ) PATH_MAP_FILE=$OPTARG;;
         P ) PROD="P";;
         d ) DEPLOY="d";;
+        s ) SYNC="s";;
         h ) usage && exit 0;;
     esac
 done
@@ -139,11 +153,35 @@ then
     exit 1
 fi
 
+if [[ ! -z $SYNC ]]
+then
+    read -p "Are you SURE you want to sync $HOST_LIST with $BRANCH (Yes/No): " ANSWER
+    echo $ANSWER
+    if [[ ! "$ANSWER" =~ [Yy]es ]]
+    then
+        echo "Aborting sync"
+        exit 2;
+    fi
+fi
+
 TMP_DIR=`mktemp -d`
-git --git-dir $REPO show $REMOTE/$BRANCH --name-only --oneline | awk '{if(NR!=1) {print}}' | \
-while read PATH_AND_FILENAME 
+
+if [[ -z "$SYNC" ]]
+then
+    LIST_BRANCH_FILES=`git --git-dir $REPO show $REMOTE/$BRANCH --name-only --oneline | awk '{if(NR!=1) {print}}'`
+else
+    BRANCH_NAME=`basename $BRANCH`
+    cd $REPO
+    LIST_BRANCH_FILES=`git ls-tree -r  --full-name $BRANCH_NAME | tr '\t' ' ' | cut -d ' ' -f 4`
+fi
+
+for PATH_AND_FILENAME in $LIST_BRANCH_FILES
 do
     FILENAME=`basename $PATH_AND_FILENAME`
     RELATIVE_PATH=`dirname $PATH_AND_FILENAME`
-    deployfile $REMOTE $BRANCH $RELATIVE_PATH $FILENAME $TMP_DIR "$HOST_LIST"
+    BASE_PATH=`find_base_path $RELATIVE_PATH`
+    if [[ $BASE_PATH ]]
+    then
+        deployfile $REMOTE $BRANCH $BASE_PATH $RELATIVE_PATH $FILENAME $TMP_DIR "$HOST_LIST"
+    fi
 done