-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt-default-branch-commit
executable file
·79 lines (66 loc) · 3.09 KB
/
prompt-default-branch-commit
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
#!/usr/bin/env bash
# ---------------------------------------------------------------------------------------- #
# Description #
# ---------------------------------------------------------------------------------------- #
# A simply script to run as a pre-commit hook to prompt the user before commits to the #
# default main branch are accepted. #
# ---------------------------------------------------------------------------------------- #
ABD=false
if [[ $ABD = true ]]; then
DEFAULT_BRANCH=$(git remote show origin | grep 'HEAD' | cut -d':' -f2 | sed -e 's/^ *//g' -e 's/ *$//g')
else
DEFAULT_BRANCH='master'
fi
PROMPT="Are you sure you want to commit to the ${DEFAULT_BRANCH} branch? [Yes/No] "
function check_for_confirmation
{
local valid_response=false
local return_code=2
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
while [[ "${valid_response}" = false ]]
do
read -r -p "${PROMPT}" response
case $response in
[yY][eE][sS]|[yY])
valid_response=true
return_code=0
;;
[nN][oO]|[nN])
valid_response=true
return_code=1
;;
*)
;;
esac
done
# Reclose STDIO
exec <&-
echo "${return_code}"
}
# ---------------------------------------------------------------------------------------- #
# Main() #
# ---------------------------------------------------------------------------------------- #
# The main function where all of the heavy lifting and script config is done. #
# ---------------------------------------------------------------------------------------- #
function main()
{
local confirmation=0
if branch=$(git rev-parse --abbrev-ref HEAD 2>&1); then
if [[ "${branch}" = "${DEFAULT_BRANCH}" ]]; then
confirmation=$(check_for_confirmation)
fi
fi
exit "${confirmation}"
}
# ---------------------------------------------------------------------------------------- #
# Main() #
# ---------------------------------------------------------------------------------------- #
# The actual 'script' and the functions/sub routines are called in order. #
# ---------------------------------------------------------------------------------------- #
main "${@}"
# ---------------------------------------------------------------------------------------- #
# End of Script #
# ---------------------------------------------------------------------------------------- #
# This is the end - nothing more to see here. #
# ---------------------------------------------------------------------------------------- #