-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgit.sh
136 lines (110 loc) · 3.5 KB
/
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
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
MAGENTA='\033[0;35m'
NC='\033[0m'
error() {
echo -e "${RED}[ERROR]${NC} $1"
exit 1
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
task() {
echo -e "${MAGENTA}[TASK]${NC} $1"
}
run_with_spinner() {
local msg="$1"
shift
local cmd=("$@")
local pid
local spin_chars='🕘🕛🕒🕡'
local delay=0.1
local i=0
"${cmd[@]}" > /dev/null 2>&1 &
pid=$!
printf "${MAGENTA}[TASK]${NC} %s... " "$msg"
while kill -0 "$pid" 2>/dev/null; do
i=$(( (i+1) %4 ))
printf "\r${MAGENTA}[TASK]${NC} %s... ${CYAN}%s${NC}" "$msg" "${spin_chars:$i:1}"
sleep "$delay"
done
wait "$pid"
local exit_status=$?
# Clear the line
printf "\r\033[K"
return $exit_status
}
# Check for existing Git installation
check_existing_git() {
if command -v git &>/dev/null; then
warn "Git is already installed: $(git --version | awk '{print $3}')"
read -rp "Do you want to reinstall/update Git? (y/N): " choice
if [[ "${choice,,}" == "y" ]]; then
return 0
else
info "Skipping Git installation."
exit 0
fi
fi
}
install_git() {
task "Identifying package manager"
if command -v apt &>/dev/null; then
info "Detected APT package manager"
run_with_spinner "Updating package lists" sudo apt update -y || error "Failed to update packages"
run_with_spinner "Installing Git" sudo apt install git -y || error "Failed to install Git"
elif command -v dnf &>/dev/null; then
info "Detected DNF package manager"
run_with_spinner "Installing Git" sudo dnf install git -y || error "Failed to install Git"
elif command -v yum &>/dev/null; then
info "Detected YUM package manager"
run_with_spinner "Installing Git" sudo yum install git -y || error "Failed to install Git"
elif command -v pacman &>/dev/null; then
info "Detected Pacman package manager"
run_with_spinner "Installing Git" sudo pacman -S git --noconfirm || error "Failed to install Git"
elif command -v zypper &>/dev/null; then
info "Detected Zypper package manager"
run_with_spinner "Installing Git" sudo zypper install git -y || error "Failed to install Git"
else
error "Unsupported package manager. Install Git manually."
fi
}
verify_installation() {
task "Verifying installation"
if command -v git &>/dev/null; then
success "Git successfully installed: $(git --version | awk '{print $3}')"
else
warn "Git binary not found in PATH. Searching system..."
local possible_paths=(
"/usr/bin/git"
"/usr/local/bin/git"
"/opt/homebrew/bin/git"
"/snap/bin/git"
)
for path in "${possible_paths[@]}"; do
if [[ -x "$path" ]]; then
warn "Found Git at: $path"
info "You can add this to your PATH by running:"
info "echo 'export PATH=\"$(dirname "$path"):\$PATH\"' >> ~/.bashrc && source ~/.bashrc"
return
fi
done
error "Git installation verification failed. Check manually with 'which git'"
fi
}
main() {
check_existing_git
install_git
verify_installation
}
main