Commit | Line | Data |
---|---|---|
a211aa1b SBS |
1 | #!/bin/bash |
2 | # Desc: Initializes a git repository | |
3 | ||
d8604361 | 4 | #==BEGIN Define script parameters== |
d8604361 | 5 | #===BEGIN Declare local script functions=== |
a211aa1b SBS |
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 | |
e3cd181c | 34 | # Version: 0.0.8 |
a211aa1b SBS |
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 | |
77f67b96 SBS |
39 | # Note: Pulls use '--ff-only' option which requires an upstream branch be set. |
40 | # This can be done by using: | |
41 | # $ git branch --set-upstream-to origin/master master | |
42 | # | |
a211aa1b SBS |
43 | #==BEGIN Validate input arguments== |
44 | arg1="$1"; | |
45 | arg2="$2"; | |
46 | arg3="$3"; | |
47 | arg4="$4"; | |
48 | # Validate repoURL | |
49 | if checkURL "$arg1"; then repoURL="$arg1"; | |
50 | else yell "ERROR:Not a valid URL:$arg1"; return 1; fi; | |
51 | ||
6a81e334 SBS |
52 | # Passthrough repoDir (it may not exist yet) |
53 | repoDir="$arg2"; | |
54 | ||
a211aa1b SBS |
55 | # Validate remoteName |
56 | if [[ "$arg3" =~ ^[[:alnum:]]+$ ]]; then remoteName="$arg3"; | |
57 | else yell "ERROR:Not a valid remote name:$arg3"; return 1; fi; # See [1]. | |
58 | ||
59 | # Validate branchName | |
60 | if [[ "$arg4" =~ ^[[:alnum:]]+$ ]]; then branchName="$arg4"; | |
61 | else yell "ERROR:Not a valid remote branch name:$arg4"; return 1; fi; # See [1]. | |
62 | ||
63 | # Check argument count | |
64 | if [[ $# -gt 4 ]]; then yell "ERROR:Too many arguments."; return 1; fi; # See [2]. | |
65 | #==END Validate input arguments== | |
66 | #==BEGIN create and populate git repository== | |
67 | if [[ ! -d "$repoDir" ]]; then mkdir "$repoDir"; fi; | |
68 | pushd "$repoDir" || return 1; | |
69 | if ! git -C "$(pwd)" rev-parse 1>/dev/null 2>&1; then | |
70 | yell "STATUS:Initializing as git repo:$repoDir"; | |
71 | git init; # Init repo if not already a git repo. See [3] | |
72 | else | |
73 | yell "STATUS:Already a git repository:$repoDir"; | |
74 | git status; git remote -v; | |
a211aa1b SBS |
75 | fi; |
76 | yell "STATUS:Adding $repoURL as remote $remoteName"; | |
77 | git remote add "$remoteName" "$repoURL"; | |
78 | yell "STATUS:Pulling branch $branchName from remote $remoteName"; | |
77f67b96 | 79 | git pull --ff-only "$remoteName" "$branchName"; |
e3cd181c | 80 | git fetch "$remoteName"; |
a211aa1b SBS |
81 | unset repoURL repoDir remoteName branchName; |
82 | popd || exit 1; | |
83 | #==END create and populate git repository== | |
bd4ff344 | 84 | yell "================================"; |
a211aa1b | 85 | } # Init Git Repository |
d8604361 | 86 | #===END Declare local script functions=== |
0a18766c | 87 | #==END Define script parameters== |
a211aa1b | 88 | |
d8604361 | 89 | #==BEGIN sample code |
a211aa1b SBS |
90 | tmpDir=/tmp/"$(date +%Y%m%dT%H%M%S%z)"; |
91 | mkdir -p "$tmpDir"; | |
92 | pushd "$tmpDir" || exit 1; | |
93 | ||
94 | repoURL="https://zdv2.bktei.com/gitweb/baltakatei-exdev.git"; | |
95 | repoDir="BK-2020-03..bkexdev"; | |
96 | remoteName="zdv2"; | |
97 | branchName="master"; | |
98 | initGitRepo "$repoURL" "$repoDir" "$remoteName" "$branchName"; | |
99 | unset repoURL repoDir remoteName branchName; | |
100 | ||
101 | yell "STATUS:Done."; | |
d8604361 SBS |
102 | #==END sample code |
103 | ||
104 | # Author: Steven Baltaktei Sandoval | |
105 | # License: GPLv3+ |