-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup
executable file
·76 lines (65 loc) · 2.08 KB
/
setup
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
LANG=C
# shellcheck source=./shellib/shellib.sh
. "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/shellib/shellib.sh"
function setup_hook() {
local hook="$1"
local script="scripts/${2:-$1}"
if [ -L ".git/hooks/$hook" ] && [ "$(readlink .git/hooks/"$hook")" == "../../$script" ]; then
info "$1 hook is installed"
else
if [ ! -f ".git/hooks/$hook" ]; then
if ln -s "../../$script" ".git/hooks/$hook"; then
info "$hook hook has been installed" "$symbol_done"
else
err "$hook hook installation failed, cannot create symbolic link" "$symbol_failed"
return "$status_err"
fi
else
err "$hook cannot be installed" "$symbol_failed"
info "Try to remove '.git/hooks/$hook' first" "$symbol_tip"
return "$status_err"
fi
fi
}
function setup() {
# Install pre-commit for commit-msg hook
if pre-commit install -t commit-msg 1>/dev/null; then
info 'commit-msg hook is installed'
else
err 'commit-msg hook installation failed'
return "$status_err"
fi
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && git rev-parse --show-toplevel)"
pushd "$REPO_DIR" 1>/dev/null
# Setup git hooks
if ! setup_hook 'pre-commit' ||
! setup_hook 'pre-merge-commit' 'pre-commit' ||
! setup_hook 'pre-push'; then
return "$status_err"
fi
popd 1>/dev/null
# Check if GL_TOKEN is set
if [ -z "${GL_TOKEN:-}" ]; then
notice 'environment variable GL_TOKEN is not set, pre-commit hook gitlab-ci-linter will be skipped'
info 'You might set up environment variable GL_TOKEN' "$symbol_tip"
else
info 'GL_TOKEN is set'
fi
}
# Main
function main() {
if is_root; then
err "Shouldn't be run as root"
info 'Try again as non-root' "$symbol_tip"
return "$status_err"
else
setup
fi
}
# Skip execution under test
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
main
fi