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