diff options
author | Clément Bénier <clement.benier@iot.bzh> | 2018-10-22 17:37:27 +0200 |
---|---|---|
committer | Clément Bénier <clement.benier@iot.bzh> | 2018-10-22 17:37:27 +0200 |
commit | 3e43a348cf7e3d9bfb9911e23ab4672a6d365cac (patch) | |
tree | 8f74c4a3a2ecd3d8ebe8e87f3270ae90716b37dd | |
parent | ac7575d2dc5052f492aa898812f6e5d7d2dc18f4 (diff) |
setupdocs.sh: improve robustness
- return errors to stderr
- check references strongly with GITREF instead of git/nogit
Change-Id: Iee0dff19c8d24c079f9b72f6009b8efa132b8e40
Signed-off-by: Clément Bénier <clement.benier@iot.bzh>
-rwxr-xr-x | setupdocs.sh | 110 |
1 files changed, 72 insertions, 38 deletions
diff --git a/setupdocs.sh b/setupdocs.sh index f09e137..40807e5 100755 --- a/setupdocs.sh +++ b/setupdocs.sh @@ -1,15 +1,16 @@ #!/bin/bash -PROGNAME=${0##*/} +DEBUG=1 +PROGNAME=$(basename $BASH_SOURCE) DESTINATION="." -NOGIT=0 +GITREF="" DOCTOOLSDIR="doctools" docswebtemplates="https://github.com/automotive-grade-linux/docs-webtemplate.git" doctools="https://github.com/automotive-grade-linux/docs-tools.git" #default branch -branch_doctools="master" -branch_docswebtemplates="master" +ref_docswebtemplate="" +ref_doctools="master" pushd() { command pushd "$@" &> /dev/null @@ -17,19 +18,46 @@ pushd() { popd() { command popd "$@" &> /dev/null } +debug() { + [[ $DEBUG -eq 0 ]] && echo "[DEBUG]: $@" >&2 +} +error() { + echo "$@" >&2 +} + +gitcheckout() { + command git checkout "$@" &> /dev/null + if [ ! $? -eq 0 ]; then + error "Cannot checkout: $@ does not exit" + exit 4 + fi +} + +gitclone() { + command git clone "$@" &> /dev/null + if [ ! $? -eq 0 ]; then + error "Cannot clone $@ " + exit 5 + fi +} function usage() { - echo "Usage: setupdocs.sh [OPTIONS]... [DIRECTORY]" - echo " -d, --directory=[DST] directory destination" - echo " -h, --help print this help" - echo " -t, --doctools-branch=[BRANCH] doctools branch; BRANCH can be master or master-next" - echo " -w, --webtemplates-branch=[BRANCH] webtemplates branch; BRANCH can be master or master-next" + cat <<EOF >&2 +Usage: $PROGNAME [OPTIONS]... [DIRECTORY] + --debug debug mode + -d, --directory=[DST] directory destination; DST is the destination + -h, --help print this help + -t, --doctools-ref=[REFERENCE] doctools reference; + REFERENCE can be a branch, a tag, a commit + -w, --webtemplates-ref=[REFERENCE] webtemplates reference; + REFERENCE can be a branch, a tag, a commit +EOF exit 1 } SHORTOPTS="w:t:d:h" -LONGOPTS="webtemplates-branch:,doctools-branch:,directory:,help" +LONGOPTS="webtemplates-branch:,doctools-branch:,directory:,debug,help" ARGS=$(getopt -s bash --options $SHORTOPTS \ --longoptions $LONGOPTS --name $PROGNAME -- "$@" ) if [ ! $? -eq 0 ]; then @@ -40,11 +68,13 @@ eval set -- "$ARGS" while [ "$#" -gt "1" ]; do case "$1" in -w|--webtemplates-branch) - branch_docswebtemplates=$2;shift 2;; + ref_docswebtemplate=$2;shift 2;; -t|--doctools-branch) - branch_doctools=$2; shift 2;; + ref_doctools=$2; shift 2;; -d|--directory) DESTINATION=$2;shift 2;; + --debug) + DEBUG=0;shift 2;; -h|--help) usage;; *) @@ -52,55 +82,59 @@ while [ "$#" -gt "1" ]; do esac done -#check writable dir -if [ ! -w $DESTINATION ]; then - echo "$DESTINATION is not a writable directory" - exit 2 -fi - #make sure nodejs and jekyll are installed node -v && jekyll -v if [ ! $? -eq 0 ]; then - echo "please, make sure nodejs and jekyll are installed" + error "please, make sure nodejs and jekyll are installed" exit 3 fi -#cloning repos -pushd $DESTINATION -#Checking if current dir is docwebtemplates repo -currentremote=$(git remote -v 2> /dev/null) -if [ $? -eq 0 ]; then #within a git - #check in remote there is docswebtemplate - echo $currentremote | grep $(basename $docswebtemplates) &> /dev/null - if [ $? -eq 0 ]; then - NOGIT=1 - fi +#check writable dir +if [ ! -w $DESTINATION ]; then + error "$DESTINATION is not a writable directory" + exit 2 fi -if [ $NOGIT -eq 0 ]; then + +pushd $DESTINATION + +#get reference +[[ -d .git ]] && [[ "$(realpath $BASH_SOURCE)" == "$(realpath $(basename $BASH_SOURCE))" ]] && GITREF=$(git rev-parse HEAD) +ref_docswebtemplate=${ref_docswebtemplate:-${GITREF:-master}} +debug "ref_docswebtemplate=$ref_docswebtemplate ref_doctools=$ref_doctools" + +[[ -d .git ]] && rev=$(git show-ref -s $ref_docswebtemplate | sort | uniq) && rev=${rev:-$ref_docswebtemplate} + +debug "GITREF=$GITREF and rev=$rev" +#check that reference given matching with local repo +[[ "$GITREF" != "$rev" ]] \ + && { error "Invalid reference between $ref_docswebtemplate and local repo in $DESTINATION"; exit 5; } +#processing cloning or update +if [ -z $GITREF ]; then echo "Cloning docwebtemplates and doctools in $DESTINATION" - git clone -b $branch_docswebtemplates $docswebtemplates &> /dev/null - pushd $(basename $docswebtemplates | sed "s/\..*//") - git clone -b $branch_doctools $doctools $DOCTOOLSDIR &> /dev/null + gitclone $docswebtemplates . + gitcheckout $ref_docswebtemplate + gitclone $doctools $DOCTOOLSDIR pushd $DOCTOOLSDIR + gitcheckout $ref_doctools npm install popd - popd echo "docwebtemplates and doctools cloned in $DESTINATION" else - echo "you are in docs-webtemplate in branch $(git branch | grep "*"): process $DOCTOOLSDIR" + echo "you are in docs-webtemplate: process $DOCTOOLSDIR" echo "so no process will be done in docs-webtemplate" if [ -d $DOCTOOLSDIR ]; then - echo "$DOCTOOLSDIR already exits: process update with branch=$branch_doctools" + echo "$DOCTOOLSDIR already exits: process update with reference=$ref_doctools" pushd $DOCTOOLSDIR - git checkout $branch_doctools &> /dev/null + gitcheckout $branch_doctools git pull $doctools $branch_doctools &> /dev/null npm install popd else echo "cloning $DOCTOOLSDIR" - git clone -b $branch_doctools $doctools $DOCTOOLSDIR &> /dev/null + gitclone $doctools $DOCTOOLSDIR pushd $DOCTOOLSDIR + gitcheckout $ref_doctools npm install popd fi |