forked from thoughtworks/talisman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·236 lines (194 loc) · 7.6 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/bash
set -euo pipefail
# we call run() at the end of the script to prevent inconsistent state in case
# user runs with curl|bash and curl fails in the middle of the download
# (https://www.seancassidy.me/dont-pipe-to-your-shell.html)
if [ $# -eq 0 ]
then
echo "Talisman supports two types of git-hooks, pre-push hook and the pre-commit hook.\nEither of them needs to be passed as the argument to the script.\nThe script will now exit as no parameter is passed to the script.\nFor more details visit https://github.com/karanmilan/talisman and see the Running Talisman section."
exit 0
fi
if [ $# -eq 1 ]
then
if [ $1 == 'pre-push' ]
then
REPO_HOOK=".git/hooks/pre-push"
HOOK="pre-push"
elif [ $1 == 'pre-commit' ]
then
REPO_HOOK=".git/hooks/pre-commit"
HOOK="pre-commit"
else
echo "Talisman only supports either pre-push or pre-commit hooks"
exit 0
fi
else
echo "The number of arguments provided to the script are incorrect, please check https://github.com/karanmilan/talisman and see the Running Talisman section for the correct implementation"
exit 0
fi
run() {
IFS=$'\n'
VERSION="v0.2.1"
GITHUB_URL="https://github.com/karanmilan/talisman"
BINARY_BASE_URL="$GITHUB_URL/releases/download/$VERSION/talisman"
DEFAULT_GLOBAL_TEMPLATE_DIR="$HOME/.git-templates"
EXPECTED_BINARY_SHA_LINUX_AMD64="55200e0e7ab572b33675b7f4c7af50597361fdc4e8f219e04bc08b0ba9f7426c"
EXPECTED_BINARY_SHA_LINUX_X86="2158e8b1c5ad57009565e4ce8d39a4511c71802caf4b91843c8feb2f9d796673"
EXPECTED_BINARY_SHA_DARWIN_AMD64="30b6252eb2aa8a2e97ba50961d3f3a692359d2ca1debad698c5d50fa81ed8a61"
declare DOWNLOADED_BINARY
E_HOOK_ALREADY_PRESENT=1
E_CHECKSUM_MISMATCH=2
E_USER_CANCEL=3
E_HEADLESS=4
E_UNSUPPORTED_ARCH=5
E_DEPENDENCY_NOT_FOUND=6
echo_error() {
echo -ne $(tput setaf 1) >&2
echo "$1" >&2
echo -ne $(tput sgr0) >&2
}
binary_arch_suffix() {
declare ARCHITECTURE
if [[ "$(uname -s)" == "Linux" ]]; then
ARCHITECTURE="linux"
elif [[ "$(uname -s)" == "Darwin" ]]; then
ARCHITECTURE="darwin"
else
echo "Do you use Windows machine[Y/N]"
read WINDOWS_CHOICE
if [ $WINDOWS_CHOICE -eq 'Y' ] || [ $WINDOWS_CHOICE -eq 'y' ]; then
ARCHITECTURE="windows"
else
echo_error "Talisman currently only supports x86 and x86_64 architectures."
echo_error "If this is a problem for you, please open an issue: https://github.com/thoughtworks/talisman/issues/new"
exit $E_UNSUPPORTED_ARCH
fi
fi
if [[ "$(uname -m)" = "x86_64" ]]; then
ARCHITECTURE="${ARCHITECTURE}_amd64"
elif [[ "$(uname -m)" =~ '^i.?86$' ]]; then
ARCHITECTURE="${ARCHITECTURE}_386"
else
echo_error "Talisman currently only supports x86 and x86_64 architectures."
echo_error "If this is a problem for you, please open an issue: https://github.com/thoughtworks/talisman/issues/new"
exit $E_UNSUPPORTED_ARCH
fi
echo $ARCHITECTURE
}
download() {
if [[ ! -x "$(which curl 2>/dev/null)" ]]; then
echo_error "This script requires 'curl' to download the Talisman binary."
exit $E_DEPENDENCY_NOT_FOUND
fi
if [[ ! -x "$(which shasum 2>/dev/null)" ]]; then
echo_error "This script requires 'shasum' to verify the Talisman binary."
exit $E_DEPENDENCY_NOT_FOUND
fi
echo 'Downloading and verifying binary...'
echo
TMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'talisman')
trap 'rm -r $TMP_DIR' EXIT
chmod 0700 $TMP_DIR
ARCH_SUFFIX=$(binary_arch_suffix)
curl --location --silent "${BINARY_BASE_URL}_${ARCH_SUFFIX}_${HOOK}" > $TMP_DIR/talisman
DOWNLOADED_BINARY="$TMP_DIR/talisman"
}
install_to_repo() {
if [[ -x "$REPO_HOOK" ]]; then
echo_error "Oops, it looks like you already have a similar hook installed at '$REPO_HOOK'."
echo_error "If this is a problem for you, please open an issue: https://github.com/thoughtworks/talisman/issues/new"
exit $E_HOOK_ALREADY_PRESENT
fi
download
mkdir -p $(dirname $REPO_HOOK)
cp $DOWNLOADED_BINARY $REPO_HOOK
chmod +x $REPO_HOOK
echo -ne $(tput setaf 2)
echo "Talisman successfully installed to '$REPO_HOOK'."
echo -ne $(tput sgr0)
}
install_to_git_templates() {
if [[ ! -t 1 ]]; then
echo_error "Headless install to system templates is not supported."
echo_error "If you would like this feature, please open an issue: https://github.com/thoughtworks/talisman/issues/new"
exit $E_HEADLESS
fi
TEMPLATE_DIR=$(git config --global init.templatedir) || true
echo "Not running from inside a git repository... installing as a"
echo "git template."
echo
echo "If you meant to install to a specific repo, 'cd' into that"
echo "repo and run this script again."
echo
echo "Installing as a template will automatically add Talisman to"
echo "any new repo that you 'init' or 'clone'."
echo
if [[ "$TEMPLATE_DIR" == "" ]]; then
echo "No git template directory is configured. Let's add one."
echo "(this will override any system git templates and modify your git config file)"
echo
read -u1 -p "Git template directory: ($DEFAULT_GLOBAL_TEMPLATE_DIR) " TEMPLATE_DIR
echo
TEMPLATE_DIR=${TEMPLATE_DIR:-$DEFAULT_GLOBAL_TEMPLATE_DIR}
git config --global init.templatedir $TEMPLATE_DIR
else
echo "You already have a git template directory configured."
echo
read -u1 -p "Add Talisman to '$TEMPLATE_DIR/hooks?' (Y/n) " USE_EXISTING
echo
case "$USE_EXISTING" in
Y|y|"") ;; # okay, continue
*)
echo_error "Not installing Talisman."
echo_error "If you were trying to install into a single git repo, re-run this command from that repo."
echo_error "You can always download/compile manually from our Github page: $GITHUB_URL"
exit $E_USER_CANCEL
;;
esac
fi
# Support '~' in path
TEMPLATE_DIR=${TEMPLATE_DIR/#\~/$HOME}
if [ -f "$TEMPLATE_DIR/hooks/$HOOK" ]; then
echo_error "Oops, it looks like you already have a $HOOK hook installed at '$TEMPLATE_DIR/hooks/$HOOK'."
echo_error "Talisman is not compatible with other hooks right now, sorry."
echo_error "If this is a problem for you, please open an issue: https://github.com/thoughtworks/talisman/issues/new"
exit $E_HOOK_ALREADY_PRESENT
fi
mkdir -p "$TEMPLATE_DIR/hooks"
download
cp $DOWNLOADED_BINARY "$TEMPLATE_DIR/hooks/$HOOK"
chmod +x "$TEMPLATE_DIR/hooks/$HOOK"
echo -ne $(tput setaf 2)
echo "Talisman successfully installed."
echo -ne $(tput sgr0)
}
install_to_all_existing_git_directories() {
PARENT_DIRECTORY=($(pwd))
echo "Installing in all child git directories under $PARENT_DIRECTORY"
cd $PARENT_DIRECTORY
LIST_OF_GIT_DIRECTORIES=($(find . -name .git -type d -prune 2>/dev/null))
for i in "${LIST_OF_GIT_DIRECTORIES[@]}"
do
echo "$i"
cd "$i/hooks/"
if [[ -x "$HOOK" ]] || [[ -x "$HOOK.exe" ]]; then
cd $PARENT_DIRECTORY
continue
else
cp $DOWNLOADED_BINARY "./$HOOK"
chmod +x "./$HOOK"
cd $PARENT_DIRECTORY
fi
done
}
if [ ! -d "./.git" ]; then
install_to_git_templates
read -u1 -p "Do you want to install Talisman in all the child git directories of the current directory?(Y/n)" CHILD_DIRECTORY_INSTALLATION
if [[ $CHILD_DIRECTORY_INSTALLATION == 'Y' ]] || [[ $CHILD_DIRECTORY_INSTALLATION == 'y' ]]; then
install_to_all_existing_git_directories
fi
else
install_to_repo
fi
}
run $0 $@