-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgitconfig.sh
executable file
·134 lines (115 loc) · 4.27 KB
/
gitconfig.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
#!/usr/bin/env bash
set -o pipefail
set -o errtrace
# The variable ON_ERROR only takes exit for exit inmediately or return for only inform the error presence
ON_ERROR=return
RESULT=0
trap catch_errors ERR
function catch_errors() {
RESULT=$?
echo "ERROR: Command ${BASH_COMMAND} line ${LINENO} failed with code ${RESULT}"
${ON_ERROR} ${RESULT}
}
if git status &> /dev/null
then
SCRIPT_BASEDIR=$(git rev-parse --show-toplevel)
else
SCRIPT_BASEDIR="$(dirname $(readlink -f "${BASH_SOURCE[0]}"))"
fi
PROGNAME="$(basename ${0})"
cd ${SCRIPT_BASEDIR}
# Global variables
function help()
{
cat << ENDHELP1
===========================================================================
Configure Git client using global scope
===========================================================================
ENDHELP1
if [ "$*" != "" ]; then echo -e "$*\n" >&2; fi
cat << ENDHELP2
Usage: ./${PROGNAME} --git-username "User name" --git-useremail useremail@domain
--git-username The user name for Git client. Use double or single quotes to enclose
--git-useremail The user email for Git client
Common options: ./${PROGNAME} [--help [all]] | <program_options_see_usage> [--debug] [--no-interactive] [--] [--options_for_wrapper_content...]
--help Shows this help
--help all Print also detailed information and examples if provided
--debug Sets the DEBUG environment variable to debug the program itself (not the wrapper) if used
--no-interactive Disable interactive questions
-- End of options and arguments of the program. Then all others are transferred to the wrapper content if used "\$@"
--options_for_wrapper_content... If used a wrapper one example is --debug
ENDHELP2
if [[ "${HELPALL}" == "true" ]]
then
cat << ENDHELP3
Examples:
./${PROGNAME} --git-username "Mrs. Alice" --git-useremail [email protected]
ENDHELP3
fi
exit 10
}
# Execute with required arguments
if [ "$#" == "0" ]; then help; fi
while [ $# -gt 0 ] ; do
case "${1}" in
--git-username)
[[ "${GITMYUSERNAME}" != "" ]] && help "ERROR: Repeated parameters"
[[ "${2}" == "" || "${2}" =~ ^--.*$ ]] && help "ERROR: Missing argument --git-username"
GITMYUSERNAME="${2}"
shift
;;
--git-useremail)
[[ "${GITMYEMAIL}" != "" ]] && help "ERROR: Repeated parameters"
[[ "${2}" == "" || "${2}" =~ ^--.*$ ]] && help "ERROR: Missing argument --git-useremail"
GITMYEMAIL="${2}"
shift
;;
--help)
[[ $# -eq 2 && "${2}" == "all" ]] && HELPALL=true
help
;;
--debug)
[[ "${DEBUG}" != "" ]] && help "ERROR: Repeated parameters"
DEBUG=true
;;
--no-interactive)
[[ "${NO_INTERACTIVE}" != "" ]] && help "ERROR: Repeated parameters"
NO_INTERACTIVE=true
;;
--)
# End of options and arguments of the program. Then all others are transferred to the wrapper content if used ("$@")
shift
break
;;
-*)
help "ERROR: Unknown option <${1}>"
;;
*)
help "ERROR: Unknown argument <${1}>"
;;
esac
shift
done
# Default options
GITCONFIGURE=false
# Required arguments if any provided
[[ -z "${GITMYUSERNAME}" ]] && [[ ! -z "${GITMYEMAIL}" ]] && help "ERROR: Missing argument --gitusername"
[[ ! -z "${GITMYUSERNAME}" ]] && [[ -z "${GITMYEMAIL}" ]] && help "ERROR: Missing argument --gituseremail"
[[ ! -z "${GITMYUSERNAME}" ]] && [[ ! -z "${GITMYEMAIL}" ]] && GITCONFIGURE=true
# Basic verification of parameter values
echo "${GITMYUSERNAME}" | grep -Pxq '\w+|\w+((\.|,|-|_|\s)?(\w+|\.))+' || help "ERROR: The --gitusername value must be a valid value"
echo "${GITMYEMAIL}" | grep -Pxq '([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})' || help "ERROR: The --gituseremail value must be a valid value"
[[ "$(type -p git)" != "/usr/bin/git" ]] && help "ERROR: Git is not installed in Cygwin"
if [[ "${GITCONFIGURE}" == "true" ]]
then
echo "INFO: Configure Git client using global scope"
echo "INFO: Git user name <${GITMYUSERNAME}>"
echo "INFO: Git user email <${GITMYEMAIL}>"
git config --global user.name "${GITMYUSERNAME}"
git config --global user.email "${GITMYEMAIL}"
git config --global http.sslVerify false
fi
echo "INFO: Show Git client config global scope"
git config -l --global
[[ "${RESULT}" != "0" ]] && echo "ERROR: Some errors exists. Error code ${RESULT}"
exit ${RESULT}