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