-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_update_git.sh
191 lines (175 loc) · 5.9 KB
/
install_update_git.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
#!/bin/bash
set -e
check_root() {
if [ -n "$TERMUX_VERSION" ]; then
return 0
fi
if [ "$PACKAGE_MANAGER" = "brew" ]; then
return 0
fi
if [ "$(id -u)" != "0" ]; then
echo "Error: This script must be run as root for $PACKAGE_MANAGER operations"
exit 1
fi
}
detect_system() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
elif type lsb_release &> /dev/null; then
OS=$(lsb_release -si | tr '[:upper:]' '[:lower:]')
elif [ "$(uname -o)" == "Android" ]; then
OS="android"
elif [ "$(uname)" == "Darwin" ]; then
OS="darwin"
if ! command -v brew >/dev/null 2>&1; then
echo "Error: Homebrew is not installed. Please install it first."
exit 1
fi
elif [ -f /etc/debian_version ]; then
OS="debian"
elif [ -f /etc/fedora-release ]; then
OS="fedora"
elif [ -f /etc/centos-release ]; then
OS="centos"
elif [ -f /etc/arch-release ]; then
OS="arch"
elif [ -f /etc/gentoo-release ]; then
OS="gentoo"
else
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
fi
case "$OS" in
"ubuntu"|"debian")
PACKAGE_MANAGER="apt"
;;
"fedora")
PACKAGE_MANAGER="dnf"
;;
"centos")
if command -v dnf >/dev/null 2>&1; then
PACKAGE_MANAGER="dnf"
else
PACKAGE_MANAGER="yum"
fi
;;
"arch")
PACKAGE_MANAGER="pacman"
;;
"gentoo")
PACKAGE_MANAGER="emerge"
;;
"darwin")
PACKAGE_MANAGER="brew"
;;
"android")
PACKAGE_MANAGER="pkg"
;;
*)
PACKAGE_MANAGER="unknown"
;;
esac
}
install_update_git() {
local package_manager=$1
local exit_code=0
if command -v git >/dev/null 2>&1; then
echo "Git is already installed, checking for updates..."
case $package_manager in
"brew")
brew update || exit_code=$?
[ $exit_code -eq 0 ] && brew upgrade git || exit_code=$?
;;
"apt")
apt-get update || exit_code=$?
[ $exit_code -eq 0 ] && apt-get upgrade -y git || exit_code=$?
[ $exit_code -eq 0 ] && apt-get clean || exit_code=$?
;;
"yum")
yum update -y || exit_code=$?
[ $exit_code -eq 0 ] && yum upgrade -y git || exit_code=$?
[ $exit_code -eq 0 ] && yum clean all || exit_code=$?
;;
"dnf")
dnf check-update || [ $? -eq 100 ] || exit_code=$?
[ $exit_code -eq 0 ] && dnf upgrade -y git || exit_code=$?
[ $exit_code -eq 0 ] && dnf clean all || exit_code=$?
;;
"pacman")
pacman -Syu --noconfirm git || exit_code=$?
[ $exit_code -eq 0 ] && pacman -Scc --noconfirm || exit_code=$?
;;
"emerge")
emerge --sync || exit_code=$?
[ $exit_code -eq 0 ] && emerge -u dev-vcs/git || exit_code=$?
[ $exit_code -eq 0 ] && eclean distfiles || exit_code=$?
;;
"pkg")
pkg update || exit_code=$?
[ $exit_code -eq 0 ] && pkg upgrade -y git || exit_code=$?
[ $exit_code -eq 0 ] && pkg clean -y || exit_code=$?
;;
*)
echo "Error: Package manager '$package_manager' not supported."
exit 1
;;
esac
else
echo "Git is not installed, installing it now..."
case $package_manager in
"brew")
brew update || exit_code=$?
[ $exit_code -eq 0 ] && brew install git || exit_code=$?
;;
"apt")
apt-get update || exit_code=$?
[ $exit_code -eq 0 ] && apt-get install -y git || exit_code=$?
[ $exit_code -eq 0 ] && apt-get clean || exit_code=$?
;;
"yum")
yum update -y || exit_code=$?
[ $exit_code -eq 0 ] && yum install -y git || exit_code=$?
[ $exit_code -eq 0 ] && yum clean all || exit_code=$?
;;
"dnf")
dnf check-update || [ $? -eq 100 ] || exit_code=$?
[ $exit_code -eq 0 ] && dnf install -y git || exit_code=$?
[ $exit_code -eq 0 ] && dnf clean all || exit_code=$?
;;
"pacman")
pacman -Syu --noconfirm git || exit_code=$?
[ $exit_code -eq 0 ] && pacman -Scc --noconfirm || exit_code=$?
;;
"emerge")
emerge --sync || exit_code=$?
[ $exit_code -eq 0 ] && emerge dev-vcs/git || exit_code=$?
[ $exit_code -eq 0 ] && eclean distfiles || exit_code=$?
;;
"pkg")
pkg update || exit_code=$?
[ $exit_code -eq 0 ] && pkg install -y git || exit_code=$?
[ $exit_code -eq 0 ] && pkg clean -y || exit_code=$?
;;
*)
echo "Error: Package manager '$package_manager' not supported."
exit 1
;;
esac
fi
if [ $exit_code -ne 0 ]; then
echo "Error: Failed to install/update git using $package_manager"
exit $exit_code
fi
}
main() {
detect_system
if [ "$PACKAGE_MANAGER" = "unknown" ]; then
echo "Error: No supported package manager found (brew, apt, yum, dnf, pacman, emerge or pkg)."
exit 1
fi
check_root
install_update_git "$PACKAGE_MANAGER"
echo "Git installation/update completed successfully using $PACKAGE_MANAGER."
}
main
exit 0