-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
131 lines (117 loc) · 4.63 KB
/
install.sh
File metadata and controls
131 lines (117 loc) · 4.63 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/sh
set -e
REPO="se7uh/git-id"
INSTALL_DIR="$HOME/.local/bin"
# Detect OS and architecture
OS="$(uname -s)"
ARCH="$(uname -m)"
case "$OS" in
Linux)
case "$ARCH" in
x86_64) ASSET="git-id-x86_64-linux" ;;
aarch64) ASSET="git-id-aarch64-linux" ;;
*) echo "Unsupported architecture: $ARCH" >&2; exit 1 ;;
esac
;;
Darwin)
case "$ARCH" in
arm64) ASSET="git-id-aarch64-macos" ;;
*) echo "Unsupported architecture: $ARCH (only Apple Silicon is supported)" >&2; exit 1 ;;
esac
;;
*)
echo "Unsupported OS: $OS" >&2
exit 1
;;
esac
# Fetch the latest release download URL
API_URL="https://api.github.com/repos/$REPO/releases/latest"
if command -v curl >/dev/null 2>&1; then
if command -v jq >/dev/null 2>&1; then
DOWNLOAD_URL="$(curl -fsSL "$API_URL" | jq -r --arg name "$ASSET" '.assets[] | select(.name == $name) | .browser_download_url' | head -n 1)"
else
DOWNLOAD_URL="$(curl -fsSL "$API_URL" | grep '"browser_download_url"' | grep "$ASSET" | sed -n 's/.*"browser_download_url":[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)"
fi
elif command -v wget >/dev/null 2>&1; then
if command -v jq >/dev/null 2>&1; then
DOWNLOAD_URL="$(wget -qO- "$API_URL" | jq -r --arg name "$ASSET" '.assets[] | select(.name == $name) | .browser_download_url' | head -n 1)"
else
DOWNLOAD_URL="$(wget -qO- "$API_URL" | grep '"browser_download_url"' | grep "$ASSET" | sed -n 's/.*"browser_download_url":[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)"
fi
else
echo "curl or wget is required" >&2
exit 1
fi
if [ -z "$DOWNLOAD_URL" ]; then
echo "Could not find download URL for $ASSET" >&2
exit 1
fi
# Fetch checksums (single file in the release)
CHECKSUM_ASSET="SHA256SUMS"
# Fetch the checksum file download URL
if command -v curl >/dev/null 2>&1; then
if command -v jq >/dev/null 2>&1; then
CHECKSUM_URL="$(curl -fsSL "$API_URL" | jq -r --arg name "$CHECKSUM_ASSET" '.assets[] | select(.name == $name) | .browser_download_url' | head -n 1)"
else
CHECKSUM_URL="$(curl -fsSL "$API_URL" | grep '"browser_download_url"' | grep "$CHECKSUM_ASSET" | sed -n 's/.*"browser_download_url":[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)"
fi
elif command -v wget >/dev/null 2>&1; then
if command -v jq >/dev/null 2>&1; then
CHECKSUM_URL="$(wget -qO- "$API_URL" | jq -r --arg name "$CHECKSUM_ASSET" '.assets[] | select(.name == $name) | .browser_download_url' | head -n 1)"
else
CHECKSUM_URL="$(wget -qO- "$API_URL" | grep '"browser_download_url"' | grep "$CHECKSUM_ASSET" | sed -n 's/.*"browser_download_url":[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)"
fi
else
echo "curl or wget is required" >&2
exit 1
fi
if [ -z "$CHECKSUM_URL" ]; then
echo "Could not find download URL for $CHECKSUM_ASSET" >&2
exit 1
fi
# Create install directory if it doesn't exist
mkdir -p "$INSTALL_DIR"
# Download binary to a temporary file first to avoid leaving a corrupted binary on failure
echo "Downloading $ASSET..."
TMP_FILE="$(mktemp "$INSTALL_DIR/git-id.XXXXXX")" || { echo "Failed to create temporary file" >&2; exit 1; }
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$DOWNLOAD_URL" -o "$TMP_FILE"
CHECKSUMS_CONTENT="$(curl -fsSL "$CHECKSUM_URL")" || { rm -f "$TMP_FILE"; echo "Failed to download checksum file" >&2; exit 1; }
else
wget -qO "$TMP_FILE" "$DOWNLOAD_URL"
CHECKSUMS_CONTENT="$(wget -qO- "$CHECKSUM_URL")" || { rm -f "$TMP_FILE"; echo "Failed to download checksum file" >&2; exit 1; }
fi
# Verify SHA256 checksum
CHECKSUM_LINE="$(printf '%s\n' "$CHECKSUMS_CONTENT" | grep -E " ${ASSET}$" | head -n 1)"
if [ -z "$CHECKSUM_LINE" ]; then
rm -f "$TMP_FILE"
echo "Checksum line not found for $ASSET in $CHECKSUM_ASSET" >&2
exit 1
fi
EXPECTED_HASH="$(echo "$CHECKSUM_LINE" | awk '{print $1}')"
if command -v sha256sum >/dev/null 2>&1; then
ACTUAL_HASH="$(sha256sum "$TMP_FILE" | awk '{print $1}')"
elif command -v shasum >/dev/null 2>&1; then
ACTUAL_HASH="$(shasum -a 256 "$TMP_FILE" | awk '{print $1}')"
else
echo "Warning: sha256sum/shasum not found, skipping checksum verification" >&2
ACTUAL_HASH="$EXPECTED_HASH"
fi
if [ "$ACTUAL_HASH" != "$EXPECTED_HASH" ]; then
rm -f "$TMP_FILE"
echo "Checksum verification failed!" >&2
echo " Expected: $EXPECTED_HASH" >&2
echo " Got: $ACTUAL_HASH" >&2
exit 1
fi
mv "$TMP_FILE" "$INSTALL_DIR/git-id"
chmod +x "$INSTALL_DIR/git-id"
echo ""
echo "git-id installed to $INSTALL_DIR/git-id"
echo ""
echo "Make sure $INSTALL_DIR is in your PATH."
echo ""
echo "To enable shell completions, run:"
echo " git-id completions bash # for Bash"
echo " git-id completions zsh # for Zsh"
echo " git-id completions fish # for Fish"