-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-scripts
More file actions
executable file
·117 lines (94 loc) · 3.03 KB
/
update-scripts
File metadata and controls
executable file
·117 lines (94 loc) · 3.03 KB
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
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
repo_dir="$HOME/.local/share/cp-scripts"
bin_dir="$HOME/.local/bin"
official_remote="https://github.com/ICPC-Amrita/cp-scripts.git"
usage() {
cat << EOF
Usage: $(basename "${BASH_SOURCE[0]}")
Updates cp-scripts by fetching and resetting to origin/main.
EOF
exit
}
cleanup() {
status=$?
trap - SIGINT SIGTERM ERR EXIT
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' CYAN='\033[0;36m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' CYAN=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1}
msg "$msg"
exit "$code"
}
check_update() {
cd "$repo_dir" || return 1
# Ensure remote is official
current_remote=$(git remote get-url origin 2>/dev/null || echo "")
if [[ "$current_remote" != "$official_remote" ]]; then
msg "${ORANGE}Remote is not official, fixing...${NOFORMAT}"
git remote remove origin || true
git remote add origin "$official_remote"
msg "${GREEN}Remote set to official: $official_remote${NOFORMAT}"
fi
# Current commit info
current_commit=$(git rev-parse HEAD)
commit_short=$(git rev-parse --short HEAD)
commit_date=$(git show -s --format=%ci "$current_commit" 2>/dev/null || echo "unknown")
msg "${CYAN}Current version: $commit_short ($commit_date)${NOFORMAT}"
# Fetch & compare
msg "${ORANGE}Checking for updates...${NOFORMAT}"
git fetch origin --prune
remote_commit=$(git rev-parse origin/main)
if [[ "$current_commit" == "$remote_commit" ]]; then
msg "${GREEN}cp-scripts is already up to date.${NOFORMAT}"
return 0
else
msg "${GREEN}Update available.${NOFORMAT}"
return 1
fi
}
update_repo() {
mkdir -p "$repo_dir" "$bin_dir"
# If repo missing → clone
if [[ ! -d "$repo_dir/.git" ]]; then
if [[ -d "$repo_dir" ]]; then
msg "${ORANGE}$repo_dir exists but is not a git repo, replacing...${NOFORMAT}"
rm -rf "$repo_dir"
else
msg "${ORANGE}$repo_dir does not exist, cloning...${NOFORMAT}"
fi
git clone "$official_remote" "$repo_dir" || die "${RED}Clone failed${NOFORMAT}"
msg "${GREEN}cp-scripts cloned successfully.${NOFORMAT}"
fi
# Check if update needed
if ! check_update; then
msg "${ORANGE}Updating to latest commit...${NOFORMAT}"
git -C "$repo_dir" reset --hard origin/main || die "${RED}Reset failed${NOFORMAT}"
new_commit=$(git -C "$repo_dir" rev-parse HEAD)
new_short=$(git -C "$repo_dir" rev-parse --short HEAD)
new_date=$(git -C "$repo_dir" show -s --format=%ci "$new_commit")
msg "${GREEN}Updated to version: $new_short ($new_date)${NOFORMAT}"
fi
# Symlink executables into ~/.local/bin
msg "${ORANGE}Linking scripts into $bin_dir...${NOFORMAT}"
for f in "$repo_dir"/*; do
if [[ -f "$f" && -x "$f" ]]; then
ln -sf "$f" "$bin_dir/$(basename "$f")"
fi
done
msg "${GREEN}Symlinks in place.${NOFORMAT}"
}
# ---- main ----
setup_colors
update_repo