feat(unitproc:bktemp-initGitRepo):Add bash script to init git repos
[BK-2020-03.git] / unitproc / bktemp-initGitRepo
... / ...
CommitLineData
1#!/bin/bash
2# Desc: Initializes a git repository
3
4yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique
5die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370
6try() { "$@" || die "cannot $*"; } #o
7checkURL() {
8 # Desc: Checks if string is a valid URL.
9 # Warning: Does not correctly handle multi-byte characters.
10 # Version: 0.0.2
11 # Input: arg1: string
12 # Output: return code 0: string is a valid URL
13 # return code 1: string is NOT a valid URL
14 # Depends: Bash 5.0.3
15 # Ref/Attrib: https://stackoverflow.com/a/3184819
16 regex='(https?|ftp|file|ssh|git)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
17 arg1="$1";
18 if [[ $arg1 =~ $regex ]]; then
19 return 0;
20 else
21 return 1;
22 fi;
23} # Check if arg1 is valid URL
24initGitRepo() {
25 # Desc: Creates, adds remotes to, and pulls to git repository
26 # Note: Does nothing if repoDir is already initialized git repo.
27 # Usage: createGitRepo arg1 arg2 arg3 arg4
28 # Input: arg1: repoURL
29 # arg2: repoDir
30 # arg3: remoteName
31 # arg4: branchName
32 # Version: 0.0.4
33 # Depends: checkURL() 0.0.2, yell(), Bash 5.0.3
34 # Ref/Attrib: [1]: Test for space-less alphanuemric string. https://unix.stackexchange.com/a/416120
35 # [2]: Test for argument count. https://stackoverflow.com/q/18568706
36 # [3]: Test if dir is (in) git repository. https://stackoverflow.com/a/39518382
37 #==BEGIN Validate input arguments==
38 arg1="$1";
39 arg2="$2";
40 arg3="$3";
41 arg4="$4";
42 # Validate repoURL
43 if checkURL "$arg1"; then repoURL="$arg1";
44 else yell "ERROR:Not a valid URL:$arg1"; return 1; fi;
45
46 # Validate remoteName
47 if [[ "$arg3" =~ ^[[:alnum:]]+$ ]]; then remoteName="$arg3";
48 else yell "ERROR:Not a valid remote name:$arg3"; return 1; fi; # See [1].
49
50 # Validate branchName
51 if [[ "$arg4" =~ ^[[:alnum:]]+$ ]]; then branchName="$arg4";
52 else yell "ERROR:Not a valid remote branch name:$arg4"; return 1; fi; # See [1].
53
54 # Check argument count
55 if [[ $# -gt 4 ]]; then yell "ERROR:Too many arguments."; return 1; fi; # See [2].
56 #==END Validate input arguments==
57 #==BEGIN create and populate git repository==
58 if [[ ! -d "$repoDir" ]]; then mkdir "$repoDir"; fi;
59 pushd "$repoDir" || return 1;
60 if ! git -C "$(pwd)" rev-parse 1>/dev/null 2>&1; then
61 yell "STATUS:Initializing as git repo:$repoDir";
62 git init; # Init repo if not already a git repo. See [3]
63 else
64 yell "STATUS:Already a git repository:$repoDir";
65 git status; git remote -v;
66 yell "STATUS:Pausing 4 seconds..."; sleep 4;
67 fi;
68 yell "STATUS:Adding $repoURL as remote $remoteName";
69 git remote add "$remoteName" "$repoURL";
70 yell "STATUS:Pulling branch $branchName from remote $remoteName";
71 git pull "$remoteName" "$branchName";
72 unset repoURL repoDir remoteName branchName;
73 popd || exit 1;
74 #==END create and populate git repository==
75} # Init Git Repository
76
77#==BEGIN test code
78tmpDir=/tmp/"$(date +%Y%m%dT%H%M%S%z)";
79mkdir -p "$tmpDir";
80pushd "$tmpDir" || exit 1;
81
82repoURL="https://zdv2.bktei.com/gitweb/baltakatei-exdev.git";
83repoDir="BK-2020-03..bkexdev";
84remoteName="zdv2";
85branchName="master";
86initGitRepo "$repoURL" "$repoDir" "$remoteName" "$branchName";
87unset repoURL repoDir remoteName branchName;
88
89yell "STATUS:Done.";
90#==END test code