2 # Desc: Initializes a git repository
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
10 # Desc: Checks if string is a valid URL.
11 # Warning: Does not correctly handle multi-byte characters.
14 # Output: return code 0: string is a valid URL
15 # return code 1: string is NOT a valid URL
17 # Ref/Attrib: https://stackoverflow.com/a/3184819
18 regex
='(https?|ftp|file|ssh|git)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
20 if [[ $arg1 =~
$regex ]]; then
25 } # Check if arg1 is valid URL
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
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==
45 if checkURL
"$arg1"; then repoURL
="$arg1";
46 else yell
"ERROR:Not a valid URL:$arg1"; return 1; fi;
49 if [[ "$arg3" =~ ^
[[:alnum
:]]+$
]]; then remoteName
="$arg3";
50 else yell
"ERROR:Not a valid remote name:$arg3"; return 1; fi; # See [1].
53 if [[ "$arg4" =~ ^
[[:alnum
:]]+$
]]; then branchName
="$arg4";
54 else yell
"ERROR:Not a valid remote branch name:$arg4"; return 1; fi; # See [1].
56 # Check argument count
57 if [[ $# -gt 4 ]]; then yell
"ERROR:Too many arguments."; return 1; fi; # See [2].
58 #==END Validate input arguments==
59 #==BEGIN create and populate git repository==
60 if [[ ! -d "$repoDir" ]]; then mkdir
"$repoDir"; fi;
61 pushd "$repoDir" ||
return 1;
62 if ! git
-C "$(pwd)" rev-parse
1>/dev
/null
2>&1; then
63 yell
"STATUS:Initializing as git repo:$repoDir";
64 git init
; # Init repo if not already a git repo. See [3]
66 yell
"STATUS:Already a git repository:$repoDir";
67 git status
; git remote
-v;
68 yell
"STATUS:Pausing 4 seconds..."; sleep 4;
70 yell
"STATUS:Adding $repoURL as remote $remoteName";
71 git remote add
"$remoteName" "$repoURL";
72 yell
"STATUS:Pulling branch $branchName from remote $remoteName";
73 git pull
"$remoteName" "$branchName";
74 unset repoURL repoDir remoteName branchName
;
76 #==END create and populate git repository==
77 } # Init Git Repository
78 #===END Declare local script functions===
79 #==END Define script parameters==
82 tmpDir
=/tmp
/"$(date +%Y%m%dT%H%M%S%z)";
84 pushd "$tmpDir" ||
exit 1;
86 repoURL
="https://zdv2.bktei.com/gitweb/baltakatei-exdev.git";
87 repoDir
="BK-2020-03..bkexdev";
90 initGitRepo
"$repoURL" "$repoDir" "$remoteName" "$branchName";
91 unset repoURL repoDir remoteName branchName
;
96 # Author: Steven Baltaktei Sandoval