From: Steven Baltakatei Sandoval Date: Tue, 23 Mar 2021 12:33:21 +0000 (+0000) Subject: feat(unitproc:bktemp-initGitRepo):Add bash script to init git repos X-Git-Tag: 0.4.3~3 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/a211aa1b371c8a4cdc08ed47f3852c4d3081e502?hp=-c feat(unitproc:bktemp-initGitRepo):Add bash script to init git repos --- a211aa1b371c8a4cdc08ed47f3852c4d3081e502 diff --git a/unitproc/bktemp-initGitRepo b/unitproc/bktemp-initGitRepo new file mode 100644 index 0000000..7a532fa --- /dev/null +++ b/unitproc/bktemp-initGitRepo @@ -0,0 +1,90 @@ +#!/bin/bash +# Desc: Initializes a git repository + +yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique +die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370 +try() { "$@" || die "cannot $*"; } #o +checkURL() { + # Desc: Checks if string is a valid URL. + # Warning: Does not correctly handle multi-byte characters. + # Version: 0.0.2 + # Input: arg1: string + # Output: return code 0: string is a valid URL + # return code 1: string is NOT a valid URL + # Depends: Bash 5.0.3 + # Ref/Attrib: https://stackoverflow.com/a/3184819 + regex='(https?|ftp|file|ssh|git)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' + arg1="$1"; + if [[ $arg1 =~ $regex ]]; then + return 0; + else + return 1; + fi; +} # Check if arg1 is valid URL +initGitRepo() { + # Desc: Creates, adds remotes to, and pulls to git repository + # Note: Does nothing if repoDir is already initialized git repo. + # Usage: createGitRepo arg1 arg2 arg3 arg4 + # Input: arg1: repoURL + # arg2: repoDir + # arg3: remoteName + # arg4: branchName + # Version: 0.0.4 + # Depends: checkURL() 0.0.2, yell(), Bash 5.0.3 + # Ref/Attrib: [1]: Test for space-less alphanuemric string. https://unix.stackexchange.com/a/416120 + # [2]: Test for argument count. https://stackoverflow.com/q/18568706 + # [3]: Test if dir is (in) git repository. https://stackoverflow.com/a/39518382 + #==BEGIN Validate input arguments== + arg1="$1"; + arg2="$2"; + arg3="$3"; + arg4="$4"; + # Validate repoURL + if checkURL "$arg1"; then repoURL="$arg1"; + else yell "ERROR:Not a valid URL:$arg1"; return 1; fi; + + # Validate remoteName + if [[ "$arg3" =~ ^[[:alnum:]]+$ ]]; then remoteName="$arg3"; + else yell "ERROR:Not a valid remote name:$arg3"; return 1; fi; # See [1]. + + # Validate branchName + if [[ "$arg4" =~ ^[[:alnum:]]+$ ]]; then branchName="$arg4"; + else yell "ERROR:Not a valid remote branch name:$arg4"; return 1; fi; # See [1]. + + # Check argument count + if [[ $# -gt 4 ]]; then yell "ERROR:Too many arguments."; return 1; fi; # See [2]. + #==END Validate input arguments== + #==BEGIN create and populate git repository== + if [[ ! -d "$repoDir" ]]; then mkdir "$repoDir"; fi; + pushd "$repoDir" || return 1; + if ! git -C "$(pwd)" rev-parse 1>/dev/null 2>&1; then + yell "STATUS:Initializing as git repo:$repoDir"; + git init; # Init repo if not already a git repo. See [3] + else + yell "STATUS:Already a git repository:$repoDir"; + git status; git remote -v; + yell "STATUS:Pausing 4 seconds..."; sleep 4; + fi; + yell "STATUS:Adding $repoURL as remote $remoteName"; + git remote add "$remoteName" "$repoURL"; + yell "STATUS:Pulling branch $branchName from remote $remoteName"; + git pull "$remoteName" "$branchName"; + unset repoURL repoDir remoteName branchName; + popd || exit 1; + #==END create and populate git repository== +} # Init Git Repository + +#==BEGIN test code +tmpDir=/tmp/"$(date +%Y%m%dT%H%M%S%z)"; +mkdir -p "$tmpDir"; +pushd "$tmpDir" || exit 1; + +repoURL="https://zdv2.bktei.com/gitweb/baltakatei-exdev.git"; +repoDir="BK-2020-03..bkexdev"; +remoteName="zdv2"; +branchName="master"; +initGitRepo "$repoURL" "$repoDir" "$remoteName" "$branchName"; +unset repoURL repoDir remoteName branchName; + +yell "STATUS:Done."; +#==END test code