4
4
# The script sets up Git configuration globally
5
5
# and backs up the existing Git configuration files
6
6
7
- trap ' echo -e "\033[31mError: script failed \033[0m" >&2 ; exit 1 ' ERR
7
+ trap ' err=$?; echo >&2 -e "\e[1;31mThe script catched this unusual error \033[0m"; exit $err ' ERR
8
8
9
9
function usage() {
10
10
cat 1>&2 << EOF
11
11
The installer for $( basename $0 )
12
12
13
13
USAGE:
14
- bash $( basename $0 ) [ -b BRANCH_NAME ] [ -e "EMAIL" ] [ -u "USERNAME " ]
14
+ ./ $( basename $0 ) [ -b " BRANCH_NAME" ] [ -e "EMAIL" ] [ -u "USER_NAME " ]
15
15
16
16
OPTIONS:
17
17
-h Usage
18
18
-b The default branch name (default is "main")
19
19
-e required: The email that will be associated with commits
20
- -u required: The name that will be associated with commits
20
+ -u required: The name that will be associated with commits (e.g, "Martin Scorcese")
21
21
EOF
22
22
}
23
23
24
24
function err() {
25
25
echo -e " \e[1;31m$1 \033[0m" >&2
26
- echo
27
26
}
28
27
29
28
maybe_install_git () {
30
29
if ! [ -x " $( command -v git) " ]; then
31
30
err " I need Git. Please install it on your system. For example 'sudo apt install -y git' on Debian"
31
+ exit 1
32
32
fi
33
33
}
34
34
@@ -48,63 +48,59 @@ function configure_git() {
48
48
git config --global alias.grep ' !git ls-files | grep -i'
49
49
}
50
50
51
- main () {
52
-
53
- local BACKUP_FOLDER=" $HOME /gitconfig-backups"
54
-
55
- while getopts " hb:e:u:" opt; do
56
- case " $opt " in
57
- b)
58
- local GIT_DEFAULT_BRANCH=${OPTARG}
59
- ;;
60
- e)
61
- local GIT_USER_EMAIL=${OPTARG}
62
- ;;
63
- u)
64
- local GIT_USER_NAME=${OPTARG}
65
- ;;
66
- h)
67
- usage;
68
- exit 0
69
- ;;
70
- : )
71
- err " No parameter provided"
72
- usage;
73
- exit 1
74
- ;;
75
- esac
76
- done
77
-
78
- shift $(( OPTIND- 1 )) # clear options
79
-
80
- mkdir -p " $BACKUP_FOLDER "
81
- cd " $PWD /files"
82
- for entry in $( ls .??* )
83
- do
84
- if [ -f " $HOME /$entry " ]; then
85
- cp -n " $HOME /$entry " " $BACKUP_FOLDER "
86
- fi
51
+ BACKUP_FOLDER=" $HOME /gitconfig-backups"
87
52
88
- cp -n $entry $HOME
89
- done
90
-
91
- if [ -z " ${GIT_USER_EMAIL-} " ] || [ -z " ${GIT_USER_NAME-} " ]; then
92
- usage
93
- exit 1
53
+ while getopts " hb:e:u:" opt; do
54
+ case " $opt " in
55
+ b)
56
+ GIT_DEFAULT_BRANCH=${OPTARG}
57
+ ;;
58
+ e)
59
+ GIT_USER_EMAIL=${OPTARG}
60
+ ;;
61
+ u)
62
+ GIT_USER_NAME=${OPTARG}
63
+ ;;
64
+ h)
65
+ usage;
66
+ exit 0
67
+ ;;
68
+ : )
69
+ err " No parameter provided"
70
+ usage;
71
+ exit 1
72
+ ;;
73
+ esac
74
+ done
75
+
76
+ shift $(( OPTIND- 1 )) # clear options
77
+
78
+ mkdir -p " $BACKUP_FOLDER "
79
+ cd " $PWD /files"
80
+ for entry in $( find . -type f -name ' \.*' -print)
81
+ do
82
+ if [ -f " $HOME /$entry " ]; then
83
+ cp -n " $HOME /$entry " " $BACKUP_FOLDER "
94
84
fi
95
85
96
- maybe_install_git
86
+ cp -n $entry $HOME
87
+ done
97
88
98
- if [ -f " $HOME /.gitconfig" ]; then
99
- cp -n " $HOME /.gitconfig" " $BACKUP_FOLDER "
100
- fi
89
+ if [ -z " ${GIT_USER_EMAIL-} " ] || [ -z " ${GIT_USER_NAME-} " ]; then
90
+ err " The script failed to set variables for user email and name"
91
+ usage
92
+ exit 1
93
+ fi
101
94
102
- configure_git " ${GIT_DEFAULT_BRANCH-main} " " $GIT_USER_EMAIL " " $GIT_USER_NAME "
95
+ maybe_install_git
103
96
104
- clear
105
- cd -
106
- echo -e " \033[32mdone:\033[0m "
107
- git config --global --list
108
- }
97
+ if [ -f " $HOME /.gitconfig " ] ; then
98
+ cp -n " $HOME /.gitconfig " " $BACKUP_FOLDER "
99
+ fi
100
+
101
+ configure_git " ${GIT_DEFAULT_BRANCH-main} " " $GIT_USER_EMAIL " " $GIT_USER_NAME "
109
102
110
- main
103
+ clear
104
+ cd -
105
+ echo -e " \033[32mdone:\033[0m"
106
+ git config --global --list
0 commit comments